Chapter 1: Introduction to Control Flow
Control flow is the order in which statements are executed in a program. In Python, it allows you to make decisions and execute specific code blocks based on conditions. Understanding control flow is essential for creating dynamic and responsive programs.
Sequential Execution
By default, Python executes statements in a sequential manner, one after the other:
# Sequential execution
print("Statement 1")
print("Statement 2")
print("Statement 3")
In this example, "Statement 1" will be executed first, followed by "Statement 2" and "Statement 3" in order.
Conditional Execution
Control flow introduces conditional execution through structures like if
, elif
, and else
:
# Conditional execution
if condition:
print("This block will be executed if the condition is True")
elif another_condition:
print("This block will be executed if the first condition is False and the second condition is True")
else:
print("This block will be executed if none of the above conditions are True")
Control flow enables you to create programs that respond to different scenarios.
Chapter 2: Conditional Statements and Expressions
Conditional statements allow you to make decisions in your code. They are based on evaluating whether a certain condition is True
or False
.
if Statements
The simplest form of conditional statement is the if
statement:
# if statement
temperature = 25
if temperature > 20:
print("It's a warm day!")
In this example, the indented block under if temperature > 20:
will only be executed if the condition is True
.
elif Statements
Use elif
to check multiple conditions sequentially:
# elif statement
temperature = 15
if temperature > 20:
print("It's a warm day!")
elif temperature <= 20 and temperature > 10:
print("It's a moderate day.")
else:
print("It's a cold day.")
The first True
condition encountered will execute its corresponding block, and the rest will be skipped.
Switching to Ternary Operators
Python supports a concise way of writing conditional expressions using the ternary operator:
# Ternary operator
result = "positive" if x > 0 else "zero or negative"
This is a compact way to express simple if-else conditions in a single line.
Chapter 3: Switching to Ternary Operators
Discover the power of the ternary operator in Python. This concise syntax allows you to condense if-else statements into a single line.
Syntax of the Ternary Operator
The ternary operator has the following syntax:
result = value_if_true if condition else value_if_false
Here, condition
is the expression to be evaluated. If it's True
, value_if_true
is assigned to result
; otherwise, value_if_false
is assigned.
Examples of Ternary Operator Usage
Let's look at some practical examples:
# Example 1
result = "positive" if x > 0 else "zero or negative"
# Example 2
status = "even" if num % 2 == 0 else "odd"
# Example 3
greeting = "Hello" if time_of_day == "morning" else "Good evening"
The ternary operator is a powerful tool for writing compact and readable code when dealing with simple if-else conditions.
Chapter 4: Putting It All Together - A Control Flow Example
Now, let's put our knowledge of control flow and conditional statements to use in a practical example. Suppose we want to create a program that determines if a given year is a leap year.
# Leap year check
year = 2024
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a leap year!")
else:
print(f"{year} is not a leap year.")
In this example, the program checks the conditions for a leap year and prints the result accordingly. Understanding control flow allows us to create programs that make decisions based on specific criteria.
Stay tuned for Day 4, where we'll explore Loops and Iterations in Python.
If you have any questions or need further clarification, feel free to ask.
No comments:
Post a Comment