Control structures in Python

Amit Dhanwani on March 10, 2023

Control structures in Python

In this blog, we will discuss control structures in Python, basically Python decision-making constructs. This contains single-statement conditions as well as nested if conditions, if else conditions, elif conditions, and if statements in Python.

Introduction

The Python programming language typically runs the commands one at a time, unless you specify otherwise. The actual power of programming, however, lies in more than just having the program start at the beginning and execute each line of code until the finish.

A program should have the ability to skip over certain instructions, return back to them later, repeat them, or simply pick one of the multiple instructions to run. This is what Python programmers refer to as “flow control” or, alternately, “control flow.”

In programming, we must make decisions and then execute the following code block in accordance with those decisions. Decision-making statements are used in programming languages to control how programs are executed.

In real life, there are times when we must make decisions, and based on those decisions, we choose what to do next.

Programming encounters similar scenarios where we must take decisions and then carry out the following block of code in accordance with those decisions.

Programming languages use decision-making statements to determine the flow of a program’s execution.

Best Online Coding class for kids

What are Python decision making statements?

Every day, every minute of every day, we indulge in conditional programming. It simply involves making decisions and evaluating the situation. If I’m going to be late for work today, I’ll let my manager know, and if it’s raining, I should bring an umbrella.

Similar to this, Python contains a number of built-in keywords that enable programs to carry out various commands under various circumstances.

In a program, we might sometimes need to make a decision on a condition. As we know, the value of an expression can be either true or false. When a specific condition is satisfied, we might only wish to take action.

Consider the example variables a and b. We want to print “a is greater” if a variable is greater than a variable. If not, we need to print “b is greater.”

We employ an “if” statement for this. Additionally, operators are useful for combining conditions to create a composite one.

Python if statements

The simplest expression for making a decision is the if statement. It is used to determine if a certain statement or block of statements will be performed or not, i.e., whether a block of statements will be executed if a specific condition is true or not.

Syntax:

if condition:
   # Statements to execute if the condition is true

The outcome of this evaluation will be either true or false for the condition. If the statement supports boolean values, it will execute the block of statements below it if the value is true; otherwise, it won’t. We may also use a condition with a bracket (“(” or “”)”).

Python, as we all know, employs indentation to identify a block. So, as illustrated in the example below, the block inside an if statement will be executed:

if condition:
  statement1
statement2
# Here if the condition is true, if block will consider only the statement1 to be inside its block.

Flowchart for if:

Figure 1: If flowchart

 

Example 1:

codingal = 2020
if (codingal < 2023):
   print("Codingal has completed 2 years")
print("A big wishes for codingal")

Codingal has completed 2 years

A big wishes for codingal

 

Since the condition inside the if is true, the code block inside the if is executed, and the code block outside the if is also executed, which is independent of the if condition.

Suppose if the condition of the if statement is false, then the code block inside the if will not execute; only the code block outside the if will execute, as shown below:

Example 2:

codingal = 2020
if (codingal > 2023):
   print("Codingal has completed 2 years")
print("A big wishes for codingal")

A big wishes for codingal A big wishes for codingal

Python if-else statements

What if we want to take a specific action when the condition is false rather than do nothing at all? We have if/then statements to fulfill that purpose.

A block of statements will be executed if a condition is true, and if it is false, they won’t be, according to the if statement alone. But what if the condition is false and we want to take another action?

The else statement is now available. If the condition is false, we may use the else statement with the if statement to run a block of code.

Syntax:

if (condition):
  # Executes this block if the condition is true
else:
  # Executes this block if the condition is false

The outcome of this evaluation will be either true or false for the condition. If the statement supports boolean values, it will execute the block of statements below it if the value is true; otherwise, it will execute the block of statements inside the else block. We may also use a condition with a bracket (“(” or “”)”).

 

Flowchart for if-else:

Figure 2: if-else flowchart

 

Example 1:

codingal = 2020
if (codingal < 2023):
  print("Codingal has completed 2 years")
  print("A big wishes for codingal")
else:
  print("Codingal has not completed 2 years")
print("Codingal is the best platform for kids to learn coding at a young age")

Codingal has completed 2 years

A big wishes for codingal

Codingal is the best platform for kids to learn coding at a young age

 

Since the condition inside the if is true, the code block inside the if is executed, and the code inside the else is not executed. Also, the code block outside the if-else condition is also executed, which is independent of the if-else condition.

Suppose if the condition of the if statement is false, then the code block inside the if will not execute and the code inside the else block will execute along with this, and the code block outside the if-else will execute as shown below:

Example 2:

if (codingal > 2023):
  print("Codingal has completed 2 years")
  print("A big wishes for codingal")
else:
  print("Codingal has not completed 2 years")
print("Codingal is the best platform for kids to learn coding at a young age")

Codingal has not completed 2 years

Codingal is the best platform for kids to learn coding at a young age

Python nested if statements

An if statement that is the target of another if statement is said to be nested. An if statement within another if statement is referred to as a “nested if statement.”

Python allows us to nest if statements within if statements. Hence, we may nest one if statement inside another.

Syntax:

if (condition1):
  # Executes when condition1 is true
if (condition2):
  # Executes when condition2 is true
  # if Block is end here
  # if Block is end here

Flowchart for nested if:

Figure 3: Nested if flowchart

 

Example 1:

codingal = 2020
if(codingal == 2020):
  if (codingal < 2021):
    print("Codingal has completed 1 year")
  if (codingal < 2022):
    print("Codingal has completed 2 years")
print("Codingal is the best platform for kids to learn coding at a young age")

Codingal has completed 1 year

Codingal has completed 2 years

Codingal is the best platform for kids to learn coding at a young age

Since the condition inside the if is true and the nested if condition is also true, the code block inside the nested if is executed, and the print statement outside the nested if loop is executed.

