Python Fundamentals-Lab-01 Exercises Practice
Python Fundamentals-Lab-01 Exercises Practice
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.
Out[8]: 0
Out[9]: 1
Variables:
Variables are containers for storing data values.
Unlike other programming languages, Python has no command for declaring a variable.
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.
Out[13]: 5
Out[14]: 3
Out[15]: 8
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
In [21]: 1var = 1
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
x != y # x is not equal to y
4
x <= y # x is less than or equal to y
x is y # x is the same as y
In [22]: x = 10 y =
20
In [23]: x == y # x is equal to y
Out[23]: False
Out[24]: True
Out[25]: False
Out[26]: True
Out[27]: False
In [28]: x <= y # x is less than or equal to y
Out[28]: True
Out[29]: False
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
False