SlideShare a Scribd company logo
Module 2
Data Collections – Lists, Tuples,
and Dictionaries
Comparison operators and
conditional execution
Module Objective
In this module, you will cover the
following topics:
• the Boolean data type;
• relational operators;
• making decisions in Python (if, if-else, if-elif,else)
• how to repeat code execution using loops (while, for)
• how to perform logic and bitwise operations in Python;
• lists in Python (constructing, indexing, and slicing; content
manipulation)
• how to sort a list using bubble-sort algorithms;
• multidimensional lists and their applications.
Module 2 Comparison operators
&Conditional execution
Questions
Yes, this is
true
No, this is
false
Probably yes
Let me think
Comparison: equality operator
Equality ==
• 2 == 2 True
• 2 == 2. True
• 1 ==2 False
Inequality !=
•
•
var = 0
Print(var != 0)
var = 1
Print(var != 0)
Module 2 Comparison operators
&Conditional execution
Priority Operator
1 +, 2 unary
2 **
3 *, /. //, %
4 +, - binary
5 <, <=, >, >=
6 ==, !=
Comparison operators
greater than >
• black_sheep > white_sheep
greater than or equal to >=
• centigrade_outside >= 0.0
less than <
• current_velocity_mph < 85
less than or equal to <=
• current_velocity_mph <= 85
Module 2 Comparison operators
&Conditional execution
Conditions & Conditional execution
if sheep_counter >= 120:
make_a_bed()
take_a_shower()
sleep_and_dream()
feed_the_sheepdogs()
If the weather is good, we'll go
for a walk then, we'll have lunch.
if the_weather_is_good:​
go_for_a_walk()​
have_lunch()​
Conditionally executed
statements have to be indented
Module 2 Comparison operators
&Conditional execution
if-else statement
if true_or_false_condition:
perform_if_condition_true
else:
perform_if_condition_false
if the_weather_is_good:
go_for_a_walk()
else:
go_to_a_theater()
have_lunch()
if the_weather_is_good:
if nice_restaurant_is_found:
have_lunch()
else:
eat_a_sandwich()
else:
if tickets_are_available:
go_to_the_theater()
else:
go_shopping()
Module 2 Comparison operators
&Conditional execution
elif statement
if the_weather_is_good:
go_for_a_walk()
elif tickets_are_available:
go_to_the_theater()
elif table_is_available:
go_for_lunch()
else:
play_chess_at_home()
If the weather is fine,
we'll go for a walk.
otherwise if we get
tickets, we'll go to
the theater
otherwise if there
are free tables at
the restaurant, we'll
go for lunch
if all else fails, we'll
return home and
play chess.
Module 2 Comparison operators
&Conditional execution
Analyzing code samples 1
# Read two numbers
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
# Choose the larger number
if number1 > number2:
larger_number = number1
else:
larger_number = number2
# Print the result
print("The larger number is:", larger_number)
# Read two numbers
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
# Choose the larger number
if number1 > number2: larger_number = number1
else: larger_number = number2
# Print the result
print("The larger number is:", larger_number)
Module 2 Comparison operators
&Conditional execution
# Read three numbers
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
number3 = int(input("Enter the third number: "))
# We temporarily assume that the first number is the largest one.
# We will verify this soon.
largest_number = number1
# We check if the second number is larger than current largest_number
# and update largest_number if needed.
if number2 > largest_number:
largest_number = number2
# We check if the third number is larger than current largest_number
# and update largest_number if needed.
if number3 > largest_number:
largest_number = number3
# Print the result
print("The largest number is:", largest_number)
Analyzing code samples 2
Module 2 Comparison operators
&Conditional execution
Pseudocode & introduction
# Read three numbers.
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
number3 = int(input("Enter the third number: "))
# Check which one of the numbers is the greatest
# and pass it to the largest_number variable.
largest_number = max(number1, number2, number3)
# Print the result.
print("The largest number is:", largest_number)
pseudocode
Module 2 Comparison operators
&Conditional execution
Key takeaways
•Comparison operators: ==, !=, >, >=, <, <=​
•Conditional statement:​
•if
•if-else
•if-elif-else
•Nested
Module 2 Comparison operators
&Conditional execution
LAB Practice
10. Questions and answers
11. Comparison operators and conditional
execution
12. Essentials of if-else statement
13. Essentials of if-else statement (1)
Ad

Recommended

PDF
Acm aleppo cpc training sixth session
Ahmad Bashar Eter
 
PDF
e computer notes - Reference variables
ecomputernotes
 
PPT
L10 sorting-searching
mondalakash2012
 
PPT
Python lab basics
Abi_Kasi
 
PDF
Python Objects
MuhammadBakri13
 
PDF
Python set
Mohammed Sikander
 
PDF
Function
MuhammadBakri13
 
PDF
Conditional Statements
MuhammadBakri13
 
PDF
computer notes - Linked list
ecomputernotes
 
PDF
Looping
MuhammadBakri13
 
PPTX
Basic Sorting algorithms csharp
Micheal Ogundero
 
PPTX
Introduction To Programming with Python-4
Syed Farjad Zia Zaidi
 
PPTX
Exam 1 Review CS 1803
Will Barr
 
PDF
computer notes - Linked list inside computer memory
ecomputernotes
 
PDF
Computer science solution - programming - big c plus plus
Praveen Tyagi
 
PPTX
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
PPTX
Recursive decomposition
hanis salwan
 
PPTX
Python for Beginners(v3)
Panimalar Engineering College
 
PPTX
8 elementary sorts-selection
irdginfo
 
PDF
Session2
daviessegera
 
PPTX
Data structure lecture 1
Samsil Arefin
 
PPTX
Selection sorting
Himanshu Kesharwani
 
PDF
Binary search algorithm
maamir farooq
 
PPTX
7 searching injava-binary
irdginfo
 
PPTX
Membrane computing
Ayesh Shaminda
 
PDF
Python list
Mohammed Sikander
 
KEY
Programming with Python - Week 3
Ahmet Bulut
 
PDF
List,tuple,dictionary
nitamhaske
 
PDF
23CSC101T PSPP python programming - UNIT 3.pdf
RajeshThanikachalam
 
PDF
23CSC101T PSPP python program - UNIT 3.pdf
RajeshThanikachalam
 

More Related Content

What's hot (20)

PDF
computer notes - Linked list
ecomputernotes
 
PDF
Looping
MuhammadBakri13
 
PPTX
Basic Sorting algorithms csharp
Micheal Ogundero
 
PPTX
Introduction To Programming with Python-4
Syed Farjad Zia Zaidi
 
PPTX
Exam 1 Review CS 1803
Will Barr
 
PDF
computer notes - Linked list inside computer memory
ecomputernotes
 
PDF
Computer science solution - programming - big c plus plus
Praveen Tyagi
 
PPTX
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
PPTX
Recursive decomposition
hanis salwan
 
PPTX
Python for Beginners(v3)
Panimalar Engineering College
 
PPTX
8 elementary sorts-selection
irdginfo
 
PDF
Session2
daviessegera
 
PPTX
Data structure lecture 1
Samsil Arefin
 
PPTX
Selection sorting
Himanshu Kesharwani
 
PDF
Binary search algorithm
maamir farooq
 
PPTX
7 searching injava-binary
irdginfo
 
PPTX
Membrane computing
Ayesh Shaminda
 
PDF
Python list
Mohammed Sikander
 
KEY
Programming with Python - Week 3
Ahmet Bulut
 
PDF
List,tuple,dictionary
nitamhaske
 
computer notes - Linked list
ecomputernotes
 
Basic Sorting algorithms csharp
Micheal Ogundero
 
Introduction To Programming with Python-4
Syed Farjad Zia Zaidi
 
Exam 1 Review CS 1803
Will Barr
 
computer notes - Linked list inside computer memory
ecomputernotes
 
Computer science solution - programming - big c plus plus
Praveen Tyagi
 
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
Recursive decomposition
hanis salwan
 
Python for Beginners(v3)
Panimalar Engineering College
 
8 elementary sorts-selection
irdginfo
 
Session2
daviessegera
 
Data structure lecture 1
Samsil Arefin
 
Selection sorting
Himanshu Kesharwani
 
Binary search algorithm
maamir farooq
 
7 searching injava-binary
irdginfo
 
Membrane computing
Ayesh Shaminda
 
Python list
Mohammed Sikander
 
Programming with Python - Week 3
Ahmet Bulut
 
List,tuple,dictionary
nitamhaske
 

Similar to Python PCEP Comparison Operators And Conditional Execution (20)

PDF
23CSC101T PSPP python programming - UNIT 3.pdf
RajeshThanikachalam
 
PDF
23CSC101T PSPP python program - UNIT 3.pdf
RajeshThanikachalam
 
PDF
Control of flow of the phyton programming
TriViccaWibisono
 
PDF
ESIT135 Problem Solving Using Python Notes of Unit-1 and Unit-2
prasadmutkule1
 
PPT
Decision Structures and Boolean Logic
Munazza-Mah-Jabeen
 
PPTX
Python-Certification-Training-Day-1-2.pptx
muzammildev46gmailco
 
PDF
Python - Lecture 2
Ravi Kiran Khareedi
 
PPT
introduction to python in english presentation file
RujanTimsina1
 
PPTX
Mastering python lesson2
Ruth Marvin
 
PPTX
Brixton Library Technology Initiative Week1 Recap
Basil Bibi
 
PPTX
Python.pptx
AKANSHAMITTAL2K21AFI
 
PPTX
Python Programming | JNTUA | UNIT 2 | Conditionals and Recursion |
FabMinds
 
PPT
PythonCourse_03_Conditionals.ppt Python introduction turorial for beginner.
sakchaisengsui
 
PPTX
Lecture 2 python conditional (ewurc)
Al-Mamun Riyadh (Mun)
 
PPTX
Pythonlearn-03-Conditional.pptx
VigneshChaturvedi1
 
PPTX
Introduction To Programming with Python Lecture 2
Syed Farjad Zia Zaidi
 
PDF
Decision and Control Statements.pdf
SrishtiChakraborty5
 
PDF
Python Basics | Python Tutorial | Edureka
Edureka!
 
PPTX
PYTHON BASICS CODING LANGUAGE GO TO.pptx
MohammedShoaib663271
 
23CSC101T PSPP python programming - UNIT 3.pdf
RajeshThanikachalam
 
23CSC101T PSPP python program - UNIT 3.pdf
RajeshThanikachalam
 
Control of flow of the phyton programming
TriViccaWibisono
 
ESIT135 Problem Solving Using Python Notes of Unit-1 and Unit-2
prasadmutkule1
 
Decision Structures and Boolean Logic
Munazza-Mah-Jabeen
 
Python-Certification-Training-Day-1-2.pptx
muzammildev46gmailco
 
Python - Lecture 2
Ravi Kiran Khareedi
 
introduction to python in english presentation file
RujanTimsina1
 
Mastering python lesson2
Ruth Marvin
 
Brixton Library Technology Initiative Week1 Recap
Basil Bibi
 
Python Programming | JNTUA | UNIT 2 | Conditionals and Recursion |
FabMinds
 
PythonCourse_03_Conditionals.ppt Python introduction turorial for beginner.
sakchaisengsui
 
Lecture 2 python conditional (ewurc)
Al-Mamun Riyadh (Mun)
 
Pythonlearn-03-Conditional.pptx
VigneshChaturvedi1
 
Introduction To Programming with Python Lecture 2
Syed Farjad Zia Zaidi
 
Decision and Control Statements.pdf
SrishtiChakraborty5
 
Python Basics | Python Tutorial | Edureka
Edureka!
 
PYTHON BASICS CODING LANGUAGE GO TO.pptx
MohammedShoaib663271
 
Ad

More from IHTMINSTITUTE (19)

PPTX
Python PCEP Tuples and Dictionaries
IHTMINSTITUTE
 
PPTX
Python PCEP Tuples and Dictionaries
IHTMINSTITUTE
 
PPTX
Python PCEP Creating Simple Functions
IHTMINSTITUTE
 
PPTX
Python PCEP Functions And Scopes
IHTMINSTITUTE
 
PPTX
Python PCEP Function Parameters
IHTMINSTITUTE
 
PPTX
Python PCEP Functions
IHTMINSTITUTE
 
PPTX
Python PCEP Multidemensional Arrays
IHTMINSTITUTE
 
PPTX
Python PCEP Operations On Lists
IHTMINSTITUTE
 
PPTX
Python PCEP Sorting Simple Lists
IHTMINSTITUTE
 
PPTX
Python PCEP Lists Collections of Data
IHTMINSTITUTE
 
PPTX
Python PCEP Logic Bit Operations
IHTMINSTITUTE
 
PPTX
Python PCEP Loops
IHTMINSTITUTE
 
PPTX
Python PCEP How To Talk To Computer
IHTMINSTITUTE
 
PPTX
Python PCEP Variables
IHTMINSTITUTE
 
PPTX
Python PCEP Operators
IHTMINSTITUTE
 
PPTX
Python PCEP Literals
IHTMINSTITUTE
 
PPTX
IHTM Python PCEP Hello World
IHTMINSTITUTE
 
PPTX
IHTM Python PCEP Introduction to Python
IHTMINSTITUTE
 
PPTX
Python PCEP Welcome Opening
IHTMINSTITUTE
 
Python PCEP Tuples and Dictionaries
IHTMINSTITUTE
 
Python PCEP Tuples and Dictionaries
IHTMINSTITUTE
 
Python PCEP Creating Simple Functions
IHTMINSTITUTE
 
Python PCEP Functions And Scopes
IHTMINSTITUTE
 
Python PCEP Function Parameters
IHTMINSTITUTE
 
Python PCEP Functions
IHTMINSTITUTE
 
Python PCEP Multidemensional Arrays
IHTMINSTITUTE
 
Python PCEP Operations On Lists
IHTMINSTITUTE
 
Python PCEP Sorting Simple Lists
IHTMINSTITUTE
 
Python PCEP Lists Collections of Data
IHTMINSTITUTE
 
Python PCEP Logic Bit Operations
IHTMINSTITUTE
 
Python PCEP Loops
IHTMINSTITUTE
 
Python PCEP How To Talk To Computer
IHTMINSTITUTE
 
Python PCEP Variables
IHTMINSTITUTE
 
Python PCEP Operators
IHTMINSTITUTE
 
Python PCEP Literals
IHTMINSTITUTE
 
IHTM Python PCEP Hello World
IHTMINSTITUTE
 
IHTM Python PCEP Introduction to Python
IHTMINSTITUTE
 
Python PCEP Welcome Opening
IHTMINSTITUTE
 
Ad

Recently uploaded (20)

PPTX
PROCESS FOR CREATION OF BUSINESS PARTNER IN SAP
AhmadAli716831
 
PDF
Logging and Automated Alerting Webinar.pdf
ControlCase
 
PPTX
最新版加拿大奎斯特大学毕业证(QUC毕业证书)原版定制
taqyed
 
PPTX
Q1 English3 Week5 [email protected]
JenniferCawaling1
 
PDF
Slides: Eco Economic Epochs for The World Game (s) pdf
Steven McGee
 
PDF
B M Mostofa Kamal Al-Azad [Document & Localization Expert]
Mostofa Kamal Al-Azad
 
PDF
ChatGPT A.I. Powered Chatbot and Popularization.pdf
StanleySamson1
 
PDF
DDoS in India, presented at INNOG 8 by Dave Phelan
APNIC
 
PDF
Paper: The World Game (s) Great Redesign.pdf
Steven McGee
 
PDF
BroadLink Cloud Service introduction.pdf
DevendraDwivdi1
 
PPTX
The ARUBA Kind of new Proposal Umum .pptx
andiwarneri
 
PPTX
BitRecover OST to PST Converter Software
antoniogosling01
 
PPTX
最新版美国特拉华大学毕业证(UDel毕业证书)原版定制
taqyea
 
PDF
Global Networking Trends, presented at the India ISP Conclave 2025
APNIC
 
PPTX
原版澳洲斯文本科技大学毕业证(SUT毕业证书)如何办理
taqyed
 
PPTX
Pitch PitchPitchPitchPitchPitchPitch.pptx
157551
 
PPTX
IAREUOUSTPIDWHY$)CHARACTERARERWUEEJJSKWNSND
notgachabite123
 
PDF
Clive Dickens RedTech Public Copy - Collaborate or Die
Clive Dickens
 
PPTX
history of internet in nepal Class-8 (sparsha).pptx
SPARSH508080
 
PPT
Almos Entirely Correct Mixing with Apps to Voting
gapati2964
 
PROCESS FOR CREATION OF BUSINESS PARTNER IN SAP
AhmadAli716831
 
Logging and Automated Alerting Webinar.pdf
ControlCase
 
最新版加拿大奎斯特大学毕业证(QUC毕业证书)原版定制
taqyed
 
Slides: Eco Economic Epochs for The World Game (s) pdf
Steven McGee
 
B M Mostofa Kamal Al-Azad [Document & Localization Expert]
Mostofa Kamal Al-Azad
 
ChatGPT A.I. Powered Chatbot and Popularization.pdf
StanleySamson1
 
DDoS in India, presented at INNOG 8 by Dave Phelan
APNIC
 
Paper: The World Game (s) Great Redesign.pdf
Steven McGee
 
BroadLink Cloud Service introduction.pdf
DevendraDwivdi1
 
The ARUBA Kind of new Proposal Umum .pptx
andiwarneri
 
BitRecover OST to PST Converter Software
antoniogosling01
 
最新版美国特拉华大学毕业证(UDel毕业证书)原版定制
taqyea
 
Global Networking Trends, presented at the India ISP Conclave 2025
APNIC
 
原版澳洲斯文本科技大学毕业证(SUT毕业证书)如何办理
taqyed
 
Pitch PitchPitchPitchPitchPitchPitch.pptx
157551
 
IAREUOUSTPIDWHY$)CHARACTERARERWUEEJJSKWNSND
notgachabite123
 
Clive Dickens RedTech Public Copy - Collaborate or Die
Clive Dickens
 
history of internet in nepal Class-8 (sparsha).pptx
SPARSH508080
 
Almos Entirely Correct Mixing with Apps to Voting
gapati2964
 

Python PCEP Comparison Operators And Conditional Execution

  • 1. Module 2 Data Collections – Lists, Tuples, and Dictionaries Comparison operators and conditional execution
  • 2. Module Objective In this module, you will cover the following topics: • the Boolean data type; • relational operators; • making decisions in Python (if, if-else, if-elif,else) • how to repeat code execution using loops (while, for) • how to perform logic and bitwise operations in Python; • lists in Python (constructing, indexing, and slicing; content manipulation) • how to sort a list using bubble-sort algorithms; • multidimensional lists and their applications.
  • 3. Module 2 Comparison operators &Conditional execution Questions Yes, this is true No, this is false Probably yes Let me think Comparison: equality operator Equality == • 2 == 2 True • 2 == 2. True • 1 ==2 False Inequality != • • var = 0 Print(var != 0) var = 1 Print(var != 0)
  • 4. Module 2 Comparison operators &Conditional execution Priority Operator 1 +, 2 unary 2 ** 3 *, /. //, % 4 +, - binary 5 <, <=, >, >= 6 ==, != Comparison operators greater than > • black_sheep > white_sheep greater than or equal to >= • centigrade_outside >= 0.0 less than < • current_velocity_mph < 85 less than or equal to <= • current_velocity_mph <= 85
  • 5. Module 2 Comparison operators &Conditional execution Conditions & Conditional execution if sheep_counter >= 120: make_a_bed() take_a_shower() sleep_and_dream() feed_the_sheepdogs() If the weather is good, we'll go for a walk then, we'll have lunch. if the_weather_is_good:​ go_for_a_walk()​ have_lunch()​ Conditionally executed statements have to be indented
  • 6. Module 2 Comparison operators &Conditional execution if-else statement if true_or_false_condition: perform_if_condition_true else: perform_if_condition_false if the_weather_is_good: go_for_a_walk() else: go_to_a_theater() have_lunch() if the_weather_is_good: if nice_restaurant_is_found: have_lunch() else: eat_a_sandwich() else: if tickets_are_available: go_to_the_theater() else: go_shopping()
  • 7. Module 2 Comparison operators &Conditional execution elif statement if the_weather_is_good: go_for_a_walk() elif tickets_are_available: go_to_the_theater() elif table_is_available: go_for_lunch() else: play_chess_at_home() If the weather is fine, we'll go for a walk. otherwise if we get tickets, we'll go to the theater otherwise if there are free tables at the restaurant, we'll go for lunch if all else fails, we'll return home and play chess.
  • 8. Module 2 Comparison operators &Conditional execution Analyzing code samples 1 # Read two numbers number1 = int(input("Enter the first number: ")) number2 = int(input("Enter the second number: ")) # Choose the larger number if number1 > number2: larger_number = number1 else: larger_number = number2 # Print the result print("The larger number is:", larger_number) # Read two numbers number1 = int(input("Enter the first number: ")) number2 = int(input("Enter the second number: ")) # Choose the larger number if number1 > number2: larger_number = number1 else: larger_number = number2 # Print the result print("The larger number is:", larger_number)
  • 9. Module 2 Comparison operators &Conditional execution # Read three numbers number1 = int(input("Enter the first number: ")) number2 = int(input("Enter the second number: ")) number3 = int(input("Enter the third number: ")) # We temporarily assume that the first number is the largest one. # We will verify this soon. largest_number = number1 # We check if the second number is larger than current largest_number # and update largest_number if needed. if number2 > largest_number: largest_number = number2 # We check if the third number is larger than current largest_number # and update largest_number if needed. if number3 > largest_number: largest_number = number3 # Print the result print("The largest number is:", largest_number) Analyzing code samples 2
  • 10. Module 2 Comparison operators &Conditional execution Pseudocode & introduction # Read three numbers. number1 = int(input("Enter the first number: ")) number2 = int(input("Enter the second number: ")) number3 = int(input("Enter the third number: ")) # Check which one of the numbers is the greatest # and pass it to the largest_number variable. largest_number = max(number1, number2, number3) # Print the result. print("The largest number is:", largest_number) pseudocode
  • 11. Module 2 Comparison operators &Conditional execution Key takeaways •Comparison operators: ==, !=, >, >=, <, <=​ •Conditional statement:​ •if •if-else •if-elif-else •Nested
  • 12. Module 2 Comparison operators &Conditional execution LAB Practice 10. Questions and answers 11. Comparison operators and conditional execution 12. Essentials of if-else statement 13. Essentials of if-else statement (1)