Python Tutorial — Chapter 8 — Conditions(if, elif, else statements)

In chapter 8 of Python Tutorial, we will learn about Conditions(if, elif, else statements)

Before we begin, if you are viewing this tutorial on Mobile, please rotate your mobile horizontally to view the tutorial for best experience. You can enable auto-rotate in the phone settings.

In Python programming, if an expression has to be evaluated or compared, we use Boolean values – True or False.

You may recollect that we have seen comparison operators in Chapter 4 — Basic Operators where we compared/evaluated expressions and the output was True or False. To refresh your memory, please find some of the expressions we covered previously below:

DataCamp Light | Standalone example
x = 17 y = 59 print(x == y) # prints out False print(x != y) # prints out True print(x < y) # prints out True

Please note that assignment of variables( x and y) is done using a single equal operator "=", whereas comparison between two variables is done using double equals operator "==". Also, to check if the variables are not equal then, "not equals" operator is marked as "!=" as shown above.

Moving on, in this chapter 8 of Python Tutorial, we'll explore how to use if, else, and elif statements along with some of the operators such as "and", "or", "is", "in" and "not".

Let's look at "and" and "or" operators examples:

DataCamp Light | Standalone example
name = "Alex" year = 2000 if name == "Alex" and year == 2000: print("My name is Alex, and I was born in year 2000.") if name == "Alex" or name == "Steve": print("Your name is either Alex or Steve.")

With "in" operator, we can find out whether a particular object is present in list or tuple. Let's take a look at the below example:

DataCamp Light | Standalone example
fruit = "apple" fruitlist = ["grape", "orange", "apple", "mango"] if fruit in fruitlist: print("Apple is in the list")

As mentioned in chapter 1 about indentation, it is used to represent a block of code. Tabs and spaces are both supported, however standard indentation requires four spaces in Python code but it is not mandatory.

Let's look at an example of code blocks in Python:

if condition1:
    # code block 1

elif condition2:
    # code block 2

else: 
    # code block 3

Please note the indentation for each code block, it is following standard indentation of four spaces.

Now, in the above example, if condition 1 is True, then the program executes the code block 1.

If condition 1 is not True, then program evaluates condition 2. If it is True, then program executes code block 2.

If condition 1 or condition 2 are not True, program starts executing code block 3.

DataCamp Light | Standalone example
marks = 73 if marks > 90: grade = "A" elif marks <= 89 and marks >= 75: grade = "B" elif marks <= 74 and marks >= 50: grade = "C" elif marks <= 49 and marks >= 35: grade = "D" else: grade = "F" print("Your grade is: %s" %grade)

In this example, a student scored 73 marks out of 100. So, we assigned 73 value to "marks" variable. Now, we are checking what grade should be given to the student based on his/her marks.

The program will go through the first condition to check whether marks achieved is greater than 90. Since, it is False, the program will start evaluating second condition to check whether marks is less than or equal to 89 and greater than or equal to 75. Again, it is False.

The program will move to the third condition to check whether marks is less than or equal 74 and greater than or equal to 50. Since, the marks achieved is between this range, the condition is True and this code block will be executed. It prints the output.

You can make changes to the marks above in the IDE itself and run the program. Or you can make your own conditions and code blocks. Give it a try, Good Luck!

"is" Operator:

"is" operator is used to check whether the two variables are same object with same memory location. It is not used to match the values of the variables. Refer below example:

DataCamp Light | Standalone example
x = ["orange", "mango"] y = ["orange", "mango"] z = x print(x is y) #returns False because x is not the same object as y, even if they have the same content print(x == y) # this comparison returns True because x is equal to y as the values are same. print(x is z) # returns True because z is the same object as x

In the above example, please note the difference between "is" operator and "==" operator. When "is" operator is used, it will check whether the variable is same object or not.

Meanwhile, in the case of "==" operator, it will check the values of the variable are same or not.

"not" operator:

With "not" operator, result is reversed. If the result is True, "not" operator will return the result as False. Refer below example:

DataCamp Light | Standalone example
x = 7 print(x > 5) #if not operator is used, the result is reversed print(not (x > 5))

As you can see above, in the first output, correct result was given i.e.., True. Then, in the second output, when the same evaluation is tried and "not" operator is used, it gave the output as False.

That brings to the end of chapter 8 of Python tutorial, see you in the next tutorial!

By the way, if you haven't installed Python and Pycharm IDE on your Windows laptop, you can visit the below links to learn on how to install and configure Python and Pycharm IDE on Windows OS.

How to Install Python 3 and Pycharm IDE on Windows

How to configure Pycharm IDE and "Run" your first Python 3 program

For your reference, to download Pycharm IDE, visit below link:

https://www.jetbrains.com/pycharm/download/?section=windows

If you have any questions or comments about chapter 8 of Python tutorial, please let us know in the comments section below.

Leave a Reply