Part 3: Python Operators
10. Arithmetic operators - Part 1
Lecture VideoCode Snippet
#Add/Sub/Mul
print("Add/Sub/Mul")
print(2 + 5)
print(2.0 + 5)
print(2 + 5.2)
print(7-2-3.0)
print(5 * 2)
print(5 * 2.0)
print("Float examples")
print(9.2 - 1.2) #Expected: 8.0
print(2.2 * 6) #Expected: 13.2
#Floating point division
print("Floating point division")
print(10/2)
print(10/2.0)
print(3/2)
print(10/3)
print(100/3)
print(1000/3)
#Floor division
print("Floor division")
print(10 // 3)
print(-10 // 3)
print(10 // 2)
print(10.0 //2)
print(10 // 3.0)
11. Arithmetic operators - Part 2
Lecture VideoCode Snippet
#Modulus
print("Modulus")
print(23 % 4)
print(23.0 % 4)
print(23.2 % 4.2)
#Exponent
print("Exponent")
print(2 ** 3)
print(2.0 ** 3)
print(4 ** -2)
#Augmented assignment
print("Augmented assignment")
a = 10
print(a)
a += 5 #a = a + 5
print(a)
a *= 5 #a = a * 5
print(a)
#Using variables
a = 10
b = 5
total = a + b
print(total)
12. Binary representation of Integers in Python
Lecture Video13. Bitwise operators 1 - Bitwise NOT, Bitwise OR, Bitwise AND
Lecture VideoCode Snippet
14. Bitwise operators 2 - Left shift and Right shift operators
Lecture VideoCode Snippet
15. Comparison Operators - Part 1
Lecture VideoCode Snippet
#Simple examples
print("Simple examples")
print(10 == 10)
print(30 == 50)
print(30 != 50)
print(50 != 50)
print(80 >= 80)
print(10.5 < 20.2)
print("HELLO" == "HeLlo")
print("abc" == "abc")
#Using == and != between different datatype objects
print("Using == and != between different datatype objects")
print(10 == 10.0)
print(10 == 7.5)
print("10" == 10)
print("10" != 10)
print("10.0" == 10.0)
#Ordered comparisons between different datatype objects
print("Ordered comparisons between different datatype objects")
print(20 < 10.6)
#print("20" > 5)
#print(7.5 <= "100")
16. Comparison Operators - Part 2
Lecture VideoCode Snippet
#String comparisons
print("String comparisons")
print("abmn" > "abc")
print("abc" < "aBc")
print("abc" > "3amn")
print("10" > "5")
print("abc" > "abcd")
print("abc" == "abc")
print("abc" < "abc")
#Chained comparisons
print("Chained comparisons")
age = 20
if 25 <= age <=35: #equivalent to --> if age >=25 and age<=35:
print("You are eligible to apply for the course")
else:
print("You are not eligible")
17. Logical Operators
Lecture VideoCode Snippet
a = 10
b = 30
c = "PYTHON"
print(a == 10 or b < 20)
print(a < 0 or b < 0)
print(a > 0 and c == "Python")
print(b == 30 and c == "PYTHON")
print(not(a > b-10))
print(not(a == 10 or b == 10))
#Real time example of logical operators
name = "Sachin"
age = 30
country = "India"
if (age >= 18) and (country == "India"):
print("You are eligible to vote")
else:
print("You are not eligible to vote")
18. Operator Precedence - Part 1
Lecture VideoCode Snippet
print(3 + 4 * 5) # (3 + (4 * 5))
#Use parentheses explicitly to change the grouping
print((3 + 4) * 5)
print(3+4 * 5)
print(20 * 6 % 5) # ((20 * 6) % 5)
#examples part 1
print("Example 1")
print(2 + 3 + 4 * 5 % 6) # (2+3) + ((4 * 5) % 6)
print("Example 2")
print(8 % 3 * 2 * 4) # (8 % 3) * 2 * 4
print("Example 3")
print(2 ** 3 ** 2) # 2 ** (3 ** 2)
19. Operator Precedence - Part 2: Order of evaluation
Lecture VideoCode Snippet
#Evaluation order - Always from left to right after grouping
print("Evaluation order demo")
a,b,c,d = 1,2,3,4
print(a + b + c * d) # ((a+b) * (c*d))
def func(value):
print("Value is:", value)
return value
print(func(1))
print(func(10))
print(func(5))
print("Evaluation order demo with function calls")
print(func(1) + func(2) + func(3) * func(4))