Skip to main content

Command Palette

Search for a command to run...

"Control Flow Statements in Python 🐍"

Updated
3 min read
"Control Flow Statements in Python 🐍"

Control Flow Statements:

Control flow statements are fundamental in programming as they allow you to change the execution order of your code based on certain conditions. In Python, control flow includes:

1.Conditional Statements (if, elif, else)

2.Loops (while, for)

3.jumping Statements (break, continue, pass)

1. Conditional Statements:

Conditional statements in Python allow you to execute certain blocks of code based on whether specific conditions are met. They are fundamental for decision-making in programming.

The primary conditional statements in Python are if, elif, and else. Here’s the basic syntax:

# if condition1:
#     # Code to execute if condition1 is true
# elif condition2:
#     # Code to execute if condition2 is true
# else:
#     # Code to execute if none of the conditions are true

# Example: Check if a number is positive, negative, or zero.
number = 5
if number > 0:
     print("Positive number")   #output:positive number
elif number < 0:
     print("Negative number")
else:
     print("Zero")

Python supports a shorthand for simple conditions, often referred to as the ternary operator:

age = 17
status = "Adult" if age >= 18 else "Minor"
print(status)  # Output: Minor

2.Loops:

Loops allow you to repeatedly execute a block of code. Python provides two types of loops:

  • while loop

  • for loop

while loop:

A while loop repeatedly executes as long as the condition is True. It checks the condition before executing the block of code.

Syntax:

while condition:
    # block of code

examples:

# a. Simple while loop example
i = 1
while i <= 5:
    print(i)
    i += 1  # Increment to avoid infinite loop
# This loop runs as long as i is less than or equal to 5.
#Example of an infinite loop (be careful!):
while True:
    print("This will run forever unless broken.")

for loop:

A for loop is used to iterate over sequences (such as lists, tuples, strings, or ranges).

Syntax:

for item in sequence:
    # block of code

examples:

#Iterating over a list
my_list = [1, 2, 3, 4, 5]
for num in my_list:
    print(num)
#output:
# 1
# 2
# 3
# 4
# 5

jumping statements:

modify the normal flow of loops.

break:

Exits the loop entirely when encountered, even if the condition hasn’t been met yet.

for i in range(10):
    if i == 5:
        break  # Exit the loop when i is 5
    print(i)

#output 
# 1
# 2
# 3
# 4
# 5

continue:

Skips the rest of the loop's body for the current iteration and proceeds with the next iteration.

for i in range(10):
    if i % 2 == 0:
        continue  # Skip the rest of the loop for even numbers
    print(i)  
# Output: 
# 1
# 3
# 5
# 7
# 9

pass:

Does nothing and can be used as a placeholder in situations where a statement is syntactically required but you don't want to write any code.

for i in range(5):
    if i == 3:
        pass  # Placeholder, does nothing
    print(i)

python

Part 4 of 7

🚀 Ready to Get Started? Join me on a journey through Python programming, This series is your friendly guide to learning Python. Whether you're a beginner or want to improve, join me to explore Python and see what you can create! Happy coding! 🎉

Up next

"Data Types and Operators in Python🐍"

What Are Data Types in Python? Data types tell the interpreter what kind of data is being used in the program. Python automatically assigns the correct type to variables when they are created.(dynamically typed) Now we will look into the most importa...

More from this blog

T

TechWave

9 posts