Python Programming Course Lecture by IoT Code Lab Training.
Discussed Topic:
Chapter 2: Decision Making
* Decision Making (Conditional Statement)
* Comparisons Operator Example
* Logical Operator Example
* Bitwise Operator Example
* Practice Problem 2.1
* Practice Problem 2.2
* Practice Problem 2.3
10. Find Pass/ Fail
mark = int(input())
if mark>=33:
print("Pass")
else:
print("Fail")
11. Find Voter or Not Voter
age = int(input())
if age>=18:
print("Voter")
else:
print("Not Voter")
12. Is a number Positive/ Negative/ Zero?
num = int(input())
if num>0:
print("Positive")
elif num<0:
print("Negative")
else:
print("Zero")
13. Find Grade of Exam
mark = int(input())
if mark>=80:
print("A+")
elif mark>=70:
print("A")
elif mark>=60:
print("B")
elif mark>=50:
print("C")
elif mark>=40:
print(“D")
elif mark>=33:
print("E")
else:
print("F")
14. Find Maximum between 2 number.
a = int(input())
b = int(input())
if a>b:
print(a)
else:
print(b)
15. Find Maximum between 2 number. (*)
a = int(input())
b = int(input())
maximum = max(a, b)
print("{} is maximum".format(maximum))
16. Find Minimum between 2 number.
a = int(input())
b = int(input())
if a<b:
print(a)
else:
print(b)
17. Check is a number EVEN or ODD.
num = int(input())
if num%2==0:
print("Even")
else:
print("Odd")
18. Practice Problem 2.1
1. Find Pass/ Fail.
2. Find Voter or Not Voter.
3. Is a number Positive/ Negative/ Zero?
4. Find Grade of Exam.
5. Find Maximum between 2 number.
6. Find Minimum between 2 number.
7. Check is a number EVEN or ODD.
24. Logical Operator in Python (AND)
Value 1 Value 2 and
False False False
False True False
True False False
True True True
25. Logical Operator in Python (OR)
Value 1 Value 2 or
False False False
False True True
True False True
True True True
26. Find Maximum between 3 number.
a = int(input())
b = int(input())
c = int(input())
if a>b and a>c:
print(a)
elif b>a and b>c:
print(b)
else:
print(c)
27. Find Minimum between 3 number.
a = int(input())
b = int(input())
c = int(input())
if a<b and a<c:
print(a)
elif b<a and b<c:
print(b)
else:
print(c)
28. Check is a year Leap Year or Not.
year = int(input())
if year%400==0:
print("Leap Year")
elif year%100==0:
print("Not Leap Year")
elif year%4==0:
print("Leap Year")
else:
print("Not Leap Year")
29. Check is a year Leap Year or Not. [2]
year = int(input())
if year%400==0 or year%100!=0 and year%4 == 0:
print("Leap Year")
else:
print("Not Leap Year")
30. Practice Problem 2.2
1. Find Maximum between 3 number.
2. Find Minimum between 3 number.
3. Check is a year Leap Year or Not.
4. Check is a year with Logical Operator in Single if statement.
34. Bitwise Operator
• (~) complement
• (&) bitwise and
• (|) bitwise or
• (^) bitwise exclusive or (xor)
• (<<) left shift
• (>>) right shift
• (~) complement
35. Bitwise Operator in Python (AND)
Bit 1 Bit 2 AND (&)
0 0 0
0 1 0
1 0 0
1 1 1
36. Bitwise Operator in Python (OR)
Bit 1 Bit 2 OR (|)
False 00 0
0 1 1
1 0 1
1 1 1
37. Check EVEN or ODD with Bitwise Ope[1].
num = int(input())
if num&1 == 0:
print("Even")
else:
print("Odd")
Bit Level Operation
30 & 1 = 00011110 & 00000001 = 00000000 = 0 (Even)
45 & 1 = 000101101 & 00000001 = 00000001 = 1 (Odd)
38. Use of Left Shift Operator
a = int(input())
b = int(input())
print(a<<b)
Left Shift Operation Example
1<<3 = 00000001<<3 = 00001000 = 8
2<<2 = 00000010<<3 = 00001000 = 8
3<<3 = 00000011<<3 = 00011000 = 24
13<<4 = 00001101<<3 = 11010000 = 208
39. Use of Right Shift Operator
a = int(input())
b = int(input())
print(a>>b)
Right Shift Operation Example
13>>2 = 00001101>>2 = 00000011 = 3
208>>2 = 11010000>>2 = 00110100 = 52
72>>3 = 01001000>>3 = 00001001 = 9
87>>4 = 01010111>>3 = 00000101 = 5
40. Test nth Bit of a number is Set or Not?
a = int(input())
n = int(input())
b = 1<<(n-1) #Set nth bit
if a&b==0: # Test nth bit
print("Bit is Not Set")
else:
print("Bit is Set")
Nth Bit Set or Not Operation Example 1
a = 13 = 00001101
n = 4
b = 1<<(n-1) = 1<<(4-1) = 1<<3
b = 000000001<<3 = 00001000
a & b = 00001101 & 00001000 = 00001000 = 8
Nth Bit Set or Not Operation Example 2
a = 13 = 00001101
n = 2
b = 1<<(n-1) = 1<<(2-1) = 1<<1
b = 000000001<<3 = 00000010
a & b = 00001101 & 00000010 = 00000000 = 0
41. Set nth Bit of a number
a = int(input())
n = int(input())
b = 1<<(n-1) #Set nth bit
c = a | b
print(c)
print(bin(c))
Set nth Bit of a number Example 1
a = 13 = 00001101
n = 4
b = 1<<(n-1) = 1<<(4-1) = 1<<3
b = 000000001<<3 = 00001000
a | b = 00001101 | 00001000 = 00001101 = 13
Set nth Bit of a number Example 2
a = 13 = 00001101
n = 2
b = 1<<(n-1) = 1<<(2-1) = 1<<1
b = 000000001<<3 = 00000010
a | b = 00001101 | 00000010 = 00001111 = 15
42. Unset nth Bit of a number
a = int(input())
n = int(input())
s = 1<<n-1
c = a & ~s
print(f"{a:08b}")
print(f"{c:08b}")
Set nth Bit of a number Example 1
a = 13 = 00001101
n = 4
s = 1<<n-1 = 00000001<<3 = 00001000
~s = ~00001000 = 11110111
c = a & ~s 00001101 & 11110111 = 00000101
43. Practice Problem 2.3
1. Check is a number EVEN or ODD using Bitwise Operator.
2. Test nth Bit of a number is Set or Not?
3. Set nth Bit of a number
4. Unset nth Bit of a number