SlideShare a Scribd company logo
1 of 9
P Y T H O N M A S T E R C L A S S B Y P Y T H O N W H I Z
Boolean & Conditional Logic
Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com
P Y T H O N M A S T E R C L A S S
Boolean &
Conditional
Logic
 How to get user input in python
 Learn about "Truthiness"
 Learn how to use comparison
operators to make a basic program
Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com
Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com
Section: Boolean & Conditional Logic
Topic: User Input
There is a built-in function in Python called "input" that will
prompt the user and store the result to a variable.
name = input("Enter your name here: ")
Enter your name here: Gurpreet Dhindsa
print(name)
Gurpreet Dhindsa
Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com
Section: Boolean Expressions
Topic: Conditional Expressions
Conditional logic using if statements represents different paths a program can
take based on some type of comparison of input.
if some condition is true:
do something
elif some other condition is true:
do something
else:
do something
Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com
Section: Boolean Expressions
Topic: Conditional Checks
Conditional logic using if statements represents different paths a program can
take based on some type of comparison of input.
if name == ”Donald Trump":
print( ”US President” )
elif name == ”Narendar Modi":
print( “Trump’s Friend” )
else:
print( ”Rest of the world!” )
name
US President
Trump’s Friend
Rest of
the world!
“NarendarModi”
Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com
Section: Boolean Expressions
Topic: Truthiness
 In python, all conditional checks resolve to True or False
x = 1
x is 1 # True
x is 0 # False
 We can call values that will resolve to True "truthy", or values that will resolve
to False "falsy".
 Besides False conditional checks, other things that are naturally falsy include:
empty objects, empty strings, None, and zero.
Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com
Section: Boolean Expressions
Topic: Comparison Operators
Here is a list of Python Comparison Operators.
In the examples, a = 1 and b = 1
Op What it does Example
== Truthy if a has the same value as b a == b # True
!= Truthy if a does NOT have the same value as b a != b # False
>
<
Truthy if a is greater than b
Truthy if a is less than be b
a > b # False
a < b # False
>=
<=
Truthy if a is greater than or equal to b
Truthy if a is less than or equal to b
a >= b # True
a <= b # True
Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com
Section: Boolean Expressions
Topic: Logical Operators
In Python, the following operators can be used to make
Boolean Logic comparisons or statements:
Op What it does Example
and Truthy if both a AND b are true
(logical conjunction)
if a and b:
print(c)
or Truthy if either a OR b are true
(logical disjunction)
if am_tired or is_bedtime:
print("go to sleep")
not Truthy if the opposite of a is true
(logical negation)
if not is_weekend:
print("go to work")
Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com
Section: Boolean & Conditional Logic
Topic: is vs “==”
In python, "==" and "is" are very similar comparators,
however they are not the same.
a = 1
a == 1 # True
a is 1 # True
a = [1, 2, 3] # a list of numbers
b = [1, 2, 3]
a == b # True
a is b # False
c = b
b is c # True
"is" is only truthy if the variables reference the same item in memory

More Related Content

What's hot (20)

What is Range Function? | Range in Python Explained | Edureka
What is Range Function? | Range in Python Explained | EdurekaWhat is Range Function? | Range in Python Explained | Edureka
What is Range Function? | Range in Python Explained | Edureka
 
PYTHON NOTES
PYTHON NOTESPYTHON NOTES
PYTHON NOTES
 
Python
PythonPython
Python
 
Sets
SetsSets
Sets
 
Python
PythonPython
Python
 
Python recursion
Python recursionPython recursion
Python recursion
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
 
programming with python ppt
programming with python pptprogramming with python ppt
programming with python ppt
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Python - Lecture 11
Python - Lecture 11Python - Lecture 11
Python - Lecture 11
 
Python basics
Python basicsPython basics
Python basics
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Begin with Python
Begin with PythonBegin with Python
Begin with Python
 
python conditional statement.pptx
python conditional statement.pptxpython conditional statement.pptx
python conditional statement.pptx
 
Python : Regular expressions
Python : Regular expressionsPython : Regular expressions
Python : Regular expressions
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 

Similar to Boolean and conditional logic in Python

Introduction to python programming ( part-1)
Introduction to python programming  ( part-1)Introduction to python programming  ( part-1)
Introduction to python programming ( part-1)Ziyauddin Shaik
 
L6 - Loops.pptx
L6 - Loops.pptxL6 - Loops.pptx
L6 - Loops.pptxEloAOgardo
 
L6 - Loops.pptx
L6 - Loops.pptxL6 - Loops.pptx
L6 - Loops.pptxEloAOgardo
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizeIruolagbePius
 
Python Functions 1
Python Functions 1Python Functions 1
Python Functions 1gsdhindsa
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An IntroductionSwarit Wadhe
 
L5 - Data Types, Keywords.pptx
L5 - Data Types, Keywords.pptxL5 - Data Types, Keywords.pptx
L5 - Data Types, Keywords.pptxEloAOgardo
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaEdureka!
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python ProgrammingDozie Agbo
 
Python breakdown-workbook
Python breakdown-workbookPython breakdown-workbook
Python breakdown-workbookHARUN PEHLIVAN
 
Introduction To Python.pptx
Introduction To Python.pptxIntroduction To Python.pptx
Introduction To Python.pptxAnum Zehra
 
