Python: Program Structure

We can structure our Python program in 3 ways-

Sequential: these steps are executed one by one in the sequence the steps are written.
Conditional: executes one or the other block based on certain conditions.
Repeat/Iterate: bunch of steps repeated several times(based on some condition, or set of data).

All these 3 ways can be combined together to create a full program.

Sequential Steps

print("Demonestrate sequential steps in Python") # Step 1

num1 = 100 # Step 2
num2 = 900 # Step 3

sum = num1 + num2 # Step 4

print(f"Sum of {num1} and {num2} is: {sum}") # Step 5

my_input = input("Enter some value:") # Step 6

print("You have entered: ", my_input) # Step 7

print(f"Value of {num2} * {my_input} is: {num2 * int(my_input)}") # Step 8
Python

Output:

Demonestrate sequential steps in Python
Sum of 100 and 900 is: 1000
Enter some value:12345
You have entered:  12345
Value of 900 * 12345 is: 11110500
Plaintext

Conditional Steps

my_val = 88

if my_val < 70:
    print("Need improvement")
elif my_val < 90:
    print("Good")
else: # More than 90
    print("Excellent")
Python

Output:

Good
Plaintext

Repeated Steps

my_val = 5

print("Output of first loop:")
while my_val > 2:
    print(my_val)
    
    # Decrease the value of my_val
    my_val = my_val - 1
    
print("Output of second loop:")

for i in range(0, 5):
    print(i)
Python

Output:

Output of first loop:
5
4
3

Output of second loop:
0
1
2
3
4
Plaintext

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.