Becoming a Python Grown-Up

These Python lessons have been moving fairly quickly, and that’s primarily because of all the other great lessons on the rest of site.  Once you learn to develop code in a language, it’s relatively simple to move on to learn a new language.  I’m assuming that new language for you, is Python.

Since we have touched on variables, basic data structures, and for loops, I want to finish up some of the core basics by going over while loops and conditional statements.

The code we’re going to work with is below:

wallet_money = 150 
# We're going to invest in penny stocks
# that cost a lot more than a penny
while wallet_money > 0:
    invest_amount = 0 
    # Checking if we should give up the rest
    if wallet_money <= 15: 
        # Invest all our money in pennies
        invest_amount = wallet_money
        wallet_money = 0
    else:
        # Invest 25% in pennies
        invest_amount = .25 * wallet_money
        wallet_money -= invest_amount
    print "Investing $%d" % (invest_amount)
    print "$%d left after losing on pennies" % (wallet_money)

print 'We squandered our savings on penny stocks!'

In order to follow this code, we have to acknowledge the elephant in the code block….we don’t have brackets!  Instead of using brackets to distinguish logic blocks, Python uses indentation, so formatting becomes the lifeblood of a successfully running python program.

Indentation needs to be consistent, ie. keep using tabs if you start with tabs, or keep using 2 spaces if you start with 2 spaces.

wallet_money = 150 
# We're going to invest in penny stocks
# that cost a lot more than a penny
while wallet_money > 0:
    invest_amount = 0
    ...stuff...

Here we start with $150 and a while statement.  While statements say “while <test is true>: do stuff.”  In our case, we say “while our wallet money hasn’t run out, invest, over, and over, and over….”  The ‘:’ colon after the test indicates that logic within this statement is to follow.

invest_amount = 0 
# Checking if we should give up the rest
if wallet_money <= 15: 
    # Invest all our money in pennies
    invest_amount = wallet_money
    wallet_money = 0
else:
    # Invest 25% in pennies
    invest_amount = .25 * wallet_money
    wallet_money -= invest_amount
print "Investing $%d" % (invest_amount)
print "$%d left after losing on pennies" % (wallet_money)

While we have money in our wallet, we first check to see how much we have left.

# Checking if we should give up the rest
if wallet_money <= 15: 
    # Invest all our money in pennies
    invest_amount = wallet_money
    wallet_money = 0

Because our money amount changes each time we invest, this if is called a conditional statement, meaning the logic is subject to change depending on the condition of our check.  If we have $15 or less, then we invest the rest of our money.

else:
    # Invest 25% in pennies
    invest_amount = .25 * wallet_money
    wallet_money -= invest_amount

Here, else is exactly what it sounds like, if we have $15 or less, then invest everything, else invest 25% of the remaining money.  This is called an if/else construct.  We can also have if/elif/elif/else, etc. where elif has another check each time for something different.

One thing to note here is the wallet_money -= invest_amount line.  This is the same as saying wallet_money = wallet_money – invest_amount, but in a more concise way.

Finally we print the investment amount and remaining amounts each time the loop runs, and we end up broke again by trusting in the stock market of pennies.  These print statements should look familiar.

I saved my file as penny_stocks.py which can be run using the command python penny_stocks.py.  You can see the output of this script below:

$ python penny_stocks.py 
Investing $37
$112 left after losing on pennies
Investing $28
$84 left after losing on pennies
Investing $21
$63 left after losing on pennies
Investing $15
$47 left after losing on pennies
Investing $11
$35 left after losing on pennies
Investing $8
$26 left after losing on pennies
Investing $6
$20 left after losing on pennies
Investing $5
$15 left after losing on pennies
Investing $3
$11 left after losing on pennies
Investing $11
$0 left after losing on pennies
We squandered our savings on penny stocks!

After this, you should be pretty close to becoming a Python professional, ready to write all sorts of applications!

Series Navigation<< Python Variables, Loops, and More…Oh My
Python Objects (or a Bike and a Wheel) >>