Suppose if the condition of the if statement is false, then the code block inside the if will not execute, the nested if will not be visited, and the code block outside the nested if will execute, as shown below:

Example 2:

codingal = 2020
if(codingal == 2020):
  if (codingal > 2021):
    print("Codingal has completed 1 year")
  if (codingal > 2022):
    print("Codingal has completed 2 years")
print("Codingal is the best platform for kids to learn coding at a young age")

Codingal is the best platform for kids to learn coding at a young age

Python nested if-else statements

It is identical to nested if statements, with the exception that, in nested if-else statements, when the if condition is false, the else component will be executed together with any blocks outside of the nested if-else statements.

Syntax:

if (condition1):
  # Statements to execute if condition1 is true
  if (condition2):
    # Statements to execute if condition2 is true
  else:
    # Statements to execute if condition2 is false
else:
  # Statements to execute if condition1 is false

 

Flow chart for nested if-else:

Figure 4: Nested if-else flowchart

 

Example 1:

codingal = 2020
if(codingal == 2020):
   if (codingal < 2023):
    print("Codingal has completed 2 year")
   else:
    print("Codingal has not completed 2 years")
else:
   print("Codingal was started in 2020")
print("Codingal is the best platform for kids to learn coding at a young age")

Codingal has completed 2 year

Codingal is the best platform for kids to learn coding at a young age

 

Since the condition inside the if is true and the nested if condition is also true, the code block inside the nested if is executed, and the print statement outside the nested if loop is executed.

Suppose if the condition of the if statement is false, then the code block inside the if will not execute and the else statement will execute, and along with this, the code block outside the nested if will execute, as shown below:

Example 2: If the inner if condition is false, then the inner else will execute.

codingal = 2020
if(codingal == 2020):
  if (codingal > 2023):
    print("Codingal has completed 2 year")
  else:
    print("Codingal has not completed 2 years")
else:
  print("Codingal was started in 2020")
print("Codingal is the best platform for kids to learn coding at a young age")

Codingal has not completed 2 years

Codingal is the best platform for kids to learn coding at a young age

 

Example 3: If outer if condition is false, then outer else will execute

codingal = 2020
if(codingal < 2020):
   if (codingal < 2023):
     print("Codingal has completed 2 year")
   else:
     print("Codingal has not completed 2 years")
else:
   print("Codingal was started in 2020")
print("Codingal is the best platform for kids to learn coding at a young age")

Codingal was started in 2020

Codingal is the best platform for kids to learn coding at a young age

Python if-elif-else statements

A user can choose from a variety of alternatives here. The top-down ordering of the if statements is followed.

When one of the criteria determining if something is true is satisfied, the statement associated with that if is carried out, and the remaining part of the ladder is skipped.

The last “else” statement will be carried out if none of the conditions are met.

Syntax:

if (condition):
   statement
elif (condition):
   statement
.
.
else:
   statement

Flow chart for if-elif-else:

Figure 5: if-elif-else flowchart

 

Example 1:

codingal = 2020
if(codingal == 2020):
  print("Codingal has started the journey")
elif (codingal == 2021):
  print("Codingal has completed 1 year")
elif (codingal == 2022):
  print("Codingal has completed 2 years")
else:
  print("Codingal was started in 2020")
print("Codingal is the best platform for kids to learn coding at a young age")

Codingal has started the journey

Codingal is the best platform for kids to learn coding at a young age

Since the condition inside the if is true, the code block inside the if is executed, and the print statement outside the nested if loop is executed.

Suppose if the condition of the if statement is false, then the code block inside the if will not execute, and the elif conditions will be checked. If that is true, then the elif statement will execute, and along with this, the code block outside the if-elif-else will execute, as shown below:

Example 2: 

codingal = 2022
if(codingal == 2020):
  print("Codingal has started the journey")
elif (codingal == 2021):
  print("Codingal has completed 1 year")
elif (codingal == 2022):
  print("Codingal has completed 2 years")
else:
  print("Codingal was started in 2020")
print("Codingal is the best platform for kids to learn coding at a young age")

Codingal has completed 2 years

Codingal is the best platform for kids to learn coding at a young age

Suppose if the condition of the if statement is false, then the code block inside the if will not execute, and the elif conditions will be checked and assumed to be false, so the elif statements will not execute either. The last else statement will then execute, and along with this, the code block outside the if-elif-else will execute as shown below:

Example 3:

codingal = 2024
if(codingal == 2020):
  print("Codingal has started the journey")
elif (codingal == 2021):
  print("Codingal has completed 1 year")
elif (codingal == 2022):
  print("Codingal has completed 2 years")
else:
  print("Codingal was started in 2020")
print("Codingal is the best platform for kids to learn coding at a young age")

Codingal was started in 2020

Codingal is the best platform for kids to learn coding at a young age

Best Coding Course for K12 Kids

Conclusion

Statements can be skipped or repeated because control structures determine which statements in a program are performed and in what sequence.

Examples of control structures that allow statements to be skipped or executed conditionally include the if, if/else, and if/elif/else statements. We have discussed the syntax, flowchart, and examples of nested if-else and if-elif-else code blocks.

Python is the best programming language to start with. Codingal offers Python for kids course to help your child enhance cognitive, logical, and computational skills. We provide 1:1 live interactive online coding classes for kids with expert coding instructors. Along with lifetime access to course content and downloadable learning resources, to cater to every child’s need by providing a personalized journey.

Coding for kids has many beneficial advantages that develop cognitive abilities as well as enhances communication skills, entrepreneurship skills, and stimulates creativity.

Sign up to our newsletter

Get the latest blogs, articles, and updates delivered straight to your inbox.

Share with your friends

Try a free class