Learn Python For Beginners

Python If Statement

The python if statement is a simple way to compare data. This section is going to be more complex than the rest in this series for learning python for beginners, so make sure you are caught up to this point. The python if statement is also a great way to learn this approach to logic because you will find that most programming languages if statements operate essentially the same.

What Is An If Statement?

An if statement in python and other languages works probably the way you imagine it. You feed the statement some scenarios and if they are true or not true then you can determine what happens next. Let’s combine what we’ve learned so far and write a program that lets us know if a user is old enough to drink.

Python If Statement

There might be quite a lot going on here that confuses you. Let’s walk through it together. After the if statement you see a colon for the first time. Directly after that the next line of code is indented. This is the syntax for a python if statement and any loop logic as well as the try-catch statement. The else statement isn’t necessary in this scenario but it’s good to know it exists. Think of it like this: When you nest logic in a statement like this you are saying run this logic when this thing is happening. For more complex if statements in python you can nest another if statement. Try this:

If you do enter a number greater than 45 you will get this output:

If you didn’t you should see something like this:

Note that in the second scenario even though we put exactly 45 we didn’t get the second output. That’s because we only used the greater than comparison operator not that greater than or equal to operator. Let’s talk more about comparison operators.

Python Comparison Operators

Python if statements rely on comparison operators to determine what needs to be happening for the nested logic to run. Let’s write a little program that can showcase how this works.

Python If Statement

Take a minute to try running the program multiple times and getting each possible output. Try changing or adding values to see what happens. While running through the program you will notice that multiple things will print out to the terminal. Many scenarios will call for a python if statement with multiple comparison operators. Let’s take our first two if statements and combine them.

Multiple Operators In Python If Statement

When combining comparison operators in an if statement we use the and, or and not operators. Before we dive right into the concept though, it’s important to note that order of operations still takes effect and certain comparisons have precedence over others. Here is an example:

Python If Statement

If we run the program now and we put a number in that’s between 21 and 45 we should see this:

This time we only got the first output as expected. Now let’s put a number higher than 45 in as input.

Now we don’t get both outputs like before. This is the first taste of the important concept of separation of concerns in software. This will become easier to understand when we reach things like functions and object-oriented programming. We are almost there, but for now let’s move on to lists in python.

Leave a Comment

Your email address will not be published. Required fields are marked *