Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views37 pages

Python Conditional Statements

Chapter 5 covers the use of if statements in programming, including conditional tests, checking for equality and inequality, and handling multiple conditions. It provides examples of simple if statements, if-else statements, and the if-elif-else chain, along with exercises to reinforce learning. The chapter emphasizes best practices for writing clear and readable conditional logic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views37 pages

Python Conditional Statements

Chapter 5 covers the use of if statements in programming, including conditional tests, checking for equality and inequality, and handling multiple conditions. It provides examples of simple if statements, if-else statements, and the if-elif-else chain, along with exercises to reinforce learning. The chapter emphasizes best practices for writing clear and readable conditional logic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

CHAPTER 5: IF STATEMENTS

A Simple Example
• A simple demonstration of using an if
statement.
• Example:
• if age >= 18:
• print('You are eligible to vote.')
Conditional Tests
• Tests that evaluate to True or False.
• Example:
• car = 'bmw'
• car == 'bmw' # True
Checking for Equality
• Check if two values are equal using ==.
• Example:
• car = 'bmw'
• car == 'bmw' # True
Ignoring Case When Checking for
Equality
• Use .lower() for case-insensitive comparison.
• Example:
• car = 'BMW'
• car.lower() == 'bmw' # True
Checking for Inequality
• Check if two values are not equal using !=.
• Example:
• car = 'audi'
• car != 'bmw' # True
Numerical Comparisons
• Compare numerical values using operators
like >, <, ==, !=.
• Example:
• age = 18
• age >= 18 # True
Checking Multiple Conditions
• Use and/or to test multiple conditions.
• Example:
• age = 22
• (age > 18) and (age < 30) # True
Checking Whether a Value Is in a List
• Use 'in' to check membership in a list.
• Example:
• fruits = ['apple', 'banana']
• 'apple' in fruits # True
Checking Whether a Value Is Not in a
List
• Use 'not in' to check absence in a list.
• Example:
• fruits = ['apple', 'banana']
• 'pear' not in fruits # True
Boolean Expressions
• Logical expressions that evaluate to True or
False.
• Example:
• True and False # False
Exercise 5-1: Conditional Tests
• Create tests that evaluate to True and False.
• Example:
• car = 'audi'
• print(car == 'audi') # True
Exercise 5-2: More Conditional Tests
• Write more tests with and/or conditions.
• Example:
• name = 'Alice'
• print(name != 'Bob') # True
if Statements
• Basic structure for conditional logic.
• Example:
• age = 18
• if age >= 18:
• print('Adult')
Simple if Statements
• Only execute code if condition is True.
• Example:
• if age >= 18:
• print('Adult')
if-else Statements
• Provide an alternate path when condition is
False.
• Example:
• age = 16
• if age >= 18:
• print('Adult')
• else:
• print('Minor')
The if-elif-else Chain
• Test multiple conditions in sequence.
• Example:
• age = 12
• if age < 4:
• print('Free')
• elif age < 18:
• print('Discounted')
• else:
• print('Full price')
Using Multiple elif Blocks
• Handle more than two conditions.
• Example:
• age = 20
• if age < 18:
• print('Child')
• elif age < 25:
• print('Young Adult')
• else:
• print('Adult')
Omitting the else Block
• Optionally omit the else block.
• Example:
• if age < 18:
• print('Minor')
• elif age >= 18:
• print('Adult')
Testing Multiple Conditions
• Use separate if statements for multiple
conditions.
• Example:
• if 'pepperoni' in toppings:
• print('Adding pepperoni')
Exercise 5-3: Alien Colors #1
• Write a program with an alien_color variable
and test its value.
• Example:
• alien_color = 'green'
• if alien_color == 'green':
• print('Player earned 5 points')
Exercise 5-4: Alien Colors #2
• Add an else block to handle different alien
colors.
• Example:
• alien_color = 'yellow'
• if alien_color == 'green':
• print('5 points')
• else:
• print('10 points')
Exercise 5-5: Alien Colors #3
• Add elif for more colors.
• Example:
• alien_color = 'red'
• if alien_color == 'green':
• print('5 points')
• elif alien_color == 'yellow':
• print('10 points')
• else:
• print('15 points')
Exercise 5-6: Stages of Life
• Determine stage of life based on age.
• Example:
• age = 30
• if age < 2:
• print('Baby')
Exercise 5-7: Favorite Fruit
• Check if favorite fruits are in a list.
• Example:
• fav_fruits = ['banana', 'apple']
• if 'banana' in fav_fruits:
• print('You love bananas!')
Using if Statements with Lists
• Iterate and check elements in lists.
• Example:
• toppings = ['mushrooms']
• if 'mushrooms' in toppings:
• print('Adding mushrooms')
Checking for Special Items
• Handle specific cases in a list.
• Example:
• toppings = ['pepperoni', 'cheese']
• if 'cheese' in toppings:
• print('Adding cheese')
Checking That a List Is Not Empty
• Ensure a list has elements before processing.
• Example:
• toppings = []
• if toppings:
• print('Adding toppings')
• else:
• print('Plain pizza')
Using Multiple Lists
• Combine or compare lists.
• Example:
• current_users = ['Alice', 'Bob']
• new_users = ['alice', 'Tom']
Exercise 5-8: Hello Admin
• Display a special greeting for admin users.
• Example:
• users = ['admin', 'user1']
• if 'admin' in users:
• print('Hello admin!')
Exercise 5-9: No Users
• Check if the users list is empty.
• Example:
• users = []
• if not users:
• print('No users available')
Exercise 5-10: Checking Usernames
• Ensure new usernames are unique.
• Example:
• current_users = ['Alice']
• new_user = 'alice'
Exercise 5-11: Ordinal Numbers
• Display proper ordinal suffixes.
• Example:
• numbers = [1, 2, 3]
• for num in numbers:
• if num == 1:
• print('1st')
Styling Your if Statements
• Tips for readability and best practices.
• Example:
• if condition:
• # Keep lines short
Exercise 5-12: Styling if Statements
• Write clean and readable if statements.
Exercise 5-13: Your Ideas
• Brainstorm and implement your own
exercises.
Summary
• Recap of chapter concepts and exercises.

You might also like