coding180 icon
Coding180
1 - Python Control flow

1 - Python Control flow

Home > Tutorials > Python for beginners > Python Conditional Looping


In Python, control flow refers to the order in which the instructions in a program are executed. This is determined by the combination of conditional statements and loop statements used in the program.

In Python, there are two main types of control flow:

  1. Conditional statements: These statements are used to control the flow of the program based on certain conditions. Examples of conditional statements include if, elif, and else.

  2. Loop statements: These statements are used to repeat a block of code multiple times. Examples of loop statements include for and while.

Together, these control flow statements allow you to create more complex and flexible programs in Python.

Conditional Statement

  1. if: This statement is used to evaluate a condition and, if the condition is true, execute a block of code.

  2. elif: This statement is short for "else if" and is used to check for additional conditions if the previous conditions are not met.

  3. else: This statement is used to specify a block of code to be executed if none of the previous conditions are met.

The if statement is used to evaluate a condition and, if the condition is true, execute a block of code. For example:

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

The elif statement is short for "else if" and is used to check for additional conditions if the previous conditions are not met. For example:

if condition1:
    # code to execute if condition1 is true
elif condition2:
    # code to execute if condition1 is not true but condition2 is true

Finally, the else statement is used to specify a block of code to be executed if none of the previous conditions are met. For example:

if condition1:
    # code to execute if condition1 is true
elif condition2:
    # code to execute if condition1 is not true but condition2 is true
else:
    # code to execute if neither condition1 nor condition2 are true

Overall, conditional statements in Python provide a way to control the flow of your program, allowing it to make decisions and execute different branches of code depending on the conditions that are evaluated.

Looping

In Python, there are two main types of loop statements:

  1. for: This statement is used to iterate over a sequence of elements, such as a list or a string. It allows you to execute a block of code multiple times, once for each element in the sequence.

  2. while: This statement is used to repeat a block of code until a certain condition is met. It continues to execute the code as long as the condition is true.

Looping Syntax

In Python, there are two types of loop statements: for loops and while loops.

A for loop is used to iterate over a sequence of elements, such as a list or a string. The syntax for a for loop in Python is:

for item in sequence:  
    # code to be executed for each item in the sequence

For example, to print each element in a list, you could use a for loop like this:

my_list = [1, 2, 3, 4]
for item in my_list:
    print(item)

This would print the numbers 1, 2, 3, and 4, each on a separate line.

A while loop, on the other hand, is used to execute a block of code repeatedly until a certain condition is met. The syntax for a while loop in Python is:

while condition: 
    # code to be executed repeatedly

For example, to print the numbers from 1 to 10, you could use a while loop like this:

i = 1
while i <= 10:
    i += 1
print(i)

This would print the numbers 1 through 10, each on a separate line. The i += 1 expression is used to increment the value of i by 1 on each iteration of the loop, so that the loop eventually terminates when the value of i becomes greater than 10.

In the following lessons in this chapter, we will talk more on control flows with examples.


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel