Programming

with Snakes (part 3)

The Lazy Programmer


"Laziness ... The first great virtue of a programmer." -- Larry Wall

What if we could type in "Armbrae is great" just once, and store it for later use? We can!

>>> feelings = "Armbrae is great"
>>> feelings
"Armbrae is great"

We've just created a variable named feelings and gave it the value "Armbrae is great".

What is a Variable?


A variable is a "bucket" that holds information.

The bucket has a name that you can use to refer to it.

You can change what goes in the bucket.

A variable is called just that because it can change

Changing a Variable


>>> tax = 10 # this stores 10 in variable 'tax'

Now let's ask for the tax amount.
>>> tax
10

We can change the variable 'tax'

>>> tax = 15
>>> tax
15

Calculating Tax


You can use a variable as if it was a value.

For instance, we have created 'tax' and assigned it an integer. That makes 'tax' an integer and we can use it like one.

>>> 9.99 * tax / 100.0
1.4985
>>> tax = 20
>>> 9.99 * tax / 100.0
1.9980000000000002

Storing a Result


We often want to store away the result of an operation. We can do that with variables as well.

>>> tax = 15
>>> tax_amount = tax * 9.99 / 100
>>> tax_amount
1.4985

We can use variables on both sides of an assignment. First the right side is calculated and then that value is assigned to the variable on the left.

Right Side Then Assignment


We can use the same variable on both sides.

Remember:
the right side is calculated first and then it is assigned to whatever is on the left side--even if it is the same variable.

>>> tax = tax - 5
>>> tax
10

String variables


We can use variables to store strings as well.

>>> school = "armbrae is my school."
>>> school
"armbrae is my school"
>>> school.capitalize()
"Armbrae is my school"

>>> school
"armbrae is my school"

Manipulating String Variables


>>> school = "armbrae is my school"
>>> me = "gordie is my name"
>>> me + " and " + school
gordie is my name and armbrae is my school

What functions would I use to capitalize the 'G' in gordie and the 'A' in armbrae and create the following string:

"Gordie is my name and Armbrae is my school"

One Possibility


>>> school = "armbrae is my school"
>>> me = "gordie is my name"
>>> me.capitalize() + " and " + school.capitalize()
Gordie is my name and Armbrae is my school

Formatted Output


How can we output the value of a variable when we want to use it in a sentence? If the variable is a string we can just use '+'

>>> name = "Gordie"
>>> "My name is " + name
My name is Gordie

What if the variable is a number? Try the following:

>>> age = 16
>>> "My age is " + age

Formatting


>>> tax = 15
>>> "Tax in Nova Scotia is %d" % tax 
'Tax in Nova Scotia is 15'

>>> province = "Alberta"
>>> "Tax in %s is 5" % province
'Tax in Alberta is 5'

%d   is a placeholder for a number
%s   is a placeholder for a string
%f    is a placeholder for a float

More than one value


>>> tax = 5
>>> province = "Alberta"
>>> "The tax in %s is %d" % (province, tax)
The tax in Alberta is 5

>>> "The tax in %s is %d %%" % (province, tax)
The tax in Alberta is 5 %

Because '%' is a special character, to output '%' we need to double it up '%%'

Saving Commands


We have been typing our commands directly into the Python interpreter and getting answers.

We sometimes have to type the same thing over and over again.

It would be good to be able to write the commands once and then reuse them if we want to do the same thing more than once.

We can save commands to a file and then ask Python to execute all the commands in the file.

The first Program


Quit the python interpreter (do not exit Terminal)
>>> quit()

Open TextWrangler

Create a new file that contains the following:
party_budget = 1000
party_budget

Save the file on your Desktop as party.py

Running Your Program


In Terminal go to your Desktop

cd ~/Desktop

Tell Python to execute the commands in your file

python party.py

... and you will not see any output

Program Output


The interpreter knows that when you type a variable name that you want to see its value. To do this in a program you need to be explicit and use 'print'

Edit (and save) the file party.py to add in the 'print' command

party_budget = 1000
print party_budget

Now try python party.py in Terminal again ... woohoo

That is your first Python Program


Next class we will start creating pretty pictures using turtle.

We will also start learning to make decisions in Python and teach the computer to do things over and over again.
Made with Slides.com