0% found this document useful (0 votes)
8 views16 pages

Selection Statements Python Presentation

Selection statements in Python allow programs to make decisions by executing different code blocks based on conditions, enhancing control flow and logic. Key types include if, if...else, and if...elif...else statements, which are essential for real-world applications like authentication and grading systems. Logical operators and nested statements further support complex decision-making processes in programming.

Uploaded by

brittaodongo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views16 pages

Selection Statements Python Presentation

Selection statements in Python allow programs to make decisions by executing different code blocks based on conditions, enhancing control flow and logic. Key types include if, if...else, and if...elif...else statements, which are essential for real-world applications like authentication and grading systems. Logical operators and nested statements further support complex decision-making processes in programming.

Uploaded by

brittaodongo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Selection

Statements in
Python
What Are Selection
Statements?
Allow programs to make decisions
◦ Execute different code blocks based on conditions
◦ Improve control flow and logic in Python
Importance of Selection
Statements
Enable conditional execution
◦ Support real-world problem solving
◦ Used in authentication, grading systems, billing systems, etc.
The if Statement
Executes a block only if the condition is True
◦ Syntax: if condition: statements
◦ Example:
age = 20
if age >= 18:
print('Eligible to vote')
The if…else Statement
Provides two alternative outcomes
◦ Useful when one condition determines two paths
◦ Example:
temp = 15
if temp > 25:
print('Warm')
else:
print('Cold')
The if…elif…else
Statement
Used for handling multiple conditions
◦ Python evaluates conditions from top to bottom
◦ Example: grading system with A, B, C, D
Using elif – Example
marks = 72
◦ if marks >= 80: print('A')
◦ elif marks >= 70: print('B')
◦ elif marks >= 60: print('C')
◦ else: print('D')
Logical Operators in
Conditions
and – both conditions must be true
◦ or – at least one condition must be true
◦ not – reverses a Boolean value
◦ Useful in login systems and validation
Examples with Logical
Operators
if age >= 18 and citizen: Eligible to vote
◦ if day == 'Saturday' or day == 'Sunday': Weekend
◦ if not rainy: No umbrella needed
Nested Selection
Statements
An if inside another if
◦ Useful when validating multiple layers of conditions
◦ Example: age check + ID verification
User Input with
Selection
number = int(input('Enter a number'))
◦ if number > 0: Positive
◦ elif number < 0: Negative
◦ else: Zero
Real-World Example:
Login Check
username = 'admin'
◦ password = '1234'
◦ Check if both match user input
◦ Print success or failure message
Real-World Example:
Billing System
Different electricity billing brackets
◦ Use if, elif and else for tiered pricing
◦ Example: units * cost per unit
References
[Link]
Common Mistakes in
Selection Statements
Using = instead of == in conditions
◦ Incorrect indentation
◦ Unreachable code after else
◦ Misplaced elif conditions
Exercises for Practice
Write a program to determine student grade
◦ Write a program to check login credentials
◦ Write a program to classify numbers as +, -, or 0
◦ Create a simple menu using selection statements

You might also like