KEMBAR78
Chapter 2 Decision Making (Python Programming Lecture) | PDF
PYTHON
PROGRAMMING
Chapter 2
Decision Making
Topic
• Decision Making (Conditional Statement)
• Operator
• Comparisons Operator
• Logical Operator
• Bitwise Operator
PYTHON
PROGRAMMING
Chapter 2
Lecture 2.1
Conditional Statement
Conditional Statement Syntax
if condition:
#statement
if condition:
#statement1
else:
#statement2
if condition:
#statement1
elif condition:
#statement2
else:
#statement3
PYTHON
PROGRAMMING
Chapter 2
Lecture 2.1.1
Comparison Operator in
Conditional Statement
Comparison Operator in Python
Operation Operator
Less Than <
Less Than or Equal <=
Greater Than >
Greater Than or Equal >=
Equality ==
Not Equal !=
Comparison Operator in Python
• 5>2 return True
• 3>5 return False
• 4>4 return False
• 5>=2 return True
• 3>=5 return False
• 4>=4 return True
• 3<5 return True
• 8<5 return False
• 4<4 return False
• 3<=5 return True
• 8<=5 return False
• 4<=4 return True
Comparison Operator in Python
• 5==5 return True
• 5==3 return False
• 4==6 return False
• 5!=5 return False
• 5!=3 return True
• 4!=6 return True
Find Pass/ Fail
mark = int(input())
if mark>=33:
print("Pass")
else:
print("Fail")
Find Voter or Not Voter
age = int(input())
if age>=18:
print("Voter")
else:
print("Not Voter")
Is a number Positive/ Negative/ Zero?
num = int(input())
if num>0:
print("Positive")
elif num<0:
print("Negative")
else:
print("Zero")
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")
Find Maximum between 2 number.
a = int(input())
b = int(input())
if a>b:
print(a)
else:
print(b)
Find Maximum between 2 number. (*)
a = int(input())
b = int(input())
maximum = max(a, b)
print("{} is maximum".format(maximum))
Find Minimum between 2 number.
a = int(input())
b = int(input())
if a<b:
print(a)
else:
print(b)
Check is a number EVEN or ODD.
num = int(input())
if num%2==0:
print("Even")
else:
print("Odd")
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.
Any Question?
Like, Comment, Share
Subscribe
PYTHON
PROGRAMMING
Chapter 2
Lecture 2.2
Logical Operator in
Conditional Statement
Logical Operator in Python
Operation Operator
Logical And and
Logical Or or
Logical Not not
Logical Operator in Python (NOT)
Value not
True False
False True
Logical Operator in Python (AND)
Value 1 Value 2 and
False False False
False True False
True False False
True True True
Logical Operator in Python (OR)
Value 1 Value 2 or
False False False
False True True
True False True
True True True
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)
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)
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")
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")
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.
Any Question?
Like, Comment, Share
Subscribe
PYTHON
PROGRAMMING
Chapter 1
Lecture 1.2
Arithmetic Operation Example
Bitwise Operator
• (~) complement
• (&) bitwise and
• (|) bitwise or
• (^) bitwise exclusive or (xor)
• (<<) left shift
• (>>) right shift
• (~) complement
Bitwise Operator in Python (AND)
Bit 1 Bit 2 AND (&)
0 0 0
0 1 0
1 0 0
1 1 1
Bitwise Operator in Python (OR)
Bit 1 Bit 2 OR (|)
False 00 0
0 1 1
1 0 1
1 1 1
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)
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
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
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
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
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
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
Any Question?
Like, Comment, Share
Subscribe
Chapter 2 Decision Making (Python Programming Lecture)

Chapter 2 Decision Making (Python Programming Lecture)

  • 2.
  • 3.
    Topic • Decision Making(Conditional Statement) • Operator • Comparisons Operator • Logical Operator • Bitwise Operator
  • 4.
  • 5.
    Conditional Statement Syntax ifcondition: #statement if condition: #statement1 else: #statement2 if condition: #statement1 elif condition: #statement2 else: #statement3
  • 6.
  • 7.
    Comparison Operator inPython Operation Operator Less Than < Less Than or Equal <= Greater Than > Greater Than or Equal >= Equality == Not Equal !=
  • 8.
    Comparison Operator inPython • 5>2 return True • 3>5 return False • 4>4 return False • 5>=2 return True • 3>=5 return False • 4>=4 return True • 3<5 return True • 8<5 return False • 4<4 return False • 3<=5 return True • 8<=5 return False • 4<=4 return True
  • 9.
    Comparison Operator inPython • 5==5 return True • 5==3 return False • 4==6 return False • 5!=5 return False • 5!=3 return True • 4!=6 return True
  • 10.
    Find Pass/ Fail mark= int(input()) if mark>=33: print("Pass") else: print("Fail")
  • 11.
    Find Voter orNot Voter age = int(input()) if age>=18: print("Voter") else: print("Not Voter")
  • 12.
    Is a numberPositive/ Negative/ Zero? num = int(input()) if num>0: print("Positive") elif num<0: print("Negative") else: print("Zero")
  • 13.
    Find Grade ofExam 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 between2 number. a = int(input()) b = int(input()) if a>b: print(a) else: print(b)
  • 15.
    Find Maximum between2 number. (*) a = int(input()) b = int(input()) maximum = max(a, b) print("{} is maximum".format(maximum))
  • 16.
    Find Minimum between2 number. a = int(input()) b = int(input()) if a<b: print(a) else: print(b)
  • 17.
    Check is anumber 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.
  • 19.
  • 21.
    PYTHON PROGRAMMING Chapter 2 Lecture 2.2 LogicalOperator in Conditional Statement
  • 22.
    Logical Operator inPython Operation Operator Logical And and Logical Or or Logical Not not
  • 23.
    Logical Operator inPython (NOT) Value not True False False True
  • 24.
    Logical Operator inPython (AND) Value 1 Value 2 and False False False False True False True False False True True True
  • 25.
    Logical Operator inPython (OR) Value 1 Value 2 or False False False False True True True False True True True True
  • 26.
    Find Maximum between3 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 between3 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 ayear 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 ayear 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.
  • 31.
  • 33.
  • 34.
    Bitwise Operator • (~)complement • (&) bitwise and • (|) bitwise or • (^) bitwise exclusive or (xor) • (<<) left shift • (>>) right shift • (~) complement
  • 35.
    Bitwise Operator inPython (AND) Bit 1 Bit 2 AND (&) 0 0 0 0 1 0 1 0 0 1 1 1
  • 36.
    Bitwise Operator inPython (OR) Bit 1 Bit 2 OR (|) False 00 0 0 1 1 1 0 1 1 1 1
  • 37.
    Check EVEN orODD 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 LeftShift 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 RightShift 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 Bitof 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 Bitof 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 Bitof 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
  • 44.