Skip to content

The Python if else statement : how it works

In the previous article, we’ve discussed Python lists, tuples, sets and dictionaries. In this article we’re going to be discussing Python if else statement, in other words flow control or conditional branching. So in this article we’re going to do a couple of different variations on that, in order to make our code a little bit more interactive.

That means, when certain things happen we can react to them and respond accordingly. The first thing we’re going to cover is the if statement which is a bit similar to the while loop where you just make condition. If this happens then you run that series of code, but with the if statement you just run it once you don’t continually run it over and over as long as the statement is true.

To clarify that, let’s take a look on the if statement syntax which looks like this:

if (test expression) : statement-to-be-executed

Let’s create a simple program to see how it works. The way we’re going to start is we’re going to do we’re going to capture a little bit of input from the user. We can do that like this

Getting user’s input

x = input ("Please enter an integer: ")

Basically what we’re doing here, is when you type in input it’s going to just pause and wait for the user to input. Note that we assign the obtained value to the variable “x”

int and str in input function
int and str in input function

Watch a 10 minutes video and get yourself up and running with Python Basics

If we run this code (if you’re using a notepad script), type in 25 and we expect it to be plus 7 which would be 32. But then we get this error because when they’re typing in this it’s actually pulling it out as a string rather than an integer.

Solving this issue is fairly simple, just include the “input” statement inside the “int” function which will basically convert the value to an integer, like so: x = int (input ("Please enter an integer: "))

Then I can type in 25 and it runs successfully it doesn’t throw an error.

int conversion - Python input function
int conversion – Python input function

Now we’re going to get into the actual if statement section

I can do if X is less than 0 then you do a colon and then you have to indent and then this is the code that it’s going to run when that condition is true, like this:

if x < 0 :

print ("X is Negative")

Now what’s going to happen is if X is less than 0 or basically if X is negative then it’s going to print out saying that the x is negative.

simple if statement example
simple if statement example

As you can see, it does nothing at this point as I typed a positive value. let’s try negative 3 and see what happen.

simple if statement example 2
simple if statement example

 it triggered our if statement and it ran the piece of code to print out that x is negative.

Python if else statement: the else Keyword

The next thing we have to talk about is the “else” keyword. As it is almost always present after the if keyword.

Back to the example above, when x is positive nothing happened. So one way to get something done, or a sentence printed out for example, when x is positive, is to add the else keyword. You can do that like this:

x = int (input ("Please enter an integer: "))

if x < 0:
print ("X is Negative")
else:
print ("X is positive")

The elif Keyword

Another keyword that is commonly used in conditional branching with Python, is the elif keyword. The elif keyword basically add more options to your code.

Let’s suppose that I typed 0 in the if else statement above. As you might imagine, nothing will happen again, because 0 is equals to 0, not greater nor less than 0!

In this case the code will be as the following:

x = int (input ("Please enter an integer: "))
if x < 0:
print ("X is Negative")
elif x==0:
print ("X is null")
else:
print ("X is positive")

if, elif, else statement example
if, elif, else statement example

Hopefuly this articles helped you understand the structure of an if else statement and how to use it in Python, Please let me know if you have any comments, remarks or questions in the comment section, and take a minute to check my Python beginners course and feel free to follow me on twitter for daily updates.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)