If statements are among the first things you should learn in any programming language and are required for almost any useful code. In this post, we take a look at how to use if statements in Python, so you can start building useful apps!
Once you understand this basic function, you open up a whole world of possibilities!
How to reuse statements in Python
If you have never programmed before, be sure to read the next section to find out exactly what an “if statement” is and how to use it.
Also read: How to call a function in Python
If you have coding experience and you just want to know how to use if statements in Python, read on:
if magic_number == 7: print("The number is correct!")
Just follow the word “if” with the statement you want to test and then add a colon. The following code blocks (all indented text) are run only if the statement is true.
What about statements in Python?
For those without programming experience, an “if statement” is a code used for “flow control.” This means that you have created a kind of fork on the road: a point in your program where the flow of events can branch out into two or more paths.
This is important in any program, as it is what enables a program to interact with the user, or to change dynamically in response to external factors.
Also read: How to use lists in Python
The “if statement” in Python does this specifically by testing if a statement is true and then only running a block of code if it is.
In other words:
“IF this is true, then do it.”
In a program this can be translated to:
“IF the user enters the correct password, then grant access.”
“IF the player has 0 health, then end the game.”
Now the code can react depending on various factors and inputs, creating an interactive experience for the user!
To achieve this, we must rely on a more advanced concept: the variable. A variable is a word that represents a piece of data. For example, we can say:
magic_number = 7
This creates a variable called “magic_number” and gives it a value of seven. This is important because we can now test if that value is correct.
To do this we write “if” and then the statement we want to test. This is called a “test statement.”
When we check the value of something, we use two equals. While this may seem confusing, it is avoids confusion; we only use an equal sign when we are assign value.
After the statement, we add a colon and then a indentation. All code that is indented after this point belongs to the same “code block” and is only executed if the value is true.
magic_number = 7 if magic_number == 7: print("The number is correct!") print("Did you get it right?")
In this example, the words “Did you get it right?” will show anyway. But if you change the value of magic_number to “8” you will not see “The number is correct!” on the screen.
How to reuse statements in Python and more
Finally, you may also want to combine if statements with “other” statements. Else does exactly what it sounds like: it tells Python what to do about the value not TRUE.
For example, we might want to check someone’s PIN:
pin_number = 7321 if pin_number == 7321: print("Correct pin!") else: print("Incorrect pin!") print(“Did you get it right?”)
The “other” code is only run here if the PIN code is incorrect. “Did you get it right?” still shows no matter what happens!
We can also use a similar variant called “else if” or “elif.” This means “if that thing is not true, but the other is.”
jeffs_pin = 7321 bobs_pin = 2212 enterred_pin = 7321 if enterred_pin == jeffs_pin: print("Welcome Jeff!") elif enterred_pin == bobs_pin: print("Welcome Bob!") else: print("Incorrect PIN") print("What would you like to do?")
Note that this example also compares two different variables with each other!
Now you know the basics of using if statements in Python, but there are many more things you can do.
For example, you can use different “operators” to create different test statements. For example, the symbol “>” means greater than, while “<" means less than.
Thus we can say: if “health” is less than one, then play over.
It is also possible to “live” ifs and elses by pulling in more and more. This way you can say “if this is true then do it but only if it is AND true.”
Similarly, we can use statements called “and” and “or” to add multiple test statements.
if enterred_pin == jeffs_pin and username == "Jeff": print("Welcome Jeff!")
if enterred_pin == jeffs_pin or enterred_pin == bobs_pin: print("Welcome!")
Now that you understand how to use statements in Python, you have an important tool under your belt! This will be the backbone of much of your programming and will help you run all kinds of logical tests.
So why not take your knowledge further with an online Pythohn course? You will find a list of our favorites to get started with here.
Or, for a more in-depth tutorial here that explains everything you need to know to start coding in Python, check out our comprehensive Python guide: