Python Variables, Loops, and More…Oh My

We’ll start with the example at the end of Python Variables: Part 2.

>>> 
>>> book_titles = ['Book Title', 'Smashed Soup', 'Learning Programming']
>>> 
>>> # Let's create a list of books we have
... for title in book_titles:
...     print title
... 
Book Title
Smashed Soup
Learning Programming
>>> 
>>> book_pricing = {'Book Title': 29.99,
...                 'Smashed Soup': 4.99,
...                 'Learning Programming': 999.99}
>>> 
>>> for title in book_pricing.keys():
...     price = book_pricing[title]
...     print title + ' - $' + str(price)
... 
Smashed Soup - $4.99
Learning Programming - $999.99
Book Title - $29.99
>>>

In this example, there are quite a few new functions and manipulations that we haven’t seen yet.  That is to be expected since variables would be quite boring and useless if there were no provided way to change them as necessary.

We start with an array, book_titles, and then loop through each title by using a for loop.  In the loop, we print the title of each book.

>>> 
>>> book_titles = ['Book Title', 'Smashed Soup', 'Learning Programming']
>>> 
>>> # Let's create a list of books we have
... for title in book_titles:
...     print title
... 
Book Title
Smashed Soup
Learning Programming
>>>

The way a for loop works in Python is again similar to other languages.  We write:

for <single_item_in_list> in <our_list>:
    <logic for each item here>

You can read this as “for each <item> in <list>, do stuff,” where <item> is your variable name for the single item in the <list>.  This is used when you have a list of items you want to perform the same logic on.

After this, we loop through the dictionary of books and their prices.  Sometimes, you have an array of items like book titles and use those titles to access their prices in a dictionary, like book_pricing.  Other times, you just want to loop through all the items in a dictionary.  Those are called key – value pairs.

>>> 
>>> book_pricing = {'Book Title': 29.99,
...                 'Smashed Soup': 4.99,
...                 'Learning Programming': 999.99}
>>> 
>>> for title in book_pricing.keys():
...     price = book_pricing[title]
...     print title + ' - $' + str(price)
... 
Smashed Soup - $4.99
Learning Programming - $999.99
Book Title - $29.99
>>>

Here we use keys() which is a method of all dictionary objects.  Method is just another name for function but used on objects which we’ll go over more when we discuss object-oriented development.  What keys() does is return all the, you guessed it, keys of the dictionary as a list.

In our case, we loop through this list of titles and use title as a key for book_pricing to get the book title’s price.  We assign this value to the price variable, then we print title and price.  You may notice, though, that we add ‘ – $’ to format the output.  In order to get this all together, we concatenate the strings by using the ‘+’ plus sign, which is like ‘adding’ our text together.  We also use the str() function to convert our price to a string, since Python will throw a fit if we try to concatenate a string and a different type:

>>> 
>>> price = 9.99
>>> title = 'Book Title'
>>> 
>>> print title + ' - $' + price
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'float' objects
>>>

As you’ll find out as you use more Python, there are many other ways to build and manipulate variables and data in general.  This is just the tip of the iceberg, and we’ll see more of that as we move forward.

Series Navigation<< Python Variables: Part 2
Becoming a Python Grown-Up >>