Professional Documents
Culture Documents
2.1 Python Variables PDF
2.1 Python Variables PDF
2.1 Python Variables PDF
In [2]: i
Out[2]: 5
When we print our variable to the screen, the value of our variable is 5.
In [3]: i * i * i
Out[3]: 125
To do this, we're assigning (with "=" this operator) new value to our variable.
In [4]: i = 6
i
Out[4]: 6
In [5]: x = y = z = 1
We can also assign multiple values to multiple variables at the same time using with commas.
3
4
5
In [7]: a = 2
b = 3
c = a + 2 * b
What will be variable c? In this calculation, first on the right side of the = operator is being calculated,
In [8]: c
Out[8]: 8
Can not be no spaces between them, if the variable name consist of more than 1 word.
Can not be used reserved words.
Must begin with a letter (a - z, A - Z) or underscore (_)
A variable name **can not** **start** with a **digit**.
Variable names are **case sensitive**
Can not be used **special characters**!
Can be any (reasonable) length
in is lambda not or
Let's one more example and calculate the circumference and area of a circle
In [11]: pi = 3.14
radius = 2
circ = 2 * pi * radius
In [12]: circ
Out[12]: 12.56
In [13]: a = 3
b = 5
print(a,b)
3 5
In [14]: temp = a
a = b
b = temp
print(a,b)
5 3
But, there is a much practical way to exchange values of two variables in Python.
In [15]: a = 3
b = 5
In [17]: a
Out[17]: 5
In [18]: b
Out[18]: 3
a,b,c = c,a,b
print(a)
print(b)
print(c)
5
3
4
In [20]: a = 2
a = a + 1
a
Out[20]: 3
In [21]: a = 5
a += 1 # same as a = a + 1.
a
Out[21]: 6
In [22]: b = 4
b -= 1 # b = b -1
b
Out[22]: 3
In [23]: b = 4
b *= 10
b
Out[23]: 40
If the comment lines are used in a program, these lines are not visible to Python and can not be executed by compiler.
In [24]: # Single Line Comment. This is a comment
print("omer") #This is also a single comment.
omer
In [25]: """
This is
Multiple Line Comment
"""
print("omer")
omer
In [ ]: