Skip to content
Home » Conditional Statements in Python – the Basics

Conditional Statements in Python – the Basics

Spread the love

Today we’ll be talking about the basics of conditional statements.

Here’s the video version of this article:

Conditional statements are necessary for branches in the work flow. The decision depends on the boolean value to which the expression in the conditional statement evaluates.

Work flow with conditions can be visualized in flow charts. Here’s an example:

chart

This chart could be represented in code like this:

 have_coupon = True
 bought_any_product = True
  
 if have_coupon == True:
     price = 120
     print("paying " + str(price))
 else:
     if bought_any_product == True:
         price = 120
         print("paying $" + str(price))
     else:
         price = 280
         print("paying $" + str(price)) 

Here we can see that conditions may be nested. The bought_any_product condition is inside the have_coupon condition.

With the two boolean variables set to True, we’ll get the following output:

 paying $120 

How did it work? The first line of code checks the first condition and it’s true. So, the block of code below is executed: the price is set to 120 and you pay (which here is simply represented by printing a message).

Now let’s change the first variable:

 have_coupon = False
 bought_any_product = True 

The rest of the program is the same. If run the program, now the output will be the same:

paying $120 

This time, the condition in the first line of code was not met, so the first block of code was not executed. Instead, the else block was executed. Inside the else block the variable bought_any_product is set to True, so the condition is met. If so, the block of code beneath is executed, again setting the price to 120. The nested else block is ignored.

Let’s try setting both variables to False:

 have_coupon = False
 bought_any_product = False 

If we now run the program, we’ll get the following output:

 paying $280 

This is because the condition in the first line was not met, which redirected the work flow into the else block. Inside the else block the condition was not met either, so the nested else block was executed, in which the value of price was set to 280.

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Logical Operators

Well, the above code works, but there are lots of repetitions. Repetitive code is bad and should be avoided. We can easily do it with our code by using logical operators. In conditional statements we can use logical operators (and, or, not).

Let’s refactor our code, so that it’s no longer repetitive:

 have_coupon = False
 bought_any_product = False
  
 if have_coupon == True or bought_any_product == True:    
     price = 120    
 else:
     price = 280
  
 print("paying $" + str(price)) 

This code looks much better. The price is set to 120 if any of the two variables is True. Otherwise it’s set to 280. Also, the paying message doesn’t have to be repeated in each block. Actually, the paying message is displayed regardless of whether any of the variables is set to True, so let’s move it to the end and remove it from the blocks under the conditional statements, which also means removing the indentation. Now the nesting is also gone and the simpler the better.

We can make this code even simpler. Instead of writing if a == True, we can simply write if a. This is a shortcut. So, our code could look like so:

 have_coupon = False
 bought_any_product = False
  
 if have_coupon or bought_any_product:    
     price = 120    
 else:
     price = 280
 print("paying $" + str(price)) 

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply