Understanding Control Flow: Essential Python Statements

In the previous blog, we learned about Variables and Datatypes in Python. This blog will cover understanding control flow which are essential Python Statements.

Control flow statements are an essential concept in programming that allow developers to dictate the order in which code is executed.They enable the creation of complex logic by controlling whether certain blocks of code are run based on specific conditions or by repeating sets of instructions. In Python, control flow statements can be broadly categorized into conditional statements, looping statements, and control statements for loops.

What Are Control Flow Statements?

Control flow statements determine the direction or flow of execution in a program. They allow the programmer to specify conditions that will influence which parts of the code run and how many times. This flexibility is crucial for building dynamic applications that respond to user input, manage state, and perform repetitive tasks.

Types of Control Flow Statements in Python

1. Conditional Statements

Conditional statements execute different blocks of code based on whether a specified condition evaluates to true or false. The primary types of conditional statements in Python are if, elif, and else.

The IF Statement

The if statement is used to validate the condition and if the condition is satisfied only, it will execute that block of code otherwise it will not execute.

Syntax:

if condition:

    # Code to execute if the condition is true

Example The If Statement:

In this example, we will use the If statement to decide a person’s eligibility to vote. If the person’s age is greater than 18 years, then he/she is eligible to vote.

The code below is written in command prompt (cmd). If you are new to Python , learn how to write your first Python program.

The IF control statement in Python.

In the above example, we have declared a variable “age” and initialize it with the value of 20. Then we wrote the If condition to check if age is greater than 18. If the age is greater than 18, we printed “Elligible to Vote”. Learn more about variables and print statement.

IndentationError in Python: As you type your code in Python, you may encounter an IndentationError. This occurs because Python requires consistent indentation within the same block of code. If you receive this error, ensure that you use the same amount of space before the print statement so that Python understands that the print command is specific to the if block. Also, remember to include a colon(:) at the end of the if statement.

The Else Statement:

The else statement is used to run another block of code when the if condition is not satisfied. Mostly else statement is used in combination with the if statement.

Syntax:

if condition:

    # Code if the condition is true

else:

    # Code if the condition is false

Example

We will use an example similar to the previous one. In this example, we will set age = 15 and check the condition age > 18. If age is greater than 18, we will print ‘Eligible to vote.’ If the condition is not met, we will print ‘Not eligible to vote.”

The IF and Else control statement in Python.

In the above example, the else: statement is used to print “Not Eligible to Vote” when the first condition failed.

The elif statement

The elif statement (short for Else If) is used to check multiple conditions. If the first condition evaluates to false, it checks the next one. It executes a block of code as soon as one of the conditions is met.

Syntax

if condition1:

    # Code if condition1 is true

elif condition2:

    # Code if condition2 is true

else:

    # Code if all conditions are false

Example:

We use the previous example with an added condition: “People with an age greater than 18 or who are citizens can cast their vote.” Here, we need to check for two conditions, which can be done using the elif statement. The initial if statement will validate whether a person’s age is greater than 18, while the elif statement will check whether the person is a citizen.

The elif control statement in Python.

Note: In Python, == is a comparison operator used to check if two values are equal. If the values are the same, it returns True; otherwise, it returns False.

Using Logical Operators

In Python, logical operators, are used to perform logical operations that can yield Boolean values, either True or False. These statements are essential for controlling the flow of the program, especially when combined with conditional statements like if, elif, and else. The primary logical operators in Python are AND, OR, and NOT.

The And Operator

The and operator returns True if both conditions are true. If either condition is false, it returns False.

Syntax

if condition1 and condition2:

    # Code to execute if both conditions are true

Example:

Consider the previous example: the conditions to cast a vote are “age greater than 18 and having a registered voter ID.” In this case, a person can cast a vote only if both conditions are true. This can be accomplished using the “and” operator.

The AND logical operator in Python.

The OR operator

The or operator is a logical operator that combines two or more conditions. It returns True if at least one of the conditions is true; otherwise, it returns False. This operator is useful for constructing compound conditional statements where more than one criterion can lead to a successful outcome.

Syntax

if condition1 or condition2:

 # Code to execute if at least one condition is true

Example:

We will use an example similar to the previous ones. In this case, the conditions to cast a vote are either age greater than 18 or citizenship equal to ‘Yes.’ We can use the or operator to evaluate these conditions.”

The OR Logical operator in Python.

The Not Operator

The “Not” operator can be used to reverse the result of the condition it is applied to. This is useful in situations where you want to check if a condition is not met.

Syntax

if not condition:

    # Code to execute if the condition is false

Example:

To find the people who are not eligible to vote based on age criteria, we simply need to reverse the condition, meaning that the age should not be less than 18. This can be achieved using the “Not” operator.

The NOT logical operator in Python.

Conditional statements are essential for making decisions in the code. They allow us to control the program’s behavior based on different conditions. By experimenting with these statements, you will become comfortable using them in programming. If you have any questions, please mention them in the comments section.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.