0% found this document useful (0 votes)
56 views8 pages

Assignment Statements: Message 'And Now For Something Completely Different' N 17 Pi 3.141592653589793

The document discusses assignment statements in Python. It explains that assignment statements create variables and assign values or objects to them. It provides examples of basic assignment, tuple assignment, list assignment, sequence unpacking, multiple target assignment, and augmented assignment. Variable names must be meaningful and cannot be keywords like "class". Expressions evaluate to a value, while statements perform an action like printing or assigning values.

Uploaded by

Vishal Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views8 pages

Assignment Statements: Message 'And Now For Something Completely Different' N 17 Pi 3.141592653589793

The document discusses assignment statements in Python. It explains that assignment statements create variables and assign values or objects to them. It provides examples of basic assignment, tuple assignment, list assignment, sequence unpacking, multiple target assignment, and augmented assignment. Variable names must be meaningful and cannot be keywords like "class". Expressions evaluate to a value, while statements perform an action like printing or assigning values.

Uploaded by

Vishal Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Programming, Data Structures and Algorithms Using Python

Assignment Statements
An assignment statement creates a new variable and gives it a value:
>>> message = 'And now for something completely different'
>>> n = 17
>>> pi = 3.141592653589793

This example makes three assignments. The first assigns a string to a new variable named message; the second gives
the integer 17 to n; the third assigns the (approximate) value of π to pi.
A common way to represent variables on paper is to write the name with an arrow pointing to its value. This kind of
figure is called a state diagram because it shows what state each of the variables is in (think of it as the variable’s state
of mind). Figure shows the result of the previous example.

ABHAY RATNA PANDEY 1


Programming, Data Structures and Algorithms Using Python

Variable Names
Programmers generally choose names for their variables that are meaningful—they document what the variable is used for.
Variable names can be as long as you like. They can contain both letters and numbers, but they can’t begin with a number. It
is legal to use uppercase letters, but it is conventional to use only lowercase for variables names.
The underscore character, _, can appear in a name. It is often used in names with multiple words, such as your_name or
airspeed_of_unladen_swallow.
If you give a variable an illegal name, you get a syntax error:
>>> 76trombones = 'big parade'
SyntaxError: invalid syntax
>>> more@ = 1000000
SyntaxError: invalid syntax
>>> class = 'Advanced Theoretical Zymurgy'
SyntaxError: invalid syntax

ABHAY RATNA PANDEY 2


Programming, Data Structures and Algorithms Using Python

76trombones is illegal because it begins with a number. more@ is illegal because it contains
an illegal character, @. But what’s wrong with class?
It turns out that class is one of Python’s keywords. The interpreter uses keywords to
recognize the structure of the program, and they cannot be used as variable names.
Python 3 has these keywords:

False class finally is return None continue


for lambda try True def from nonlocal
while and del global not with as
elif if or yield assert else import
pass break except in raise

You don’t have to memorize this list. In most development environments, keywords
are displayed in a different color; if you try to use one as a variable name, you’ll know.
ABHAY RATNA PANDEY 3
Programming, Data Structures and Algorithms Using Python

Expressions and Statements


An expression is a combination of values, variables, and operators. A value all by
itself is considered an expression, and so is a variable, so the following are all legal
expressions:
>>> 42
42
>>> n
17
>>> n + 25
42
When you type an expression at the prompt, the interpreter evaluates it, which
means that it finds the value of the expression. In this example, n has the value 17 and
n + 25 has the value 42.
A statement is a unit of code that has an effect, like creating a variable or displaying a
value.
>>> n = 17
>>> print(n)
The first line is an assignment statement that gives a value to n. The second line is a
print statement that displays the value of n.
When you type a statement, the interpreter executes it, which means that it does
whatever the statement says. In general, statements don’t have values.
ABHAY RATNA PANDEY 4
Programming, Data Structures and Algorithms Using Python

Assignment Statements in Python

We use Python assignment statements to assign objects to names. The target of an assignment statement is

written on the left side of the equal sign (=), and the object on the right can be an arbitrary expression that

computes an object.

There are some important properties of assignment in Python :-

•Assignment creates object references instead of copying the objects.

•Python creates a variable name the first time when they are assigned a value.

•Names must be assigned before being referenced.

•There are some operations that perform assignments implicitly.

ABHAY RATNA PANDEY 5


Programming, Data Structures and Algorithms Using Python

Assignment statement forms :-


1. Basic form: 4. Sequence assignment:
This form is the most common form. In recent version of Python, tuple and list assignment
have been generalized into instances of what we now call
student = 'Geeks'
print(student)
sequence assignment – any sequence of names can be
assigned to any sequence of values, and Python assigns
2. Tuple assignment: the items one at a time by position.

# equivalent to: (x, y) = (50, 100) a, b, c = 'HEY'


x, y = 50, 100
print('a = ', a)
print('x = ', x) print('b = ', b)
print('y = ', y) print('c = ', c)

3. List assignment:
[x, y] = [2, 4]

print('x = ', x)
print('y = ', y)

ABHAY RATNA PANDEY 6


Programming, Data Structures and Algorithms Using Python

5. Extended Sequence unpacking:


It allows us to be more flexible in how we select portions of a sequence to assign.
p, *q = 'Hello'

print('p = ', p)
print('q = ', q)

Here, p is matched with the first character in the string on the right and q with the rest. The starred name (*q)
is assigned a list, which collects all items in the sequence not assigned to other names.

This is especially handy for a common coding pattern such as splitting a sequence and accessing its front and
rest part.

ranks = ['A', 'B', 'C', 'D']


first, *rest = ranks

print("Winner: ", first)


print("Runner ups: ", ', '.join(rest))

ABHAY RATNA PANDEY 7


Programming, Data Structures and Algorithms Using Python

6. Multiple- target assignment:


x = y = 75

print(x, y)

In this form, Python assigns a reference to the same object (the object which is rightmost) to all the target
on the left.

7. Augmented assignment :
The augmented assignment is a shorthand assignment that combines an expression and an assignment.

x = 2

# equivalent to: x = x + 1
x += 1

print(x)

ABHAY RATNA PANDEY 8

You might also like