Python Variables

Variables in Python are similar to most other languages.  They store values, both static and dynamic, for use and manipulation later in your application.

Here is an example:

>>> name = 'Zach'
>>> print name
Zach
>>> name
'Zach'
>>> 
>>> 
>>> wallet_money = 150
>>> print '%s has $%d in his wallet'%(name,wallet_money)
Zach has $150 in his wallet
>>> 
>>> book_cost = 55
>>> 
>>> print '%s buys a book for $%d'%(name,book_cost)
Zach buys a book for $55
>>> 
>>> wallet_money = wallet_money - book_cost
>>> wallet_money
95
>>> print '%s has $%d left in his wallet'%(name,wallet_money)
Zach has $95 left in his wallet
>>> 
>>> # Comment here isn't processed as code
... # Zach loses $40 by being clumsy
... 
>>> clumsy_loss = 40
>>> wallet_money = wallet_money - clumsy_loss
>>> wallet_money
55
>>> print '%s has $%d left after being clumsy'%(name,wallet_money)
Zach has $55 left after being clumsy
>>> # Zach changes his name to George to hide his
... # previous clumsy life
... 
>>> name = 'George'
>>> print '%s is no longer clumsy or broke'%(name)
George is no longer clumsy or broke
>>>

In this code snippet, we have variables namewallet_moneybook_cost, and clumsy_loss.  Variables here are the names with an ‘=’ sign then a value.  This is called a variable assignment.  The primary purpose for using these is to be able to interchange values without having to know the actual value at the time your code runs.  That way, if you create a bank account application, each variable will store different items for name, wallet_money, book_cost, etc., depending on the person and account currently running.

You as the developer are then able to create logic around how you want your program to work and have it apply the same to everyone who uses your application.

It is best to name variables appropriately for their use so you are able to understand what is happening within your program, as well as anyone else who works with you or after you.  For example, you wouldn’t create a variable var1, var2, var3 instead of name, wallet_money, and book_cost, because you wouldn’t be able to tell them apart or why you are using them.

I’ll break each part in the code down below:

>>> name = 'Zach'
>>> print name
Zach
>>> name
'Zach'
>>>

Here we assign the value ‘Zach’ to the variable name.  ‘Zach’ is a string type which just means it is treated as text.  We then print name to see the value, (or if you are using the Python interpreter you can just type the variable to see the current value.

>>> 
>>> wallet_money = 150
>>> print '%s has $%d in his wallet'%(name,wallet_money)
Zach has $150 in his wallet
>>>

This variable wallet_money, is assigned a number that represents $150.  This type of variable is called an integer in fancy programming lingo.  As we see later in the code, you can perform standard math on this type of value to also include multiplication ‘*’ and division ‘/’.  We’ll use more of these operations in later lessons.

We also use what we learned in the last lesson to print the name and wallet_money values in a way that is useful for us.

>>> book_cost = 55
>>> 
>>> print '%s buys a book for $%d'%(name,book_cost)
Zach buys a book for $55
>>> 
>>> wallet_money = wallet_money - book_cost
>>> wallet_money
95
>>> print '%s has $%d left in his wallet'%(name,wallet_money)
Zach has $95 left in his wallet
>>>

This time we have a variable that represents the cost of a book, book_cost equal to 55.  We print book_cost in a pretty sentence with name.  Then we subtract book_cost from wallet_money because name buys a $55 book.  The value of wallet_money can be seen now as 95.

 

>>> # Comment here isn't processed as code
... # Zach loses $40 by being clumsy
... 
>>> clumsy_loss = 40
>>> wallet_money = wallet_money - clumsy_loss
>>> wallet_money
55
>>> print '%s has $%d left after being clumsy'%(name,wallet_money)
Zach has $55 left after being clumsy

We now see a comment in the code.  Comments are used to explain what your thoughts or what is going on to help you remember something unusual or tell a future programmer why you are writing your code this way.  It could also just be a reminder.  In any case, it isn’t run as the rest of the code, it’s purely informational for developers.  clumsy_loss now has a value of 40 and is subtracted from wallet_money.  $55 is left after being clumsy.

>>> # Zach changes his name to George to hide his
... # previous clumsy life
... 
>>> name = 'George'
>>> print '%s is no longer clumsy or broke'%(name)
George is no longer clumsy or broke
>>>

Lastly, because Zach is clumsy and doesn’t want to keep that sort of reputation, he changes his name to be George.  I use this as an example to show that both wallet_money and name can contain the the same value, a modified version of its original value, or an entirely new value.  Here it doesn’t even have to be a string, as we could assign 100 to name, but that wouldn’t make any sense would it?  It would also cause an issue in our code and introduce a bug.

This is quite a bit to chew on, but I hope it explains the basics of variables, why we use them, and how to use them to make your application run.  In its essence, programming is simply a way to speed up a manual process so it can be run many, many times in a short amount of time.

If you have any questions, feel free to contact either Daryl @codecrawl or me (Zach) @zachhilbert on Twitter.  See you next time, same bat time, same bat channel.

Series Navigation<< Hello Python World
Python Variables: Part 2 >>