If Statements

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..

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments

Saving Progress...

Sign up for free!

Sign up for free and get instant access to this course!.

Python 3 for Beginners

0%

0/1 Lessons

Installing Python on Windows

• 1hr 17min

0 / 4 lessons complete

Python Basics

• 28min

0 / 7 lessons complete

Python Variables

• 41min

0 / 8 lessons complete

Even more Python Variables!

• 41min

0 / 6 lessons complete

Conditional Statements

• 15min

0 / 3 lessons complete

Writing Functions

• 30min

0 / 5 lessons complete

Python Loops

• 23min

0 / 5 lessons complete

Python PIP and Modules

• 18min

0 / 4 lessons complete

RegEx

• 26min

0 / 4 lessons complete

Working with APIs

• 12min

0 / 3 lessons complete

Course Conclusion

• 2min

0 / 1 lessons complete