SlideShare a Scribd company logo
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

Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
Ranel Padon
 
Python basic
Python basicPython basic
Python basic
Saifuddin Kaijar
 
Python programming
Python  programmingPython  programming
Python programming
Ashwin Kumar Ramasamy
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
Dipankar Achinta
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
Pooja B S
 
What is Python Lambda Function? Python Tutorial | Edureka
What is Python Lambda Function? Python Tutorial | EdurekaWhat is Python Lambda Function? Python Tutorial | Edureka
What is Python Lambda Function? Python Tutorial | Edureka
Edureka!
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
VedaGayathri1
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
Python by Rj
Python by RjPython by Rj
Python If Else | If Else Statement In Python | Edureka
Python If Else | If Else Statement In Python | EdurekaPython If Else | If Else Statement In Python | Edureka
Python If Else | If Else Statement In Python | Edureka
Edureka!
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
Ayshwarya Baburam
 
Python | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python TutorialPython | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python Tutorial
QA TrainingHub
 
Python ppt
Python pptPython ppt
Python ppt
Mohita Pandey
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 
Python
PythonPython
Python
Aashish Jain
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAChapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Maulik Borsaniya
 
Python functions
Python functionsPython functions
Python functions
Prof. Dr. K. Adisesha
 
Function in Python
Function in PythonFunction in Python
Function in Python
Yashdev Hada
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
Praveen M Jigajinni
 

What's hot (20)

Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
 
Python basic
Python basicPython basic
Python basic
 
Python programming
Python  programmingPython  programming
Python programming
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
What is Python Lambda Function? Python Tutorial | Edureka
What is Python Lambda Function? Python Tutorial | EdurekaWhat is Python Lambda Function? Python Tutorial | Edureka
What is Python Lambda Function? Python Tutorial | Edureka
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Python by Rj
Python by RjPython by Rj
Python by Rj
 
Python If Else | If Else Statement In Python | Edureka
Python If Else | If Else Statement In Python | EdurekaPython If Else | If Else Statement In Python | Edureka
Python If Else | If Else Statement In Python | Edureka
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python TutorialPython | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python Tutorial
 
Python ppt
Python pptPython ppt
Python ppt
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Python
PythonPython
Python
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAChapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
 
Python functions
Python functionsPython functions
Python functions
 
Function in Python
Function in PythonFunction in Python
Function in Python
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 

Similar to Boolean and conditional logic in Python

Python Keywords
Python KeywordsPython Keywords
Python Keywords
khanmusfera32
 
Lecture 07.pptx
Lecture 07.pptxLecture 07.pptx
Lecture 07.pptx
Mohammad Hassan
 
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.pptx
EloAOgardo
 
L6 - Loops.pptx
L6 - Loops.pptxL6 - Loops.pptx
L6 - Loops.pptx
EloAOgardo
 
Core python programming tutorial
Core python programming tutorialCore python programming tutorial
Core python programming tutorial
Amarjeetsingh Thakur
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
IruolagbePius
 
Python Functions 1
Python Functions 1Python Functions 1
Python Functions 1
gsdhindsa
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An Introduction
Swarit Wadhe
 
L5 - Data Types, Keywords.pptx
L5 - Data Types, Keywords.pptxL5 - Data Types, Keywords.pptx
L5 - Data Types, Keywords.pptx
EloAOgardo
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | Edureka
Edureka!
 
Welcome to python workshop
Welcome to python workshopWelcome to python workshop
Welcome to python workshop
Mukul Kirti Verma
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python Programming
Dozie Agbo
 
Python breakdown-workbook
Python breakdown-workbookPython breakdown-workbook
Python breakdown-workbook
HARUN PEHLIVAN
 
C pythontalk
C pythontalkC pythontalk
C pythontalk
Nicholaus Jackson
 
Introduction To Python.pptx
Introduction To Python.pptxIntroduction To Python.pptx
Introduction To Python.pptx
Anum Zehra
 
Revision Lecture
Revision LectureRevision Lecture
Revision Lecture
emailharmeet
 
Python For Machine Learning
Python For Machine LearningPython For Machine Learning
Python For Machine Learning
YounesCharfaoui
 
PRESENTATION ON PYTHON.pptx
PRESENTATION ON PYTHON.pptxPRESENTATION ON PYTHON.pptx
PRESENTATION ON PYTHON.pptx
abhishek364864
 
Python Fundamentals Class 11
Python Fundamentals Class 11Python Fundamentals Class 11
Python Fundamentals Class 11
chinthala Vijaya Kumar
 

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

WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Jeffrey Haguewood
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStrDeep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
saastr
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
alexjohnson7307
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
flufftailshop
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
Pravash Chandra Das
 

Recently uploaded (20)

WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStrDeep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.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