Python- Day 15-IF Statements- Core Principles
Overview of Conditional Statements in Python:
Programming often involves examining conditions and deciding which action to take based on those conditions.
Python
if
statement: Enables examining the current state of a program and responding appropriately to that state.
Goal: Learn to write conditional tests that allow checking conditions of interest. You’ll write simple and complex if
statements to identify exact conditions.
You’ll use these concepts with lists, allowing you to write loops that handle most items uniformly but treat certain items with specific conditions differently.
Simple Example of if
Statement:
You have a list of car names, most of which should be printed in title case except for ‘bmw’, which should be printed in uppercase.
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
>>
Audi
BMW
Subaru
Toyota
The loop checks if the current value of car
is 'bmw'
and prints it in uppercase if true; otherwise, it prints the car name in title case.
Conditional Tests:
An expression evaluated as either True
or False
.
Python uses the outcome of these tests to determine whether to execute the following code block.
If the test evaluates to
True
, Python executes the code following theif
statement.If the test evaluates to
False
, Python ignores the code following theif
statement.
Checking for Equality:
Equality Operator (==
): Checks if the value of a variable matches a specific value.
car = 'bmw'
car == 'bmw' # True
If the values match, the result is True
; otherwise, it is False
car = 'bmw'
car == 'bmw'
True
car = 'audi'
car == 'bmw'
False
Ignoring Case When Checking for Equality:
Case Sensitivity: Python checks for case differences during comparisons.
car = 'Audi'
car == 'audi' # False
Solution: Convert the string to lowercase using .lower()
to perform case-insensitive comparisons.
car = 'Audi'
car.lower() == 'audi' # True
Checking for Inequality:
Inequality Operator (!=
): Used to check if two values are not equal.
requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
print("Hold the anchovies!")
This condition checks if the topping is not ‘anchovies’ and prints a message if true.
Numerical Comparisons:
Python allows various comparisons with numbers, such as ==
, !=
, <
, <=
, >
, >=
.
age = 18
age == 18 # True
age < 21 # True
Checking Multiple Conditions:
and
Operator: Both conditions must be true for the overall expression to be True
age_0 = 22
age_1 = 18
age_0 >= 21 and age_1 >= 21 # False
or
Operator: Only one condition needs to be true for the overall expression to be True
age_0 = 22
age_1 = 18
age_0 >= 21 or age_1 >= 21 # True
Checking Whether a Value is in a List:
in
Keyword: Checks if a value exists in a list.
requested_toppings = ['mushrooms', 'onions', 'pineapple']
'mushrooms' in requested_toppings # True
'pepperoni' in requested_toppings # False
Checking Whether a Value is Not in a List:
not in
Keyword: Used to check if a value is absent from a list.
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
print(f"{user.title()}, you can post a response if you wish.")
Boolean Expressions:
A Boolean expression is a conditional test that evaluates to True
or False
.
Boolean values (True
, False
) are used to track the state of conditions in programs, such as game status or user permissions
game_active = True
can_edit = False
This structured approach covers the core principles of Python’s
if
statements, conditional tests, equality/inequality checks, and Boolean logic.
If you found this guide helpful then do click on 👏 the button.
Follow for more Learning like this 😊
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!