Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Chapter 2 Decision Making (Python Programming Lecture)

233 Aufrufe

Veröffentlicht am

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

Veröffentlicht in: Bildung
  • Loggen Sie sich ein, um Kommentare anzuzeigen.

Chapter 2 Decision Making (Python Programming Lecture)

  1. 1. PYTHON PROGRAMMING Chapter 2 Decision Making
  2. 2. Topic • Decision Making (Conditional Statement) • Operator • Comparisons Operator • Logical Operator • Bitwise Operator
  3. 3. PYTHON PROGRAMMING Chapter 2 Lecture 2.1 Conditional Statement
  4. 4. Conditional Statement Syntax if condition: #statement if condition: #statement1 else: #statement2 if condition: #statement1 elif condition: #statement2 else: #statement3
  5. 5. PYTHON PROGRAMMING Chapter 2 Lecture 2.1.1 Comparison Operator in Conditional Statement
  6. 6. Comparison Operator in Python Operation Operator Less Than < Less Than or Equal <= Greater Than > Greater Than or Equal >= Equality == Not Equal !=
  7. 7. 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
  8. 8. 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
  9. 9. Find Pass/ Fail mark = int(input()) if mark>=33: print("Pass") else: print("Fail")
  10. 10. Find Voter or Not Voter age = int(input()) if age>=18: print("Voter") else: print("Not Voter")
  11. 11. Is a number Positive/ Negative/ Zero? num = int(input()) if num>0: print("Positive") elif num<0: print("Negative") else: print("Zero")
  12. 12. 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")
  13. 13. Find Maximum between 2 number. a = int(input()) b = int(input()) if a>b: print(a) else: print(b)
  14. 14. Find Maximum between 2 number. (*) a = int(input()) b = int(input()) maximum = max(a, b) print("{} is maximum".format(maximum))
  15. 15. Find Minimum between 2 number. a = int(input()) b = int(input()) if a<b: print(a) else: print(b)
  16. 16. Check is a number EVEN or ODD. num = int(input()) if num%2==0: print("Even") else: print("Odd")
  17. 17. 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.
  18. 18. Any Question? Like, Comment, Share Subscribe
  19. 19. PYTHON PROGRAMMING Chapter 2 Lecture 2.2 Logical Operator in Conditional Statement
  20. 20. Logical Operator in Python Operation Operator Logical And and Logical Or or Logical Not not
  21. 21. Logical Operator in Python (NOT) Value not True False False True
  22. 22. Logical Operator in Python (AND) Value 1 Value 2 and False False False False True False True False False True True True
  23. 23. Logical Operator in Python (OR) Value 1 Value 2 or False False False False True True True False True True True True
  24. 24. 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)
  25. 25. 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)
  26. 26. 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")
  27. 27. 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")
  28. 28. 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.
  29. 29. Any Question? Like, Comment, Share Subscribe
  30. 30. PYTHON PROGRAMMING Chapter 1 Lecture 1.2 Arithmetic Operation Example
  31. 31. Bitwise Operator • (~) complement • (&) bitwise and • (|) bitwise or • (^) bitwise exclusive or (xor) • (<<) left shift • (>>) right shift • (~) complement
  32. 32. Bitwise Operator in Python (AND) Bit 1 Bit 2 AND (&) 0 0 0 0 1 0 1 0 0 1 1 1
  33. 33. Bitwise Operator in Python (OR) Bit 1 Bit 2 OR (|) False 00 0 0 1 1 1 0 1 1 1 1
  34. 34. 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)
  35. 35. 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
  36. 36. 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
  37. 37. 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
  38. 38. 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
  39. 39. 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
  40. 40. 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
  41. 41. Any Question? Like, Comment, Share Subscribe

×