Loading...

How to use Python If Else statements

What are conditional statements? How to use Python If-Else statements?

This blog gives in-depth knowledge about the use of Python If-Else statements. Read our blog to learn more...

How to use Python If Else statements

Aug 28, 2022    By Team YoungWonks *

Introduction

Computers were created to make humans' lives easier by performing complex operations in less time. Because computers can only understand machine language, instructions must be given in a specific manner.

Programming languages were created to provide these instructions in a way that the computer could understand. A programming language is a set of commands written in a specific way that are followed by a computer. Python, C++, Java, C#, JavaScript, and other programming languages are available.

In this tutorial, we will learn in depth how to use conditional statements in the Python programming language. The concept of conditional statements is the same across all programming languages; the only difference is the syntax. Syntax is a predefined way of writing commands. Syntax varies from programming language to programming language.

 

What are Conditional Statements?

The use of conditional statements allows the computer to take specific action in response to a condition. The computer checks the conditions and converts their result to a boolean value, which it will understand as either true or false.

For example, my parents won't let me play soccer on the playground if it's raining. As a result, we make our decision on the weather to determine whether or not I can play soccer.

As we read the above example, it becomes clear that we also make our decisions in real life on a condition. Similar to this, we must write these conditions in a language that the computer can understand in order for it to understand them. Technically speaking, these conditions are referred to as conditional expressions or conditional statements.

If we concentrate on the above example, we see that we have two choices: we can play or we can choose not to play, which is to say, it's a yes or no. In computer language, yes and no are true and false, respectively.

The action specified by the user is carried out based on the evaluated result of the conditional expression. Depending on the requirement, different keywords are used to write the conditions such as if, elif, and else. These are also known as the logical blocks of programming. We will go over these in greater detail below.

 

If Statements

A program's first condition is always specified in an if statement. It enables the computer to use the specified criteria for decision making. Let’s look into the block of code given below to understand this better.

Syntax:

if (expression):

          statements to be executed

The syntax above designates the if block. When the if condition evaluates to True, the statements contained in the condition will be executed. Indentation is the term for when there are white spaces at the beginning of a line. The usage of indentation when generating code is one of the fundamental elements for Python programming.

Let's examine the flowchart illustration and the following example. The flowchart shows the control flow of the conditions.

python if pseudocode

python if flowchart

python if example

If it's sunny, the flowchart tells you to put on your shades; otherwise, the program ends.

In the illustration, we use the user's input to advise the user to put on shades if it's sunny outside.

 

Else statements

The optional statements after the conditions are known as the else clause. It is constructed using an if statement. If the if statement returns False, the else condition is carried out.

Syntax:

if (expression):

          statements to be executed

else:

          statements to be executed

python if else pseudocode

python if else flowchart

python if else example

If it's snowing, the flowchart tells you to put on your sweatshirt; otherwise, the wear a t-shirt.

In the illustration, we use the user's input to advise the user to put on a t-shirt or sweatshirt based on the weather.

 

Multiple if statements

Multiple if statements are used when you want to check all the conditions. Even if the previous if statement was True, other if statements will still be checked. It is not dependent on the result of the previous condition.

When you wish to verify every condition, you use multiple if statements. Other if statements will still be examined even if the previous if statement returned True. It is independent of how the prior condition turned out.

python multiple if pseudocode 2

python multiple if example 2

python multiple if example 1

In the illustration and flowchart we see multiple conditions and all of them will be checked.

 

Elif statements

Other names for Elif include else-if. When it is necessary to add multiple conditions, elif statements are utilised. Only after the if condition can this condition be added. The next conditions in the if-elif block are not checked when a specific condition is True. When the if condition becomes False, the elif condition is examined. After the if statement, multiple elif statements may be introduced.

Syntax:

if (expression):

       statements to be executed

elif (expression):

       statements to be executed

Lets take a look at the flowchart and illustration to understand the logic.

The two conditions are shown in the following code. Wear a cap if it's sunny; a raincoat if it's raining.

python if elif pseudocode

python if elif flowchart

python if elif example

if-elif-else Block Code

Either of the statements in the if/else clause can be the final condition. If none of the above conditions are True, else clause is executed.

Syntax:

if (expression):

        statements to be executed

elif (expression):

         statements to be executed

else:

         statements to be executed

Let’s take a look at an if-elif-else statement examples given below:

python if elif else pseudocode

python if elif else flowchart

python if elif else example

The above code states, if its sunny wear shades, else if the weather is rainy carry an umbrella else wear a cap.

 

Elif-else block with multiple if conditions

Elif-else condition works in a group along with an if statement. Only if the above if condition is False, these are checked. However, each if block is checked in conditional statements.

python multiple if example

There are three if blocks in the above example, as can be seen. The first one has a single if statement, the second one has an elif statement, and the third one has an else statement. Since each of the three blocks in this case has its own if block condition, they will all be examined.

python multiple if flowchart

python multiple if pseudocode 1

Even in the example given above, there are two if blocks and both will be checked.

 

Nested if conditions

These conditions form a nest. Nested if conditions are conditions inside conditions. Only when the if condition returns True is the condition within examined.

Syntax:

if (expression):

       if (expression):

             statements to be executed

       else:

            statements to be executed

Let us understand the pseudocode, flowchart and the following example:

Example 1:

python nested if pseudocode

python nested if flowchart

Example 2:

python nested if flowchart 1

python nested if example

The user input to choose between eat and play are given. If the user choses to eat then the choice of cuisine is taken and based on the input the conditions are met.

 

Ternary operator in Python

This a single line condition written instead of writing a multi-line if-else block.

Syntax:

(on-true) if statement else (on-false)

When the if statement is true, the on-true statement is executed else the on-false statement if executed.

 

Conclusion

We learned about all the different kinds of conditional statements used in the Python programming language in this tutorial. The building blocks of coding are conditional expressions. You can use conditional statements inside strings, functions, or iterable loops like while or for loops, now that you have learned how to do so.

Mastering Control Flow in Python with If-Else Statements

For young learners keen on deepening their understanding and application of Python, our Coding Classes for Kids at YoungWonks offer a nurturing environment to explore and master programming concepts. Our specialized Python Coding Classes for Kids are designed to equip students with the foundational skills needed to tackle more complex programming challenges, such as those involving conditional logic with if-else statements. Additionally, for students interested in exploring the integration of software with hardware, our Raspberry Pi, Arduino, and Game Development Coding Classes provide hands-on experience, further enhancing their understanding of how software can interact with and control the physical world. Join us on this fascinating journey to become proficient in Python and beyond, laying the groundwork for future innovation and creativity in technology.

*Contributors: Written by Aayushi Jayaswal; Lead image by Shivendra Singh

This blog is presented to you by YoungWonks. The leading coding program for kids and teens.

YoungWonks offers instructor led one-on-one online classes and in-person classes with 4:1 student teacher ratio.

Sign up for a free trial class by filling out the form below:



By clicking the "Submit" button above, you agree to the privacy policy
Share on Facebook Share on Facebook Share on Twitter Share on Twitter
help