What is a Control Structure in Python

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

What is a Control Structure in Python?

Control structures in Python are fundamental building blocks that allow the program
to make decisions, repeat actions, or control the flow of execution based on
conditions.

Python offers three main types of control structures:

Sequential, Selection (Decision-making), and Repetition (Loops).

Sequential execution is straightforward, where code runs line by line.

Selection structures, like if statements, execute specific blocks based on conditions,


while

repetition structures, like for or while loops, allow code to run multiple times until
a condition is met.

These structures give flexibility to handle various scenarios in a program.

Selection Statements in Python


Selection statements in Python are used to make decisions in a program by
executing certain blocks of code depending on conditions.

1. if Statement

The if statement is the simplest selection structure in Python. It evaluates a condition and
executes the associated block of code only if the condition is True. If the condition is False,
the program skips the if block and continues with the next statement.

Syntax:

if condition:

# Code to execute if the condition is True

Example:
number = int(input("Enter a number: "))

if number > 0:
print("The number is positive.")
if-else Statement

The if-else statement allows the program to handle two possible outcomes
of a condition. If the condition is True, the code inside the if block executes;
otherwise, the code inside the else block runs.

Syntax:

if condition:
# Code to execute if the condition is True
else:
# Code to execute if the condition is False
Example
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")

• The program checks if age is greater than or equal to 18.


• If True, it prints "You are eligible to vote."
• If False, it prints "You are not eligible to vote."

if-elif-else Statement

The if-elif-else statement is used when there are multiple conditions to check.

It allows you to test multiple conditions sequentially.

The elif (short for "else if") is checked only if the previous conditions are False. The else
block, if present, executes only when all conditions are False.

Syntax:

if condition1:
# Code to execute if condition1 is True
elif condition2:
# Code to execute if condition2 is True
elif condition3:
# Code to execute if condition3 is True
else:
# Code to execute if all conditions are False
Example :
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 50:
print("Grade: C")
else:
print("Grade: F")

The program checks the user's marks against multiple conditions:

• If marks >= 90, it prints "Grade: A."


• If the first condition is False but marks >= 75, it prints "Grade: B."
• If both are False but marks >= 50, it prints "Grade: C."
• If none of the conditions are True, it prints "Grade: F."

Key Points to Remember

1. Indentation: Python uses indentation (whitespace) to define blocks of code. Ensure


proper alignment to avoid errors.
2. Boolean Conditions: Conditions in if statements should return True or False.
3. Nested Selection: You can nest if statements inside other if, else, or elif blocks
to handle more complex decisions.
4. Readability: Use elif instead of multiple if statements when the conditions are
mutually exclusive.

You might also like