Python Programming with Corey Schafer
Lecture 3 - Integers and Floats
• Integer is whole number and float is a decimal number
• To see data type
num = 3
print(type(num))
→ <class ‘int’>
It shows that it is an integer
num = 3.14
print(type(num))
→ <class ‘float’>
It shows that it is a float
• Arithmetic operators
Addition
print(3 + 2)
→5
Subtraction
print(3 - 2)
→1
Multiplication
print(3 * 2)
→6
Division
print(3 / 2)
→ 1.5
Floor division (whole number only, no decimal)
print(3 // 2)
→1
Exponent
print(3 ** 2)
→9
Modulus (remainder after division)
print(13 % 5)
→3
Modulus by 2 is used to check if number is even or odd
• Arithmetic rules are followed
print(3 * 2 + 1) or print(1 + 2 * 3)
→7
print(3 * (2 + 1))
→9
• Incrementing values
num = 1
num = num + 1
print(num)
→2
num = 1
num += 1
print(num)
→2
• Multiplying values
num = 1
num *= 10
print(num)
→ 10
• Absolute function(removing minus sign)
print(abs(-3))
→3
• Round function(rounding up or down)
print(round(3.75))
→4
print(round(3.75, 1))
→ 3.8
To round to first decimal place
• Comparison operators
Equal
num_1 = 3
num_2 = 2
print(num_1 == num_2)
→ False
Not equal
num_1 = 3
num_2 = 2
print(num_1 != num_2)
→ True
Greater than
num_1 = 3
num_2 = 2
print(num_1 > num_2)
→ True
Less than
num_1 = 3
num_2 = 2
print(num_1 < num_2)
→ False
Greater than or equal to
num_1 = 3
num_2 = 2
print(num_1 >= num_2)
→ True
Less than or equal to
num_1 = 3
num_2 = 2
print(num_1 <= num_2)
→ False
num_1 = '100'
num_2 = '200'
print(num_1 + num_2)
→ 100200
It is because adding strings just concatenates them together
• Casting (make integer)
num_1 = '100'
num_2 = '200'
num_1 = int(num_1)
num_2 = int(num_2)
print(num_1 + num_2)
→ 300