Salvatore Lopiparo

Senior Software Engineer

Super Simple Python #5 – True, False, and The if Statement

If you run out of money, then don’t spend money.

Please, keep that in mind. (Not just for this lesson…)

Note: In this lesson and in most future lessons, we will be writing functions that span several lines. It will be easier to write the lines into a file, then copy-paste them into the Python Console. From within PyCharm, go to File>New>Python File, then name it “lesson5”. Use that as a place to write functions for this lesson. Now to the programming!

If this is your first time here, I recommend looking at the first lesson. Starting there and going through the rest of the lessons will prepare you to go through this more advanced lesson!

First, True and False are used in Python to denote whether something is… well, true or false. Here’s your basic Truth Table:

1 < 2 - True (1 is less than 2)
1 > 2 - False (1 is greater than 2)

Since 1 is NOT greater than 2, you’ll get False. Here are the rest (for reference. We’ll use them more later):

1 == 1 - True (1 is equal to 1, note the double equal signs)
1 == 2 - False (1 is equal to 2)

1 != 1 - False (1 is NOT equal to 1)
1 != 2 - True (1 is NOT equal to 2)

1 <= 2 - True (1 is less than OR equal to 2)
1 <= 1 - True (1 is less than OR equal to 1)
2 <= 1 - False (2 is less than OR equal to 1)

1 >= 2 - False ( 1 is greater than OR equal to 2)
1 >= 1 - True (1 is greater than OR equal to 1)
2 >= 1 - True ((2 is greater than OR equal to 1)

(x.x) o-(''Q) - Boxing Kirby

if statements work like this:

If some value is True, then do some action.

So in Python, we would write something like the following. Note the 4 space indent similar to a function:

1
2
3
4
5
6
7
>>> if True:
...     print('It was True!')
... 
It was True!
>>> if False:
...     print("It was True!")
...

Or something a bit more useful:

1
2
3
4
5
6
7
8
9
10
>>> my_money = 100
>>> if my_money > 10:
...     print('Yay! I have money to spend: ' + str(my_money))
...     my_money = my_money - 10
...     print('I just spent $10!')
...     print('This is how much money I have left: ' + str(my_money))
...     
Yay! I have money to spend: 100
I just spent $10!
This is how much money I have left: 90

We can also add a different statement afterwards, called else, that will handle when the if statement didn’t happen:

If some value is True, then do some action, otherwise (else) do some other action.

1
2
3
4
5
6
>>> if False:
...     print("It was True!")
... else:
...     print("It was False!")
...     
It was False!

Or back to our more useful code:

1
2
3
4
5
6
7
8
9
10
11
12
>>> if my_money > 10:
...     print('Yay! I have money to spend: ' + str(my_money))
...     my_money = my_money - 10
...     print('I just spent $10!')
...     print('This is how much money I have left: ' + str(my_money))
... else:
...     print("I don't have enough money!!")
...     print('I\'m poor now: ' + str(my_money))
...     
Yay! I have money to spend: 90
I just spent $10!
This is how much money I have left: 80

Let’s put that in a function:

1
2
3
4
5
6
7
8
9
10
def spend_money(money_in_bank, money_to_spend):
    if money_in_bank > money_to_spend:
        print('Yay! I have money to spend: ' + str(money_in_bank))
        money_in_bank = money_in_bank - money_to_spend
        print('I just spent $' + str(money_to_spend) + '!')
        print('This is how much money I have left: ' + str(money_in_bank))
    else:
        print("I don't have enough money!!")
        print('I\'m poor now: ' + str(money_in_bank))
    return money_in_bank

Here, we’re replacing the $10 with money_to_spend so we can change how much we spend. Don’t forget the return statement at the end!

Now let’s go on a shopping spree!

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> my_money = spend_money(my_money, 40)
Yay! I have money to spend: 80
I just spent $40!
This is how much money I have left: 40
 
>>> my_money = spend_money(my_money, 20)
Yay! I have money to spend: 40
I just spent $20!
This is how much money I have left: 20
 
>>> my_money = spend_money(my_money, 300)
I don't have enough money!!
I'm poor now: 20

Leave a Reply