Python 3 Programming Tutorial: If Elif Else




Now we bring the in “elif” statement. The elif allows us to tie multiple if statements together as we might have intended to before with multiple if statements before we learned that the else will only be contingent on the if statement above it.

The “elif” statement is a hybrid of the else and the if. The way it works is:

We ask if something is the case. If it is, then the elif and the else will not run. If it is not, then the elif will run and question the if statement. If it is True, then it will run and the else will not run. If it is not true, then the else statement will run.

Sample code for this basics series:

Python 3 Programming tutorial Playlist:

In this python 3 programming tutorial video, you are shown how to use the if elif else statements.

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6

source

40 thoughts on “Python 3 Programming Tutorial: If Elif Else”

  1. Does anyone know if you can put an elif statement as your first logic test or does it have to come after an if statement?

  2. # Using If, Elif, Else

    def rerun_gender (): # Defines rerun_gender
        gender ()        # Runs the program called gender 

    def gender ():                        # Defines gender
        print ("Are you Male or Female?") # prints "Are you Male or Female?"
        choice = input ("")               # Lets you answer

    # The thing below means if any of those become True, it will activate the thing its ment to.
    # True + True = True, True + False = True, False + False = False, ("This doesnt matter how many True/Falses there are, in an
    # Or statement, aslong as 1 is true, it activates.

        if choice == 'm' or  choice == 'M' or choice == 'male' or choice == 'Male' or choice == 'MALE': # If choice == (Then the thing in '')
            print ("You are male!")                                                                     # prints "You are male!"

        elif choice == 'f' or choice == 'F' or choice == 'female' or choice == 'Female' or choice == 'FEMALE': # Or if choice == (Then the thing in '')
            print ("You are female!")                                                                          # prints "You are female!"

        else:                                           # If your response didnt match/fit in to any of the above:
            print ("Invalid Reponse!, answer with M/F") # prints "Invalid Reponse!, answer with M/F"
            rerun_gender ()                             # Runs rerun_gender

    gender () # Runs the program called gender

  3. Is this function the way Pathon implements the "case" instruction and this means "case" instruction does not exist?

  4. I'm a Network Engineer starting my Python class next week and just warming up. I have seen hundreds of hours of videos on different Microsoft and networking topics from companies such as TrainSignal and CBT Nuggets over the years and I must say your training is excellent.

    Videos are short and informal. Keep up the good work!

  5. I`m digital compositor and colourist works with Nuke and Davinci Resolve. It`s very handy for me to know Python. This videos are perfect for starting. Just for one week, and now I make my custom tools and gizmos in Python. Thank you!

  6. Hi ,
    I am new to Python. I find your videos are great starting point for learning. My question is regarding conditional statements.
    Why there are no switch statements in Python?

  7. so im new to this programming stuff with python too. I have an assignment that I need to do it wants me to determine the season by the month. my teacher wants me to use this if, elif, else statements how would I do that?please help me oout

  8. Thanks for your wonderful videos. A little question. If I want to see the outcome of both in case of they both are right statements, and outcome of rejection in the case of they both are false statements? how can I manage this problem? thanks in advance.

  9. Thanks a lot for sharing the python video tutorials

    i have i question can we use for loop and else condition at a time.

  10. for example, if we take a program to find whether a year is a leap or not. We need 2 3 conditions to be true and then go ahead. So, in that case are we not supposed to use these statements to code? If my first condition is true it won't go to next!!

  11. how to write a programe having some working hours and salary of 50 workesr and effeciency with respect to time then we add 2500 bonous to the salary of one worker which works more effeciently???????
    plz plz help me out

  12. import re

    print("Our Magical Calculator")
    print("Type 'quit' to exitn")
    previous = 0
    run = True

    def performMath():
    global run
    global previous
    equation = ""
    if previous == 0:
    equation = input("Enter equation:")
    else:
    equation = input(str(previous))
    #How do I set the equation to 0 so that it is set as "Enter equation"?<<<<<<<<<—-

    if equation == 'quit':
    print("Goodbye vro!")
    run = False
    else:
    equation = re.sub('[a-zA-Z,.:()" "]', '', equation)

    if previous==0:
    previous = eval(equation)

    else:
    previous =eval(str(previous)+ equation)

    while run:
    performMath()

  13. I like your tutorials- they are easy to understand. However, I go a question. I built this model
    def new_age():
        new_age= float(input("Enter your age: "))
        age1= 21
        if age1 <= 20:
           print ("You are too you")
        elif age1 >=21:
           print ("You are old enough")
        
    new_age()

    But regardless of the number I put in it still gives me the message "you are old enough. Why ?  I'd expected this function to give me "you are too young" if I put a number below 21. Can you help me ?

  14. I'm a 21 year old computer science student from Kolkata, India..
    Awesome video as usual, that's why I subscribed ages ago!!!
    Keep up the good work bruh! God bless you! ♥ ♥♥

Leave a Reply