Python Condition & Branching:- The elif statement

by | Nov 24, 2020 | Python | 0 comments

In this Basic Python Tutorials Our Topic of Discussion will be Python Condition & Branching.

First Let us start with




Comparison operations compare some value or operand. Comparison operators compare two values and evaluate down to a single Boolean value. Boolean data type has only two values: True and False. it has same functionality as in other programming languages. If our conditions doesn’t match result is Boolean data type ‘False’ and it it matches it is ‘True’.

When comparing two values We can use these operators:

  • equal: ==
  • not equal: !=
  • greater than: >
  • less than: <
  • greater than or equal to: >=
  • less than or equal to: <=

= is for assignment but not a operator.



x==10 what will be result?

We can use these operators on any data type.

Mixing Boolean and Comparison Operators

Since the comparison operators evaluate to Boolean values, you can use them in expressions with the Boolean operators.
Recall that the and, or, and not operators are called Boolean operators because they always operate on the Boolean values True and False. While expressions like 4 < 5 aren’t Boolean values, they are expressions that evaluate
down to Boolean values. Try entering some Boolean expressions that use comparison operators into the interactive shell.

>>> (10.5 < 11) and (8 < 9)

>>> (4 < 5) and (11 < 112)

>> (1 == 1) or (2 == 3)


For the choice of selection or favorable results or For Putting some restrictions in form of conditions we have Branching in Python.

if i am right you are wrong

else you are right.

if you have 60 dollars you are allowed to come in this market or get lost!

I follow Python for Data Science course at They have neatly covered it-

Branching allows us to run different statements for a different input. It’s helpful to think of an if statement as a locked room, if the statement is true you can enter the room and your program can run some predefined task if the statement is false your program will skip the task.

For example, consider the blue rectangle representing an ACDC concert. If the individual is 18 or older, they can enter the ACDC concert. If they are under the age of 18 they cannot enter the concert. We have the if statement, we have the expression that can be true or false, the brackets are not necessary. We have a colon within an indent, we have the expression that is run if the condition is true. The statements after the if statement will run regardless if the condition is true or false.


#expression that can be true or false
if age>18:

#within an indent, we have the expression that is run if the condition is true
print(“you can enter” )

#The statements after the if statement will run regardless if the condition is true or false
print(“move on”)

The else statement

it will run a different block of code if the same condition is false. Let us use the ACDC concert analogy again, if the user is 17 they can not go to the ACDC consort, but they can go to the Meatloaf concert.

The syntax of the else statement is similar; we simply append the statement else statement at with the new condition. You can try changing the values of age to see what happens.

if age>18:
    print("you can enter" )
    print("go see Meat Loaf" )
print("move on")

elif Statements

While only one of the if or else clauses will execute, you may have a case where you want one of many possible clauses to execute. The elif statement is an “else if” statement that always follows an if or another elif statement.
It provides another condition that is checked only if any of the previous conditions were False. In code, an elif statement always consists of the following:-
• The elif keyword
• A condition (that is, an expression that evaluates to True or False)
• A colon
• Starting on the next line, an indented block of code (called the elif

Join Today

Know more about latest trends, News in Field of Data Analytics.

We will offer Free SAS and Python Programme in Data Science & enhance your understanding of data analysis.

By rlochan2021

We offer Everything Free here and always will be, so joining is Risk Free and Always Cost Free.

Check Out These Related Posts

Functions in Python Programming for Data Science

I am really impressed with two online courses one is “Analytics in Python” at edX by ColombiaX and another is “Python for Data Science” at or (Previously Bigdatauniversity). So i am going to document what ever i will learn about functions in these two courses. First there are Built-in functions(Ready made for use), such […]

read more

String Functions in SAS and Python

I always believe SAS and Python can make a great team together for Data Scientists. So Why Now we study them together. In this Post we will discuss various String functions in SAS and Python. String Functions in Python 3 Hope you must have gone through my previous post “Python Programming- Strings explained“. len(): returns […]

read more



Submit a Comment

Your email address will not be published. Required fields are marked *