Python Fundamentals
December 12, 2021
Python Data Types
Numbers
Python has two basic number types, integer and float
For example, 2 is an integer and 2.0 is a floating point number which has a decimal attached to it. We
can perform arithmetic operations to either of these numbers types. Let’s try!
In [1]: # addition
5+2
Out[1]: 7
In [2]: # Subtraction
5-2
Out[2]: 3
In [3]: # multiplication
5*2
Out[3]: 10
With a forward slash, we can divide numbers in Python (note, two integers are giving floating point in
results).
If you are using Python 2, you need to add 2.0 in the expression (e.g. 5 / 2.0) to get the same results.
In [4]: 5/2
Out[4]: 2.5
We can compute the power of a number (exponent) with two Asterix ** together
In [5]: 5 ** 2
1
Out[5]: 25
Python follow the order of arithmetic operations, for example: for 1 + 2 * 3 + 4. Python will first
multiply 2 & 3 then perform the other operations.
In [6]: 1 + 2 * 3 + 4
Out[6]: 11
A good practice is to use parentheses “( )” to tell the Python, which operation needs to be performed and
to clarify the order. The operations in the parentheses will be performed first.
In [7]: (1 + 2) * (3 + 4)
Out[7]: 21
Modulus or mod operation is a “%” (percentage sign) in Python. It returns what remains after a
division. With mod operation, we can check if the number is even or odd. Mod (%) returns 0, for even
number and returns 1 for odd numbers.
In [8]: 6 % 2 # returns 0, 6 is even no.
Out[8]: 0
In [9]: 5 % 2 # returns 1, 5 is odd no. (if we divide 5 by 2, remainder is 1)
Out[9]: 1
Variables:
Variables are containers for storing data values.
Unlike other programming languages, Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
At many occasions, we pick a variable with some name and assign object or a data type to that variable.
We can do this in Python using equal sign operator “=”. For example, if we assign a value of 5 to a variable
“x”, whenever we call x, this will return 5 in the output.
It is a common practice to create a variable with multiple names. A good way is to separate them with
underscore “_” between the words. By doing this, you will easily identify the variables e.g. total_profit,
total_loss, first_name etc.
In [10]: name_of_the_variable = 20
In [11]: name_of_the_variable
2
Out[11]: 20
Important regarding variable name: Before we move on to the next data type, we should know that, in
Python: variable name cannot be started with number (e.g. 1var, 2x etc. ), variable name cannot be started
with special characters (e.g. !, @, #,!y,+,- etc. ) * variable name are case sensitive, “Name” is not the same
as “name”, reserved words cannot be a variable name, e.g. “class” is a reserve word in Python, it cannot be
used as a variable name, but “klass” and “Class” works fine.
In [12]: # Let's try x = 5 and y = 3
x=5
y=3
In [13]: # what is x now!
x
Out[13]: 5
In [14]: # what is y now!
y
Out[14]: 3
We can perform arithmetic operations using these variable.
In [15]: # Adding x and y
x+y
Out[15]: 8
In [16]: # Multiplying x and y
x*y
Out[16]: 15
After performing any arithmetic operation, we can assign the result to a new variable.
In [17]: # Subtraction of the variables and assigning the result to a new variable
z=x-y #5-3
In [18]: z
Out[18]: 2
We can re-assign a value to the same variables. This will replace the existing value to its new value.
In [19]: x = y + 2
3
x=x*x
In [20]: x
Out[20]: 25
Let’s try some invalid variable names!
In [21]: 1var = 1
File "<ipython-input-21-460332d99e8f>", line 1
1var = 1
ˆ
SyntaxError: invalid syntax
In [ ]: $var = 3
Booleans
Booleans are simply True and False with capital T and Capital F – just a customized version of 1 and 0.
In [ ]: True
In [ ]: False
In [ ]: 10 == 10
In [ ]: 5 == 10
In [ ]: a = 10 b = 5
a == b
0.3 Comparison Operators
x == y # x is equal to y
x != y # x is not equal to y
x > y # x is greater than y
x < y # x is less than y
x >= y # x is greater than or equal to y
4
x <= y # x is less than or equal to y
x is y # x is the same as y
x is not y # x is not the same as y
In [22]: x = 10 y =
20
In [23]: x == y # x is equal to y
Out[23]: False
In [24]: x != y # x is not equal to y
Out[24]: True
In [25]: x > y # x is greater than y
Out[25]: False
In [26]: x < y # x is less than y
Out[26]: True
In [27]: x >= y # x is greater than or equal to y
Out[27]: False
In [28]: x <= y # x is less than or equal to y
Out[28]: True
In [29]: x is y # x is the same as y
Out[29]: False
In [30]: x is not y # x is not the same as y
Out[30]: True
Logical Operators
There are three logical operators: and, or, and not
In [31]: x = 10
5
print (x > 0 and x < 10)
False
In [32]: print (not (x%2 == 0))
False