Variables
Variables
Introduction
Variables are containers used to store values. In Python, variables are defined using the assignment operator =
. For example:
x = 1
y = 100.0
z = "good"
Variables can also be updated using the assignment operator:
x = 1
print('The initial value of x is', x)
x = 2
print('The value after updating x is', x)
The output is:
The initial value of x is 1
The value after updating x is 2
Assignment Operator
The syntax of the assignment statement is:
<variable-name> = <expression>
The assignment operator works from right to left. For example:
x = 1 + 2 * 3 / 2
print(x)
The output is:
4.0
Having a literal to the left of the assignment operator will result in an error:
3 = x
This will throw the following error:
SyntaxError: cannot assign to literal
Dynamic Typing
Python supports dynamic typing. In a dynamically typed language, a variable is simply a value bound to a name; the value has a type — like int
or str
— but the variable itself doesn’t. For example:
a = 1
print(type(a))
a = 1 / 2
print(type(a))
The output is:
<class 'int'>
<class 'float'>
Referencing vs Defining
When a variable that has already been defined is used in an expression, we say that the variable is being referenced.
x = 2
print(x * x, 'is the square of', x)
If a variable is referenced before it has been assigned a value, Python throws a NameError
:
print(someVar)
The output is:
NameError: name 'someVar' is not defined
Keywords and Naming Rules
Certain words in Python, called keywords, have special meanings. Examples include:
not, and, or, if, for, while, in, is, def, class
Keywords cannot be used as variable names:
and = 2
In addition to this restriction, variable names in Python must follow these rules:
- A variable name can only contain alphanumeric characters and underscores:
a-z
,A-Z
,0-9
,_
- A variable name must start with a letter or an underscore.
- Variable names are case-sensitive (
age
,Age
, andAGE
are three different variables).
Reusing Variables
Variables can be used in computing the values of other variables. For example:
x = 10
y = x ** 2
z = (x + 1) * (y + 1)
Multiple Assignment
You can assign values to multiple variables in one line:
x, y = 1, 2
Another way is to assign the same value to multiple variables:
x = y = z = 10
Even though x
, y
, and z
start off equal, the equality is broken if one of them is updated:
x = x * 1
y = y * 2
z = z * 3
print(x, y, z)
The output is:
10 20 30
Assignment Shortcuts
Python provides shorthand notations for arithmetic operations combined with assignment:
Shortcut | Meaning |
---|---|
x += a |
x = x + a |
x -= a |
x = x - a |
x *= a |
x = x * a |
x /= a |
x = x / a |
x %= a |
x = x % a |
x **= a |
x = x ** a |
For example:
x = 1
x += 1
print(x)
The output is:
2
Deleting Variables
You can delete variables using the del
keyword:
x = 100
print('x is a variable whose value is', x)
print('we are now going to delete x')
del x
print(x)
After the deletion, trying to access x
will throw a NameError
:
NameError: name 'x' is not defined