Python Diaries ….. Day 7
Conditions…..(if, if-else, if-elif-else)
Welcome to Day 7 of Python Diaries! Today, we’re diving into one of the most fundamental concepts in programming: conditional statements. These statements allow us to control the flow of our programs based on certain conditions.
What are Condition Statements?
Condition statements, often referred to as “if-else” statements, allow us to execute different code blocks based on whether a specified condition is true or false. This enables our programs to make decisions and respond accordingly.
The if
Statement
The if
statement is the most basic form of a condition statement. It checks a condition, and if it evaluates to true, it executes a block of code.
# Example of an if statement
age = 25
if age >= 18:
print("You are an adult.")
The else
Statement
The else
statement is used in conjunction with if
to provide an alternative block of code to execute if the condition is false.
# Example of an if-else statement
age = 15
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
The elif
Statement
The elif
statement is short for "else if". It allows you to check multiple conditions one by one.
# Example of an if-elif-else statement
age = 25
if age < 18:
print("You are a minor.")
elif age >= 18 and age < 30:
print("You are a young adult.")
else:
print("You are an adult.")
Nested Condition Statements
Nested condition statements involve placing one condition statement inside another. This allows for more complex decision-making.
age = 25
if age >= 18:
if age <= 30:
print("You are a young adult.")
else:
print("You are an adult.")
else:
print("You are a minor.")
Multiple Conditions with and
and or
You can use the and
and or
operators to combine multiple conditions.
age = 25
if age >= 18 and age <= 30:
print("You are a young adult.")
"pass"
statement
The pass
statement in Python is a null operation. It is used when a statement is syntactically required, but you want to do nothing.
For example, it can be used in the body of an if
statement or a loop when you don't want to perform any action:
if condition:
pass # No action is taken
for i in range(5):
pass # No action is taken
"continue"
Statement
The continue
statement is used to skip the rest of the loop and continue with the next iteration.
for i in range(5):
if i == 2:
continue
print(i)
In this example, when i
is 2, the continue
statement is executed, which skips the print statement for that iteration.
"break”
Statement
The break
statement is used to exit a loop prematurely, before it has completed all its iterations.
for i in range(5):
if i == 3:
break
print(i)
"assert"
Statement
The assert
statement is used to check conditions that should always be true. If the condition is false, an AssertionError is raised.
x = 10
assert x > 5, "x should be greater than 5"
Short Hand If,if-else
The ternary operator (if-else
expression) provides a concise way to write conditional expressions in a single line.
age = 25
status = "adult" if age >= 18 else "minor"
print(f"You are a {status}.")
Condition statements are crucial for building programs that can make decisions based on different scenarios. They allow us to control the flow of our code, making it more dynamic and responsive. Practice writing condition statements to become proficient in using them effectively.
You can access the other topics in this tutorial series right here:
- Day 1: Data types, Comment, Type casting and User input.
- Day 2: Numbers, Strings, and Booleans.
- Day 3: Operators
- Day 4: Lists
- Day 5: Tuples and Sets
- Day 6: Dictionaries
- Day 7: If , If Else, If elif else Condition statements
- Day 8: While loops
- Day 9: For loops
- Day 10: Functions
- Day 11: File handling
- Day 12: Error Handling
- Day 13: Regular expressions
- Day 14: OOPS in python (class)
- Day 15: Python GUI libraries
In Day 8, we’ll explore loops, another fundamental concept in programming. Keep up the great work! Happy coding!