SlideShare ist ein Scribd-Unternehmen logo
1 von 45
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)

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Python
PythonPython
Python
 
Introduction to Algorithm
Introduction to AlgorithmIntroduction to Algorithm
Introduction to Algorithm
 
Introduction to Python IDLE | IDLE Tutorial | Edureka
Introduction to Python IDLE | IDLE Tutorial | EdurekaIntroduction to Python IDLE | IDLE Tutorial | Edureka
Introduction to Python IDLE | IDLE Tutorial | Edureka
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An Introduction
 
Numeric Data types in Python
Numeric Data types in PythonNumeric Data types in Python
Numeric Data types in Python
 
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
 
Python programming
Python  programmingPython  programming
Python programming
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
 
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...
 
Flask – Python
Flask – PythonFlask – Python
Flask – Python
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshop
 
Compiler Optimization Presentation
Compiler Optimization PresentationCompiler Optimization Presentation
Compiler Optimization Presentation
 
Modules in Python Programming
Modules in Python ProgrammingModules in Python Programming
Modules in Python Programming
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 

Ähnlich wie Chapter 2 Decision Making (Python Programming Lecture)

Operators expressions-and-statements
Operators expressions-and-statementsOperators expressions-and-statements
Operators expressions-and-statements
CtOlaf
 
Dti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpressionDti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpression
alish sha
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01
Abdul Samee
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01
Abdul Samee
 

Ähnlich wie Chapter 2 Decision Making (Python Programming Lecture) (20)

Python : basic operators
Python : basic operatorsPython : basic operators
Python : basic operators
 
Python PCEP Logic Bit Operations
Python PCEP Logic Bit OperationsPython PCEP Logic Bit Operations
Python PCEP Logic Bit Operations
 
Operators expressions-and-statements
Operators expressions-and-statementsOperators expressions-and-statements
Operators expressions-and-statements
 
Dti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpressionDti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpression
 
Java - Operators
Java - OperatorsJava - Operators
Java - Operators
 
Java 2
Java 2Java 2
Java 2
 
03. Operators Expressions and statements
03. Operators Expressions and statements03. Operators Expressions and statements
03. Operators Expressions and statements
 
3306617
33066173306617
3306617
 
Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
 
Python tutorials for beginners | IQ Online Training
Python tutorials for beginners | IQ Online TrainingPython tutorials for beginners | IQ Online Training
Python tutorials for beginners | IQ Online Training
 
Session 4.pptx
Session 4.pptxSession 4.pptx
Session 4.pptx
 
03. operators and-expressions
03. operators and-expressions03. operators and-expressions
03. operators and-expressions
 
Python operators
Python operatorsPython operators
Python operators
 
15 bitwise operators
15 bitwise operators15 bitwise operators
15 bitwise operators
 
1_Python Basics.pdf
1_Python Basics.pdf1_Python Basics.pdf
1_Python Basics.pdf
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01
 
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp013 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
Introduction To Python.pptx
Introduction To Python.pptxIntroduction To Python.pptx
Introduction To Python.pptx
 

Mehr von IoT Code Lab (8)

7.1 html lec 7
7.1 html lec 77.1 html lec 7
7.1 html lec 7
 
6.1 html lec 6
6.1 html lec 66.1 html lec 6
6.1 html lec 6
 
5.1 html lec 5
5.1 html lec 55.1 html lec 5
5.1 html lec 5
 
4.1 html lec 4
4.1 html lec 44.1 html lec 4
4.1 html lec 4
 
3.1 html lec 3
3.1 html lec 33.1 html lec 3
3.1 html lec 3
 
2.1 html lec 2
2.1 html lec 22.1 html lec 2
2.1 html lec 2
 
1.1 html lec 1
1.1 html lec 11.1 html lec 1
1.1 html lec 1
 
1.0 intro
1.0 intro1.0 intro
1.0 intro
 

Kürzlich hochgeladen

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Kürzlich hochgeladen (20)

psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

Chapter 2 Decision Making (Python Programming Lecture)

  • 1.
  • 3. Topic • Decision Making (Conditional Statement) • Operator • Comparisons Operator • Logical Operator • Bitwise Operator
  • 5. Conditional Statement Syntax if condition: #statement if condition: #statement1 else: #statement2 if condition: #statement1 elif condition: #statement2 else: #statement3
  • 6. PYTHON PROGRAMMING Chapter 2 Lecture 2.1.1 Comparison Operator in Conditional Statement
  • 7. Comparison Operator in Python Operation Operator Less Than < Less Than or Equal <= Greater Than > Greater Than or Equal >= Equality == Not Equal !=
  • 8. 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
  • 9. 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
  • 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.
  • 19. Any Question? Like, Comment, Share Subscribe
  • 20.
  • 21. PYTHON PROGRAMMING Chapter 2 Lecture 2.2 Logical Operator in Conditional Statement
  • 22. Logical Operator in Python Operation Operator Logical And and Logical Or or Logical Not not
  • 23. Logical Operator in Python (NOT) Value not True False False True
  • 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.
  • 31. Any Question? Like, Comment, Share Subscribe
  • 32.
  • 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
  • 44. Any Question? Like, Comment, Share Subscribe