Python For Machine Learning
Python For Machine LearningPython For Machine Learning
Python For Machine LearningYounesCharfaoui
 
PRESENTATION ON PYTHON.pptx
PRESENTATION ON PYTHON.pptxPRESENTATION ON PYTHON.pptx
PRESENTATION ON PYTHON.pptxabhishek364864
 

Similar to Boolean and conditional logic in Python (20)

Python Keywords
Python KeywordsPython Keywords
Python Keywords
 
Lecture 07.pptx
Lecture 07.pptxLecture 07.pptx
Lecture 07.pptx
 
Introduction to python programming ( part-1)
Introduction to python programming  ( part-1)Introduction to python programming  ( part-1)
Introduction to python programming ( part-1)
 
L6 - Loops.pptx
L6 - Loops.pptxL6 - Loops.pptx
L6 - Loops.pptx
 
L6 - Loops.pptx
L6 - Loops.pptxL6 - Loops.pptx
L6 - Loops.pptx
 
Core python programming tutorial
Core python programming tutorialCore python programming tutorial
Core python programming tutorial
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
 
Python Functions 1
Python Functions 1Python Functions 1
Python Functions 1
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An Introduction
 
L5 - Data Types, Keywords.pptx
L5 - Data Types, Keywords.pptxL5 - Data Types, Keywords.pptx
L5 - Data Types, Keywords.pptx
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | Edureka
 
Welcome to python workshop
Welcome to python workshopWelcome to python workshop
Welcome to python workshop
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python Programming
 
Python breakdown-workbook
Python breakdown-workbookPython breakdown-workbook
Python breakdown-workbook
 
C pythontalk
C pythontalkC pythontalk
C pythontalk
 
Introduction To Python.pptx
Introduction To Python.pptxIntroduction To Python.pptx
Introduction To Python.pptx
 
Revision Lecture
Revision LectureRevision Lecture
Revision Lecture
 
Python For Machine Learning
Python For Machine LearningPython For Machine Learning
Python For Machine Learning
 
PRESENTATION ON PYTHON.pptx
PRESENTATION ON PYTHON.pptxPRESENTATION ON PYTHON.pptx
PRESENTATION ON PYTHON.pptx
 
Python Fundamentals Class 11
Python Fundamentals Class 11Python Fundamentals Class 11
Python Fundamentals Class 11
 

Recently uploaded

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 

Recently uploaded (20)

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 

Boolean and conditional logic in Python

  • 1. P Y T H O N M A S T E R C L A S S B Y P Y T H O N W H I Z Boolean & Conditional Logic Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com
  • 2. P Y T H O N M A S T E R C L A S S Boolean & Conditional Logic  How to get user input in python  Learn about "Truthiness"  Learn how to use comparison operators to make a basic program Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com
  • 3. Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com Section: Boolean & Conditional Logic Topic: User Input There is a built-in function in Python called "input" that will prompt the user and store the result to a variable. name = input("Enter your name here: ") Enter your name here: Gurpreet Dhindsa print(name) Gurpreet Dhindsa
  • 4. Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com Section: Boolean Expressions Topic: Conditional Expressions Conditional logic using if statements represents different paths a program can take based on some type of comparison of input. if some condition is true: do something elif some other condition is true: do something else: do something
  • 5. Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com Section: Boolean Expressions Topic: Conditional Checks Conditional logic using if statements represents different paths a program can take based on some type of comparison of input. if name == ”Donald Trump": print( ”US President” ) elif name == ”Narendar Modi": print( “Trump’s Friend” ) else: print( ”Rest of the world!” ) name US President Trump’s Friend Rest of the world! “NarendarModi”
  • 6. Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com Section: Boolean Expressions Topic: Truthiness  In python, all conditional checks resolve to True or False x = 1 x is 1 # True x is 0 # False  We can call values that will resolve to True "truthy", or values that will resolve to False "falsy".  Besides False conditional checks, other things that are naturally falsy include: empty objects, empty strings, None, and zero.
  • 7. Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com Section: Boolean Expressions Topic: Comparison Operators Here is a list of Python Comparison Operators. In the examples, a = 1 and b = 1 Op What it does Example == Truthy if a has the same value as b a == b # True != Truthy if a does NOT have the same value as b a != b # False > < Truthy if a is greater than b Truthy if a is less than be b a > b # False a < b # False >= <= Truthy if a is greater than or equal to b Truthy if a is less than or equal to b a >= b # True a <= b # True
  • 8. Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com Section: Boolean Expressions Topic: Logical Operators In Python, the following operators can be used to make Boolean Logic comparisons or statements: Op What it does Example and Truthy if both a AND b are true (logical conjunction) if a and b: print(c) or Truthy if either a OR b are true (logical disjunction) if am_tired or is_bedtime: print("go to sleep") not Truthy if the opposite of a is true (logical negation) if not is_weekend: print("go to work")
  • 9. Course: Python Masterclass © PythonWhiz 2020 pythonwhiz.com Section: Boolean & Conditional Logic Topic: is vs “==” In python, "==" and "is" are very similar comparators, however they are not the same. a = 1 a == 1 # True a is 1 # True a = [1, 2, 3] # a list of numbers b = [1, 2, 3] a == b # True a is b # False c = b b is c # True "is" is only truthy if the variables reference the same item in memory