Basic Conditional Operations in Python 3

Basic Conditional Operations in Python 3

Saturday, April 9, 20225 min read

As humans, we almost always have to make decisions. Be it buying a car, deciding whether to go to a party, or even deciding whether to go to a movie. These all decisions majorly result in a Yes or a No.

If you are interested in watching a video tutorial in Hindi, you can watch it here.

What are conditional statements?

Be it Python or any other programming language, conditionals are an essential part of programming. They are the basis for making decisions in programming. When we code a program, we have to drive the user flow according to the conditions that we have set.

Example of conditions in programming

These are some of the examples we have faced multiple times in our life.

  • When you log in to your account, you have to enter your username and password. If you enter the correct username and password, you will be able to access your account. The server checks the correctness of the username and password using conditional statements.

  • When you withdraw money from your bank account, you have to enter the amount you want to withdraw. If you enter the correct amount, you will be able to withdraw the money from your account. Also, your PIN is checked using conditional statements.

Conditional Statements in Python

A condition is a statement that evaluates to True or False.

  • Syntax and examples of condition:

If Statement

To check conditions in Python, we use the if statement.

  • Syntax

  • Example

  • Compare the code with C Program to understand the difference.

  • Here as you can see that in C, we define the scope of the condition using {}. In Python, we define the scope by giving proper indentation. The indentation is important because Python considers the code as a block of code, and only that block of code will be executed when the condition is true.

Else Statement

The if block is executed when the condition is true. If the condition is false, then the else block is executed.

  • Syntax

  • Example

Elif Statement

In case we have multiple options for a single decision, we can use the elif statement also called as ladder if

  • Syntax

  • Example

  • If one of the conditions is true, then only that block of code is executed. All the other blocks are ignored.

Nested If Statement

If we have multiple conditions which are dependent on each other, we can use the nested if statement.

  • Syntax

  • Example

Additional Resources

  • Starting from Python 3.10, there is a new feature called match case. It is similar to the switch case in C.

  • It is a replacement of the if-elif-else statement, but it checks the equality of the condition and pattern matching.

  • Example of switch in C

  • Same example in Python

  • It is a relatively new feature in Python. You can read more about it here

Conclusion

Great job! You made it to the end of the article. You have learned the basics of conditional statements in Python. Do share this knowledge with your friends and colleagues.

Read Next Here