codescracker


python

Python if else elif Statements



« Previous Tutorial Next Tutorial »


Here you will learn all about if, if-else, elif, nested if statements in python. Let's first start with if statement in python.

Python if Statements

The if statement in python, consists of a boolean expression followed by statement(s). Here is the general form to use if statement in your python program:

if expression:
	statement(s)

Example

Here is an example demonstrates the if statement in python:

# Python if Statement - Example Program

num1 = 20
num2 = 0

if num1:
    print("num1 has some value which is ", num1);

if num2:
    print("num2 has some value which is", num2);

print("exiting...");

Here is the sample output of the above python program:

python elif statement

Let's take another example on if statement in Python. This is password protection program using if statement.

# Python if statement Example - Password protection
print("Welcome to Python if Statement Example.");
print("You are here at codescracker.com");
pswd = input("Enter the password:");
if pswd == "pythonAtCodescracker":
    print("Access Granted.");

Here is the sample run of the above example of if statement using python.

if statement example python

Now enter the password as pythonAtCodescracker and press enter button, you will see the following output.

if statement in python

Python if-else Statement

The if-else statement in python, is similar to the if statement, but in this case, there is an else statement available to execute the statement in case if the expression evaluates to false. Here is the general form to use if-else statement in python:

if expression:
	statement(s)
else:
	statement(s)

Example

Here is an example program, illustrates the use of if-else statement in Python:

# Python if-else Statement - Example Program

num1 = 20
num2 = 0

if num1:
    print("num1 has some value which is ", num1);
else:
    print("num1 hasn't any value");

if num2:
    print("num2 has some value which is", num2);
else:
    print("num2 hasn't any value");

print("exiting...");

Here is the sample run of the above python program:

python if statement

Let's take again the password protection example program but this time using if-else statement in python.

# Python if-else statement Example
print("Welcome to Python if-else Statement Example.");
print("You are here at codescracker.com");
pswd = input("Enter the password:");
if pswd == "pythonAtCodescracker":
    print("Access Granted.");
else:
    print("Access Denied!");

Here is the sample output of above if-else example in Python.

if else statement in python

Now enter password as pythonAtOther, and press enter to see the following output.

if else example python

And enter password as pythonAtCodescracker and press enter again to see below output.

python if else

Here is another example asking from the teacher to tell the marks obtained by the student and student replied according to his/her marks as shown in the program given below based on if-else statement in python.

# Python if else Example
print("Welcome to if-else statement of Python.");
print("\nHi Sir! How many mark i have got in Python ?");
marks = int(input());
if marks:
    print("Thanks sir, atleast i have got some mark.");
else:
    print("Oh shit, i have got no marks.");

Here is the sample output produced by the above example code of if-else statement in python.

python if-else code

Now provide the mark as 0 that the student has got and see the following output.

python if-else program

This time, provide some mark and see the message replied by the student.

if-else statement program python

Python Nested if Statement

You are free to use of if or if-else statement inside another if or if-else statement(s). Here is the general form to use nested if statement in Python:

if expression1:
	statement(s)
	if expression2:
		statement(s)
	elif expression3:
		statement(s)
	else:
		statement(s)
else:
	statement(s)

Example

Here is an example program, illustrates the concept and use of python nested if statement:

# Python Nested if Statement - Example Program

num = 10

if(num<20):
    print("num is less than 2");
    if(num==15):
        print("value of num is 15");
    elif(num == 10):
        print("value of num is 10");
    elif(num==5):
        print("value of num is 4");
    else:
        print("value of num is other than 15, 10, 4");
elif(num<5):
    print("num is less than 5");
else:
    print("num is not less than 5");
print("exiting...");

Here is the sample run of the above Python program:

python if else statement

Let's take an example generates automatic mood face with word depends on randomly generate value using if-elif-else statement in Python.

# Python if-elif-else Statement Example
# Random Mood generating program in Python
import random
mood = random.randrange(4);
if mood == 1:
    print("\
           _________\n\
          |         |\n\
          | o     o |\n\
          |   \ /   |\n\
          |         |\n\
          | .    .  |\n\
          |  `..`   |\n\
           ---------\n\
           Or\n\
          Happy");
elif mood == 2:
    print("\
           _________\n\
          |         |\n\
          | o     o |\n\
          |   \ /   |\n\
          |         |\n\
          |         |\n\
          |  ----   |\n\
           ---------\n\
           Or\n\
          Normal");
elif mood == 3:
    print("\
           _________\n\
          |         |\n\
          | o     o |\n\
          |   \ /   |\n\
          |         |\n\
          |   ..    |\n\
          | .`  `.  |\n\
           ---------\n\
           Or\n\
          Sad");
else:
    print("Mood off!!");
print("Today.");

Here are some sample run of the above if-elif-else statement example in python.

First output for sad mood:

python if elif else

Second output for normal mood:

if-elif-else example python

Third output for happy mood:

python if-else statement example

Fourth output for mood off mood:

if-elif-else statement python

More Examples

Here are the list of some more examples in python to illustrate if statement, if-else statement, and nested if or elif statement in python practically:


« Previous Tutorial Next Tutorial »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test