If Statements
Server Academy Members Only
Sorry, this lesson is only available to Server Academy members. Create a free account now to get instant access to this and more free courses. Click the Sign Up Free button below to get access to our free courses now, or sign in if you have an account.
In this lesson, we're going to learn about if
, else if
, and else
statements in Python.
if
statements are used to execute a block of code only if a certain condition is true. Here's an example of an if
statement in Python:
x = 5
if x > 10:
print("x is greater than 10")
In this code, we first define a variable called x
and set it equal to 5
. Then, we use an if
statement to check if the value of x
is greater than 10
. If it is, then the code inside the if
block will be executed, and the message "x is greater than 10" will be printed to the screen. However, since the value of x
is not greater than 10
, the code inside the if
block will not be executed, and the message will not be printed.
We can also use else if
statements in Python to check for additional conditions if the if
condition is not true. else if
statements are written as elif
in Python. Here's an example that uses an else if
statement:
x = 5
if x > 10:
print("x is greater than 10")
elif x == 5:
print("x is equal to 5")
In this code, we first define a variable called x
and set it equal to 5
. Then, we use an if
statement to check if the value of x
is greater than 10
. Since it is not, the code inside the if
block is not executed, and the program moves on to the elif
statement.
The elif
statement checks if the value of x
is equal to 5
. Since it is, the code inside the elif
block is executed, and the message "x is equal to 5" is printed to the screen.
Finally, we can use an else
statement in Python to execute a block of code if none of the if
or else if
conditions are true. Here's an example that uses an else
statement:
x = 5
if x > 10:
print("x is greater than 10")
elif x == 5:
print("x is equal to 5")
else:
print("x is not greater than 10 and not equal to 5")
In this code, we first define a variable called x
and set it equal to 5
. Then, we use an if
statement to check if the value of x
is greater than 10
. Since it is not, the code inside the if
block is not executed, and the program moves on to the elif
statement.
The elif
statement checks if the value of x
is equal to 5
. Since it is, the code inside the elif
block is executed, and the message "x is equal to 5" is printed to the screen. However, since the elif
condition is true, the code inside the else
block is not executed, and the message "x is not greater than 10 and not equal to 5" is not printed.
To summarize, if
statements in Python are used to execute a block of code only if a certain condition is true. else if
statements, written as elif
Server Academy Members Only
Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..
Saving Progress...
Python 3 Fundamentals
Installing Python on Windows • 1hr 17min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 1 min
Downloading and Installing Python on Windows
Free Preview Lesson
Text | 8 min
Installing and configuring VS Code for Python
Free Preview Lesson
Text | 8 min
Lab: Installing Python
Free Preview Lesson
Devops Lab | 60
Python Basics • 28min
0 / 7 lessons complete
Section Overview
Free Preview Lesson
Text | 2 min
Executing Python Code
Free Preview Lesson
Text | 3 min
Python 3 Syntax
Free Preview Lesson
Text | 5 min
Help! Python Keywords
Free Preview Lesson
Text | 4 min
Printing to the console!
Free Preview Lesson
Text | 5 min
Python Operators
Free Preview Lesson
Text | 4 min
Section Review
Free Preview Lesson
Quiz | 5 min
Python Variables • 41min
0 / 8 lessons complete
Section Overview
Free Preview Lesson
Text | 4 min
Casting
Free Preview Lesson
Text | 4 min
Strings
Free Preview Lesson
Text | 5 min
Booleans
Free Preview Lesson
Text | 8 min
User Input
Free Preview Lesson
Text | 2 min
Numbers
Free Preview Lesson
Text | 7 min
NoneType
Free Preview Lesson
Text | 5 min
Assignment: Write a Mad Libs Script
Free Preview Lesson
Text | 6 min
Even more Python Variables! • 41min
0 / 6 lessons complete
Python Lists
Free Preview Lesson
Text | 8 min
Python Tuples
Free Preview Lesson
Text | 7 min
Python Sets
Free Preview Lesson
Text | 7 min
Frozensets
Free Preview Lesson
Text | 6 min
Dictionaries
Free Preview Lesson
Text | 8 min
Iterator and Iterable
Free Preview Lesson
Text | 5 min
Conditional Statements • 15min
0 / 3 lessons complete
Writing Functions • 30min
0 / 5 lessons complete
Section Overview
Free Preview Lesson
Text | 3 min
Defining Functions
Free Preview Lesson
Text | 4 min
Importing functions from another file
Free Preview Lesson
Text | 4 min
Assignment: Rock, paper, scissors
Free Preview Lesson
Text | 12 min
Assignment: Count Dictionary Words
Free Preview Lesson
Text | 7 min
Python Loops • 23min
0 / 5 lessons complete
Section Overview
Free Preview Lesson
Text | 2 min
For In Loops
Free Preview Lesson
Text | 5 min
While Loops
Free Preview Lesson
Text | 5 min
Nested Loops
Free Preview Lesson
Text | 3 min
Python Loops Challenge!
Free Preview Lesson
Text | 8 min
Python PIP and Modules • 18min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 3 min
Installing Python PIP
Free Preview Lesson
Text | 4 min
Installing Modules with PIP
Free Preview Lesson
Text | 5 min
Importing Modules
Free Preview Lesson
Text | 6 min
RegEx • 26min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 4 min
Regex 101
Free Preview Lesson
Text | 10 min
Importing Regex and manipulating strings
Free Preview Lesson
Text | 7 min
Regex Challenge!
Free Preview Lesson
Text | 5 min
Working with APIs • 12min
0 / 3 lessons complete
Making HTTP Requests
Free Preview Lesson
Text | 3 min
Working with JSON
Free Preview Lesson
Text | 5 min
Get your weather with the OpenWeatherMap
Free Preview Lesson
Text | 4 min
Course Conclusion • 2min
0 / 1 lessons complete