This section provides a brief overview of:
In this section I am going to focus on integer, floating points, Boolean data types and associated arithmetic operations. 5 is an example of an integer data type whereas 5.0, 6.2 are examples of floating point data types. Note that python also has a string data type which is not covered in this tutorial.
All standard arithmetic operators are defined in Python.
3 + 2
3 - 1
3 / 2
3 // 2 # Integer division
3 ** 2 # 3 to the power of 2
3 % 2 # This is the remainder operator
float(5) # Returns a floating point number
int(5.6) # Returns an integer
Try the following mathematical operations. Are you getting the answer you are expecting?
(1/49)*49
0.1 + 0.1 + 0.1 == 0.3
This is because, floating points are represented as binary fractions. For example, 0.25 is represented as 0.01 $\left( 0 \times \frac{1}{2} + 1 \times \frac{1}{4}\right)$. Decimal number 0.1 cannot be accurately represented as binary fractions which results in the above issues.
Python treats every line as a statement to be executed except the line beginning with a #. # indicates the line contains comments to the right of # sign which are not to be executed. Take the time to write good comments.
# This is a comment
x = 10 # We assign variable x value 10. This is a poor comment.
x = 10 # The initial solution is 10. This is a good comment.
Python has the following comparison operators on all relevant data types:
Boolean is another data type which can take two values – true or false. Python comparisons can be combined using the and, or keyword.
3 == 2
3 != 2
(5 > 3) and (2 < 1)
(5 > 3) or (2 < 1)
not(3 == 2)
In Python, there is no need to declare variables before assigning them. Variables are references to object. Variable type depends on object type.
x = 5 # Assigns 5 to an integer variable x
print(x)
type(x)
y = 5.2 # Assigns 5.2 to a floating point variable y
print("y = ", y)
type(y)
You can assign multiple variables in a single line.
a, b = 10, 22.3
print(a)
print(b)
x + y
float(x) # converts to floating point
int(y) # converts to integer
type(x) #prints the variable type
When you run the command x = 5, you are creating a variable x which is a reference to an integer object 5. Variable x is a reference to the memory location containing integer object 5.
x = 5
y = 5
When you run the above command you are creating two references to the memory location containing integer object 5.
x = x + 5
Once you update x as shown above, the variable references changes.
Some rules on variable names.
Naming conventions are provided at PEP 8 Python Style Guide.
If loops can be used to execute codes based on conditions.
Note that indentations are important for defining code blocks. Normal indentation is one tab or four whitespaces. The code will not compile properly and give errors if it is not indented properly. Watch out for the colons.
x = 10
y = 20
if x < y:
print(x, "is smaller")
else:
print(y, "is smaller")
You can check as many logical conditions as needed using if, elif, and else.
x = 10
print(x)
if x > 5:
print("x is larger than 5")
elif x < 5:
print("x is smaller than 5")
else:
print("x is equal to 5")
You can nest if loops.
x = 5
y = 15
z = 25
if x < y:
if x < z:
print(x, "is the smallest")
else:
print(z, "is the smallest")
else:
if y < z:
print(y, "is the smallest")
else:
print(z, "is the smallest")
While loops execute a section of code until a logical condition is met. The following code prints 1 through 10.
y=0
while y <= x:
print(y)
y=y+1
We can combine logical conditions using and, not.
count = 0
total = 0
while count <= 10 and count % 2 == 0:
total = total + count
count = count + 1
print(total)
We can combine if and while loops also.
count = 0
total = 0
while count <= 10:
if count%2 == 0:
total = total + count
count = count + 1
print(total)
The break statement allows you to exit a loop if an additional constraint is satisfied.
count = 1
total = 0
while count <= 10:
if count % 5 == 0:
break
total = total + count
count = count + 1
print(total)
The continue statement returns the control to the beginning of the while loop.
count = 0
total = 0
while count < 10:
count = count + 1
if count % 2 == 0:
continue
total = total + count
print(total)