When instructions are one of the first things you should learn in any programming language, and that is required of just about any useful code. In this post, we’re going to look at how to use if statements in Python so you can build useful apps!
Once you understand this basic function, you open up a whole world of possibilities!
Using if statements in Python
If you’ve 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 are experienced in coding and just want to know how to use if statements in Python, keep reading:
if magic_number == 7: print("The number is correct!")
Just follow the word “if” with the statement you want to test, then add a colon. The following block of code (all of the indented text) is only executed if the statement is true.
What are if statements in Python?
For those with no programming experience, an “if statement” is code used for “flow control”. This means that you’ve created some kind of fork in the road: a point in your program where the flow of events can branch into two or more paths.
This is essential in any program because it enables a program to interact change with the user or dynamically in response to external factors.
Also read: Using lists in Python
Specifically, the “if statement” in Python does this by testing whether a statement is true and then only executing a block of code if it is.
In other words:
“IF this is true then do it.”
In a program this can mean:
“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 and create an interactive experience for the user!
To do this, we have to rely on a more advanced concept: the variable. A variable is a word that represents an item of data. For example we can say:
magic_number = 7
This creates a variable called “magic_number” and specifies a value of seven. This is important because we can now exam if this value is correct.
We write “if” and then the statement that we want to test. This is known as a “test instruction”.
When checking the value of something, we use two equal signs. This may seem confusing, but it really is avoids Confusion; We only use a single equal sign when we are to assign Value.
After the instruction, we add a colon and then an 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 whatever the case. However, if you change the value of magic_number to “8”, “The number is correct!” Not displayed. on the screen.
Using if statements in Python with else
Finally, you may want to combine if statements with “else” statements. Otherwise it does exactly what it sounds like: it tells Python what to do when the value is not true.
For example, we might want to check someone’s PIN number:
pin_number = 7321 if pin_number == 7321: print("Correct pin!") else: print("Incorrect pin!") print(“Did you get it right?”)
Here the “else” code is only executed if the PIN is incorrect. “Did you do it right?” still shows no matter what!
We can also use a similar variant called “else if” or “elif”. This means “when one thing is not true, but the other thing 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 in this example two different variables are also compared with one another!
Now that you know the basics of using if statements in Python, there is a lot more 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.
So we can say: if “health” is less than one, then gameover.
It is also possible to “nest” ifs and others by indenting more and more. In this way you can say, “If this is true, do this, but only if this is ALSO true.”
Likewise, we can use “and” and “or” statements 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 you understand how to use if statements in Python. You have a crucial tool under control! This forms the backbone of much of your programming and helps you run all kinds of logic tests.
So why not expand your knowledge with an online Pythohn course? A list of our favorites can be found here.
Our in-depth Python manual has a more in-depth tutorial that explains everything you need to know to get started coding in Python: