Course Content

If Else & Elif Conditionals In Python

Let's start today's topic of “If else and elif in python”, with a definition:

“If, else and elif statement can be defined as a multiway decision taken by our program due to the certain conditions in our code.”

For few viewers, the term “elif” is new as they are not familiar with the word and it is also not like most of the other words such as list or loops, etc. that have the same meaning in the English language and in Python programming. In fact, in English “elif” means honest. But if you have ever done programming in any language, you must be familiar with “else-if” statement, well “elif” is just that.

Now coming towards a more formal sort of description. “If and else” are known as decision-making statements for our program. They are very similar to the decision making we apply in our everyday life that depends on certain conditions. The everyday example is thoroughly explained in the tutorial so I will not waste your time on that, instead, I would now like to focus on more technical details.

Let us focus a little on the working:

            Our compiler will execute the if statement to check whether it is true or false now if it’s true the compiler will execute the code in the “if” section of the program and skip the bunch of code written in “elif” and “else”. But if the “if” condition is false then the compiler will move towards the elif section and keep on running the code until it finds a true statement(there could be multiple elif statements). If this does not happen then it will execute the code written in the “else” part of the program.

            An “if” statement is a must because without an if we cannot apply “else” or “else-if” statement. On the other hand else or else if statement are not necessary because if we have to check between only two conditions we use only “if and else” and even though if we require code to run only when the statement returns true and do nothing if it returns false then an else statement is not required at all.  

Now Let’s talk about some technical issues related to the working of decision statements:

  • There is no limit to the number of conditions that we could use in our program. We can apply as many elif statements as we want but we can only use one “else” and one “if” statement.
  • We can use nested if statements i.e. if statement within an if statement. It is quite helpful in many cases.
  • Decision statements can be written using logical conditions which are:
  • Equal to
  • Not equal to
  • Less than
  • Greater than
  • Greater than equal to
  • Less than equal to

We can also use Boolean or our custom-made conditions too.

Bonus part:

            As we know that an “if” statement is necessary and you can’t have an “else” or “else-if” without it, but let’s suppose you have a large amount of code and for some reason, you have to remove the “if” part of the code (because maybe your code is better without it) but you do not want to do lots of coding again. Then the solution is just to write pass instead of the code and this will help your code run without any error without executing the if part.

Code file as described in the video

# var1 = 6
# var2 = 56
# var3 = int(input())
# if var3>var2:
#     print("Greater")
# elif var3==var2:
#     print("Equal")
# else:
#     print("Lesser")

# list1 = [5, 7, 3]
# print(15 not in list1)
# if 15 not in list1:
#     print("No its not in the list")

# Quiz
print("What is your age?")
age = int(input())
if age<18:
    print("You cannot drive")

elif age==18:
    print("We will think about you")

else:
    print("You can drive")



Comments(18)

ROHIT@123 1 year ago
thx biyaa
zadrain 8 months, 1 week ago
age = int(input("enter your age"))
if age <=4:
   print("enter logical age")
elif age >=100:
   print("enter logical age")
elif age > 18:
   print("you ca drive")
elif age==18:

   print("we cant decide")


else:
   print("you cant")
anubhavs921 7 months, 4 weeks ago
the answer of challenge given by you sir:
print "what is your age"
age = int(input())
if ((age<7) or (age>100)):
    print "you entered wrong age"
elif age<18:
    print "you can not drive"
elif age==18:
    print "We will think about you"
else:
    print " you can drive"
omkarrege 7 months, 2 weeks ago
not show description
sanayasingh 7 months, 1 week ago
print("What is your age")
age=int(input())
if age<18:
    print("You cannot drive")
elif age==18:
    print("We will think about you")
elif age>100:
    print("over age")
else:
    print("You can drive")
Vivek-Mishra 5 months, 1 week ago
Full Notes and Source Code ::::::::::} https://github.com/Optimized-World/Complete-Python-Tutorial-and-Notes |::::::::::::| Thanks, Harry Bhai for Awesome Tutorials
Mitesh21 5 months ago
age = int(input("Enter the age:"))
if age>=18:
    print("you are eligible for 2 wheeler licence")
    if age>=20:
        print("you are eligible for 4 wheeler licence")
    else:
        print("you are not eligible 4 wheeler licence")
else:
    print("you are under age")
dpkpdy 4 months, 2 weeks ago
print("What is your age?")
age = int(input())

if age >= 100:
    if age == 100:
        print("can drive")
    else:
        print("Out of age")


if age <= 18:
    if age == 18:
        print("We can  not decide, please come to us and pass the driving test")
    else:
        print("You can not drive")
rahul87 4 months, 2 weeks ago
print("What is your age?")
age = int(input())

if 10 < age < 100:
    x=age

    if x<18:
        print("You cannot drive")

    elif x==18:
            print("We will think about you")

    else:
                print("You can drive")

else:
                    print("sorry invalid age")
mnvjhamb 4 months, 1 week ago
age = int(input("please enter your age " , ))
print(age)


if age<18:
    print("you can't drive until you are 18")
elif age == 18:
    print("we can't decide")
else:
    print("you are eligible to drive")
priyanshg 4 months, 1 week ago
Thanks bhaiya
sud71 3 months, 2 weeks ago
Eage = int(input("Enter Age"))
if Eage > 18:
    print("you can apply for License")
elif Eage == 18:
    print("Let us Think")
else:
    print("underage")
kunjshah 3 months, 2 weeks ago
print("Enter your age")
age=int(input())
if age>18:
    print("You can start driving")
elif age==18:
    print("You should come to center for further process")
else:
    print("Not eligible for driving")
ankit100 3 months, 1 week ago
Sir aap files empty kue chodne lage hai??
maruf512 3 months ago
age = int(input("What is your age ? "))

if age>18 and age<91:
    print("You can drive.")
elif age<18:
    print("You can't drive")
elif age==18:
    print("We will think about you")
elif age>90:
    print("Over age")
else:
    print("Sorry we can't understand")
jit87 2 months, 1 week ago
age = int(input("Enter your age:"))

if age<7:
	print("Not valid")
	
elif age>100:
	print("Not valid")
	
elif age>18:
	print("You can drive")

elif age==18:
	print("Come for physical test")
	
else:
	print("You cannot drive")
jit87 2 months, 1 week ago
age = int(input("Enter your age:"))

if age<7:
	print("Not valid")
	
elif age>100:
	print("Not valid")
	
elif age>18:
	print("You can drive")

elif age==18:
	print("Come for physical test")
	
else:
	print("You cannot drive")
vishv96 2 months, 1 week ago
print("what is your age?")
age = int(input())
if (age<=7) or (age>=100):
    print ("sorry, please enter your correct age:")
    age = int(input())
    if age>18:
        print("you are able to drive")
    elif age==18:
        print("you have need special permission")
    else:
        print("you are not eligible")

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here