Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Introduction and the Python "if" Statement

Give Feedback

In this first lesson of the course, you’ll get an overview about the courses content as well as an introduction into conditional statements, why you need and when to use them. Subsequently, Python’s if-statement is introduced.

00:00 Hi there! Welcome to Python Conditional Statements on Real Python. In this video series, we’ll cover the if statement. You’ll use this a lot in your Python journey.

00:12 We’ll cover the else and elif clauses. We’ll go over one-liners and conditional expressions. We’ll cover the pass statement. And in the last section, we’ll summarize and throw together a quick program to utilize all these concepts you’re going to learn. With that being said, let’s jump into Section 1: Introduction to the if Statement.

00:34 So, here’s the thing about conditional statements. You use them every day. They typically start or have the word “if” in them. You might say things like “If it is a weekday, I drink three cups of coffee in the morning,” “If I have a free hour, I will watch some Real Python videos,” “If I eat healthy all week, I can eat cake and ice cream for every meal on Saturday. Maybe not every meal, but most of them.” And what you’re doing in real life with these conditional statements is what we call flow control.

01:08 The way we typically learn to program at the very beginning is with what’s called sequential execution. This is where commands or statements simply run one after the other—always.

01:19 But what if we want more flexibility? And you can imagine looking at this screen that maybe you don’t want to do all these things every day. Maybe you only want to do them on certain days.

01:29 And that’s where flow control and conditional statements comes in. So, our days or week will now look like this. “If it is a weekday, I drink three cups of coffee,” “If I have a free hour, I will watch Real Python videos,” “If I get through an hour of Real Python material, I could watch an episode of Game of Thrones,” and “If I ate healthy all week and it is Saturday, I’m going to eat cake and ice cream at every meal.” So you can see that this lends you some flexibility in the structure of your routine, or, in our case, our program.

02:02 Here we have the syntax for our if statements. We start with the word if. Then we need an expression to evaluate to True or False.

02:13 This expression is followed by a colon (:). And then Python will expect a statement that we want to run when the expression evaluates to True. And notice, this is very important that the statement is indented.

02:28 That indention is very important. It indicates everything that you want to run if your expression evaluates to True. So in this example, say we’re running a program and it hits this if statement.

02:42 The followed_diet variable will contain a True or False Boolean value. If it contains True, then our program is going to run the eat_cake(), eat_ice_cream(), and eat_pizza() functions. If it is False, nothing will happen. Either way, the program moves on to the next statement or following code after evaluating this if statement.

03:05 How about we work through a couple of examples in the console? Okay. Here we are in the console. Two examples for you. The first one, let’s set up two variables, x and y. We’ll say x = 10 and y = 3.

03:23 Let’s set up our if statement. if x > y,

03:30 remember we need a colon (:), and notice here that my console auto-indents for me.

03:38 And let’s do this. We’ll print something.

03:50 So, what should happen here? if x > y: this print statement will run. Enter twice. 10 is greater than 3. Scenario two, let’s put a quick list together.

04:07 Brackets will indicate our list. Let’s populate it. We’ll put 'baseball' in there, 'hockey', what else? 'dodgeball'.

04:25 Now, let’s set up our if statement to check if something exists in our list.

04:33 If 'dodgeball' is in sports_list, let’s print something out.

04:47 So, what do you think? What will the expression evaluate to? There it is. So, our if statement checked if 'dodgeball' was in the list. That’s True, so it printed 'Yes, its a sport'.

007 on May 6, 2019

whats the use of the print(f”{x} as used in the above tutorial

Dan Bader RP Team on May 7, 2019

What’s the use of print(f"{x} ..."})?

It’s an f-string, a new way to format strings in Python 3.6 and above. Check out this course to see how they work.

Become a Member to join the conversation.