Flow control statements in Python

Flow control statements

Mastering Flow Control in Python

The power to control your programs

Welcome to the world of Python programming, where you have the power to control how your programs flow and make decisions! In this article, specially designed for beginners, we will explore the exciting realm of flow control statements in Python. These statements allow you to determine which parts of your code are executed, how many times they are executed, and make choices based on specific conditions. Let's dive in and unravel the magic of flow control in Python together!

Conditional Statements with if

The "if" statement allows your program to make decisions based on certain conditions. It checks whether a condition is true and executes a block of code if the condition is met. Example:
age = 12
if age <= 13:
     print("You are a teenager!")
else:
    print("You are not yet a teenager.")
In this example, the program checks if the variable age is greater than or equal to 13. If it is, it prints "You are a teenager!" If the condition is not met, it executes the code under the "else" statement and prints "You are not yet a teenager."

Looping Statements

Looping statements allow you to repeat a block of code multiple times. There are two primary types of loops in Python: "for" and "while."

For loop

The "for" loop iterates over a sequence (such as a list) and performs an action for each element. Example:
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
     print(fruit)
In this example, the program iterates over each element in the fruits list and prints them one by one.

While loop

The "while" loop repeats a block of code as long as a condition is true. Example:
count = 0
while count < 5:
     print("Count:", count)
    count += 1
In this example, the program keeps printing the value of count as long as it is less than 5. It increments the value of count by 1 after each iteration.

List of flow control statements

if

Allows your program to make decisions based on certain conditions. It checks whether a condition is true and executes a block of code if the condition is met.

if condition:
# Code block executed if condition is true
else

Works together with the "if" statement. If the condition in the "if" statement is not met, the code block under the "else" statement is executed.

if condition:
# Code block executed if condition is true
else:
# Code block executed if condition is false
elif

An abbreviation for "else if." It is used to check additional conditions after the "if" statement and before the "else" statement. If the condition in the "elif" statement is true, the corresponding code block is executed.

if condition1:
# Code block executed if condition1 is true
elif condition2:
# Code block executed if condition2 is true
else:
# Code block executed if all conditions are false
for

Creates a loop that iterates over a sequence (such as a list or a string) and performs an action for each element in the sequence.

for element in sequence:
# Code block executed for each element in the sequence
while

Creates a loop that repeats a block of code as long as a condition is true. It keeps executing the code until the condition becomes false.

while condition:
# Code block executed as long as the condition is true
break

Allows you to exit or terminate a loop prematurely. It is often used with the "if" statement to check a certain condition and stop the loop when that condition is met.

while condition:
if some_condition:
# Code block executed when some_condition is true
break
# Code block continues if some_condition is false
continue

Skips the remaining code in the current iteration of a loop and moves on to the next iteration. It is often used with the "if" statement to check a certain condition and move to the next iteration when that condition is met.

while condition:
if some_condition:
# Code block skipped when some_condition is true
continue
# Code block continues if some_condition is false
pass

Used as a placeholder when you want an empty code block. It allows you to have syntactically correct code without any specific actions.

if condition:
# Code block executed if condition is true
else:
pass # Empty code block
try/except

Allows you to handle and manage exceptions or errors that may occur in your code. The "try" block contains the code that might raise an exception, and the "except" block handles the exception by providing an alternative code to execute.

try:
# Code block that might raise an exception
except ExceptionType:
# Code block executed if the specified exception occurs
finally

Works together with the "try/except" statement. The "finally" block contains code that will be executed regardless of whether an exception occurred or not.

try:
# Code block that might raise an exception
except ExceptionType:
# Code block executed if the specified exception occurs
finally:
# Code block executed regardless of exceptions

Make decisions

Congratulations on mastering flow control in Python! You now have the power to make decisions, repeat code, and control the flow of your programs. Conditional statements like "if" help you make choices based on conditions, while looping statements like "for" and "while" allow you to repeat code and perform actions multiple times. Remember, flow control is like having a superpower that empowers you to create dynamic and interactive programs. So, keep exploring, practicing, and let your imagination soar as you continue your coding journey into the fascinating world of Python! Happy coding!