If Elif Else Python Tutorial




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. Here's an example:

x = 5
y = 10
z = 22

if x > y:
    print('x is greater than y')
elif x < z:
    print('x is less than z')
else:
    print('if and elif never ran...')

Here, we ask if x is greater than y first. 5 is not greater than 10, so this is False. So the elif runs to ask if x is less than z. In this case, it is asking if 5 is less than 22. It is, so we will see a print out saying x is less than z. The "else" part of this will not run.

x = 5
y = 10
z = 22

if x > y:
    print('x is greater than y')
elif x > z:
    print('x is greater than z')
else:
    print('if and elif never ran...')

Here, we ask if x is greater than y first. 5 is not greater than 10, so this is False. So the elif runs to ask if x is greater than z. In this case, it is asking if 5 is greater than 22. This is false, so it does not run. Then, we find ourselves at the else statement, which notifies us that 'if and elif never ran...'


There exists 5 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.

The next tutorial:





  • Python Introduction
  • Print Function and Strings
  • Math with Python
  • Variables Python Tutorial
  • While Loop Python Tutorial
  • For Loop Python Tutorial
  • If Statement Python Tutorial
  • If Else Python Tutorial
  • If Elif Else Python Tutorial
  • Functions Python Tutorial
  • Function Parameters Python Tutorial
  • Function Parameter Defaults Python Tutorial
  • Global and Local Variables Python Tutorial
  • Installing Modules Python Tutorial
  • How to download and install Python Packages and Modules with Pip
  • Common Errors Python Tutorial
  • Writing to a File Python Tutorial
  • Appending Files Python Tutorial
  • Reading from Files Python Tutorial
  • Classes Python Tutorial
  • Frequently asked Questions Python Tutorial
  • Getting User Input Python Tutorial
  • Statistics Module Python Tutorial
  • Module import Syntax Python Tutorial
  • Making your own Modules Python Tutorial
  • Python Lists vs Tuples
  • List Manipulation Python Tutorial
  • Multi-dimensional lists Python Tutorial
  • Reading CSV files in Python
  • Try and Except Error handling Python Tutorial
  • Multi-Line printing Python Tutorial
  • Python dictionaries
  • Built in functions Python Tutorial
  • OS Module Python Tutorial
  • SYS module Python Tutorial
  • Python urllib tutorial for Accessing the Internet
  • Regular Expressions with re Python Tutorial
  • How to Parse a Website with regex and urllib Python Tutorial
  • Tkinter intro
  • Tkinter buttons
  • Tkinter event handling
  • Tkinter menu bar
  • Tkinter images, text, and conclusion
  • Threading module
  • CX_Freeze Python Tutorial
  • The Subprocess Module Python Tutorial
  • Matplotlib Crash Course Python Tutorial
  • Python ftplib Tutorial
  • Sockets with Python Intro
  • Simple Port Scanner with Sockets
  • Threaded Port Scanner
  • Binding and Listening with Sockets
  • Client Server System with Sockets
  • Python 2to3 for Converting Python 2 scripts to Python 3
  • Python Pickle Module for saving Objects by serialization
  • Eval Module with Python Tutorial
  • Exec with Python Tutorial