The execution flow of a Python program is governed by three fundamental control structures: conditional statements, loops, and function calls.
Python encompasses three primary types of control structures:
- Sequential: This mode represents the default behavior where statements are executed in the order they appear in the script. It follows a linear progression from the beginning to the end of the code.
- Selection: This structure is employed for decision-making and branching within the program. It allows the execution of specific code blocks based on the evaluation of conditions. Selection control structures include
if,elif, andelsestatements. - Repetition: This control structure facilitates looping, enabling the repetition of a particular code segment multiple times. It is instrumental in iterating over sequences, performing tasks iteratively, and executing code until a specified condition is met. In Python, repetition is achieved through constructs like
forloops andwhileloops.
Let us look into all one by one
Sequential Control Structure in Python
pythonCopy code# Sequential control structure example
# This program demonstrates the default sequential execution of code
# Step 1: Displaying a welcome message
print("Welcome to the sequential control structure example.")
# Step 2: Getting user input
name = input("Please enter your name: ")
# Step 3: Displaying a personalized greeting
print("Hello,", name, "!")
# Step 4: Performing a calculation
x = 5
y = 10
result = x + y
# Step 5: Displaying the result
print("The result of adding", x, "and", y, "is:", result)
# Step 6: Farewell message
print("Thank you for using the sequential control structure example.")
Explanation:
- In this example, each step is executed sequentially, following the order of appearance in the code.
- Step 1: Displays a welcome message.
- Step 2: Prompts the user to enter their name and stores it in the variable
name. - Step 3: Greets the user with a personalized message using the input from Step 2.
- Step 4: Performs a simple calculation by adding two numbers,
xandy, and stores the result in the variableresult. - Step 5: Displays the result of the calculation.
- Step 6: Prints a farewell message.
- The program executes each step in sequence, illustrating the sequential control structure of Python programs.
Selection control structures
In Python help your program decide what to do depending on different situations. They let you run specific parts of your code only if certain conditions are met. So, if something is true, your program will do one thing, and if it’s false, it will do something else. It’s like giving your program a set of instructions to follow depending on what’s happening at the moment.
#if Statement:
#Syntax:
if condition:
statement
or
if condition:
statement-1
statement-2
statement-3
#If the condition is true, the associated statements will be executed.
Example:
name = input("Enter Name:")
if name == "nuclear geeks":
print("Hello Nuclear Geeks, Good Morning")
print("How are you!!!")
#if-else Statement:
Syntax:
if condition:
Action-1
else:
Action-2
If the condition is true, Action-1 will be executed; otherwise, Action-2 will be executed.
Example:
name = input("Enter Name:")
if name == "nuclear geeks":
print("Hello Nuclear Geeks, Good Morning")
else:
print("Hello Guest, Good Morning")
print("How are you!!!")
#if-elif-else Statement:
Syntax:
if condition1:
Action-1
elif condition2:
Action-2
elif condition3:
Action-3
...
else:
Default Action
Based on the conditions, the corresponding action will be executed. If none of the conditions are met, the default action will be executed.
Example:
brand = input("Enter Your Favourite Brand:")
if brand == "RC":
print("It is a children's brand")
elif brand == "KF":
print("It is not that much kick")
elif brand == "FO":
print("Buy one get one Free")
else:
print("Other Brands are not recommended")
Each of these structures helps your program make decisions based on conditions, allowing it to be more flexible and responsive to different situations.