Assignment Statements: Message 'And Now For Something Completely Different' N 17 Pi 3.141592653589793
Assignment Statements: Message 'And Now For Something Completely Different' N 17 Pi 3.141592653589793
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.
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
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:
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
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.
•Python creates a variable name the first time when they are assigned a value.
3. List assignment:
[x, y] = [2, 4]
print('x = ', x)
print('y = ', y)
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.
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)