0% found this document useful (0 votes)
5 views4 pages

Python Control Structures Explained

Control structures in Python, including conditional statements and loops, dictate how a program executes statements, enabling decision-making and repetition of actions. Conditional statements such as if, elif, and else allow code execution based on specific conditions, while loops (for and while) enable repetitive execution of code blocks. Additionally, keywords like break, continue, and pass modify loop behavior and control flow.

Uploaded by

itsurboysayron
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Python Control Structures Explained

Control structures in Python, including conditional statements and loops, dictate how a program executes statements, enabling decision-making and repetition of actions. Conditional statements such as if, elif, and else allow code execution based on specific conditions, while loops (for and while) enable repetitive execution of code blocks. Additionally, keywords like break, continue, and pass modify loop behavior and control flow.

Uploaded by

itsurboysayron
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Control Structures

Control structures dictate how a program executes statements; they allow it to make decisions and repeat actions. In
Python, there are two principal types of control structures: conditional statements and loops. These structures serve to
guide the flow of code based on certain conditions or to iterate through datasets.

Conditional Statements

Conditional statements in Python allow for the execution of code blocks based on specific conditions. This capability is
crucial for developing interactive and responsive applications. The primary conditional statements are if, elif, and else.

If Statements

The simplest conditional structure is the if statement. It executes a block of code if the specified condition evaluates to
true.

age = 20

if age >= 18:

print("You are eligible to vote.")

In this example, if the age variable is 18 or older, the message indicating eligibility will print. If the condition is false,
nothing occurs.

Elif Statements

The elif statement is an abbreviation for “else if,” allowing the evaluation of another condition if the previous if
condition is false.

num = 10

if num > 10:

print("Number is greater than 10.")

elif num == 10:

print("Number is equal to 10.")

This code evaluates if num is greater than 10; if not, it checks if it equals 10, providing an alternative path based on the
outcome of these conditions.
Else Statements

The else statement is utilized when all preceding conditions evaluate as false. It serves as a final catch-all option.

temperature = 15

if temperature > 30:

print("It's a hot day!")

elif temperature > 20:

print("It's a warm day!")

else:

print("It's a cold day!")

In this instance, if the temperature is neither above 30 nor 20, it outputs that it’s a cold day.

Chained Comparisons

Python allows chaining multiple comparison operators in one line, making code more efficient and concise.

age = 25

if 18 <= age < 65:

print("Working age")

This structure is particularly useful for age restrictions or other ranges.

Compound Conditions

You can combine multiple conditions using logical operators: and, or, and not. These operators help in creating more
complex decision-making logics.

is_raining = False

have_umbrella = True

if is_raining and have_umbrella:

print("You can go outside with an umbrella.")

else:

print("Better stay inside!")


Loops

Loops are another critical control structure in Python that enables the repetition of code blocks. The primary types of
loops in Python are for loops and while loops.

For Loops

For loops iterate over a sequence (such as a list or a range) and execute a block of code for each item.

for i in range(5):

print(i)

In this example, it will print numbers from 0 to 4.

While Loops

While loops continue executing a block of code as long as a specified condition is true.

count = 0

while count < 5:

print(count)

count += 1

This loop will print numbers from 0 to 4, similar to the for loop but using a different method of iteration.
Control Flow Keywords

Python includes keywords that modify loops and control the flow within them:

Break

The break keyword terminates the loop and transfers the control to the statement following the loop.

for num in range(10):

if num == 5:

break

print(num)

In this code, the loop stops executing when num reaches 5.

Continue

The continue keyword allows the loop to skip the current iteration and proceed to the next one.

for num in range(5):

if num == 2:

continue

print(num)

This will print all numbers except 2.

Pass

The pass statement is a null operation; it is syntactically required but performs no action. It’s helpful when a statement
is required syntactically but no code should be executed.

for num in range(5):

if num == 2:

pass # This does nothing

print(num)

You might also like