Python If .. Elif .. Else Statements and Conditionals

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/python-elif-else-statements-conditionals/
PINTEREST
PINTEREST
INSTAGRAM

Decision making one of the fundamentals operations in programming languages and applications. We mostly use decisions to implements applications logic. The primary mechanism for decisions in Python is if..elif..else keywords. We can simple call them if-else . In this tutorial we will look different aspects and usage  examples of if-else.

If

If is used to check whether given condition is true and run some code. So we need some condition and some code block. The syntax of if is like below.

Now we can understand if with an example better. In this example we check if 0 is less than 10 .

Because given condition returned True the code block executed and printed.

If-Elif

If we want to check multiple conditions in a single step and run code block accordingly we can use If-Elif statement. We can provide multiple conditions like below.

We can understand if-elif with an example where we check 3 conditions.

First elif is executed and related text printed.

LEARN MORE  Powershell If-Elseif-Else Conditional Statement

If-Elif-Else

As we have seen previous parts we can define limitless conditions and code blocks. There is a special conditions which is triggered when none of the previous conditions are met. We call this as else and put at the end of the if-elif code block. Else do not need any specific condition.

In this example we can guess the given number with else .

Providing Multiple Conditions

Up to now we have defined single conditions in order to check. We can also use complex or multiple conditions in a single keyword. We generally use ( ) to group multiple or complex conditions. All inner conditions are computed and at the end single boolean value true or false returned.

Providing Multiple Conditions

Providing Multiple Conditions

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/python-elif-else-statements-conditionals/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

1 Response

  1. 22/05/2017

    […] have already looked if-elif-else statements in previously. if-elif-else requires conditions in order evaluate. These conditions may simple True , False or […]

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)