How to use if statements in Python – Android Authority

If statements are among the first things you should learn in any programming language, and are required for pretty much any useful code. In this post, well take a look at how to use if statements in Python, so that you can begin building useful apps!

Once you understand this fundamental feature, youll open up a whole world of possibilities!

If you have never programmed before, then make sure to read the next section to find out precisely 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, then read on:

Simply follow the word if with the statement you want to test, and then add a colon. The following code block (all indented text) will run only if the statement is true.

For those with no programming experience, an if statement is a piece of code that is used for flow control. This means that youve created a kind of fork in the road: a point in your program where the flow of events can branch off into two or more paths.

This is essential in any program, as it is what allows a program to interact with the user, or to change dynamically in response to outside factors.

Also read: How to use lists in Python

The if statement in Python does this specifically by testing whether a statement is true, and then executing a code block only if it is.

In other words:

IF this is true, THEN do this.

In a program, this might translate 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!

In order to accomplish this, we must rely on one more advanced concept: the variable. A variable is a word that represents a piece of data. For example, we can say:

This creates a variable called magic_number and gives it the 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 the test statement.

When checking the value of something, we use two equals signs. While this might seem confusing, this actually avoids confusion; we only use a single equals sign when we are assigning value.

After the statement, we add a colon, and then an indentation. All code that is indented after this point belongs to the same code block and will only run if the value is true.

In this example, the words Did you get it right? will show whatever the case. But if you change the value of magic_number to 8 then you wont see The number is correct! on the screen.

Finally, you may also want to combine if statements with else statements. Else does exactly what it sounds like: it tells Python what to do if the value isnt true.

For example, we might want to check someones PIN number:

Here, the else code only runs if the PIN is incorrect. Did you get it right? still shows no matter what happens!

We can also use a similar variation called else if or elif. This means if that thing isnt true, but this other thing is.

For example:

Notice that this example also compares two different variables with one another!

Now you know the basics of how to use 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 instance, the > symbol means bigger than, while < means smaller than.

Thus, we can say: if health is smaller than one, then gameover.

Its also possible to nest ifs and elses by indenting more and more. This way, you can say if this is true then do this but only if that is ALSO true.

Similarly, we can use statements called and and or in order to add multiple test statements.

For example:

Or:

Now you understand how to use if statements in Python, you have a crucial tool under your belt! This will form the backbone of much of your programming, and will help you to run all kinds of logic tests.

So why not take your knowledge further with an online Pythohn course? You can find a list of our favorites to get started with here.

Or, for a more in-depth tutorial right here that explains everything you need to know to start coding in Python, check out our comprehensive Python guide:

The rest is here:
How to use if statements in Python - Android Authority

Related Posts
This entry was posted in $1$s. Bookmark the permalink.