Crash Course in Python

Part one: Installing python and variables.

A while ago, some friends of mine wanted to learn some basic programming which made me put together a quick and dirty introduction to programming in python. Now there are a lot of tutorials out there, and a lot of them might actually be better than this one. But here we go.

Installing python

First of all, we will need to install python which you can do by clicking here and following the instructions. I reccomend you pick version 2.7.12, but you should be able to follow this tutorial regardless of which version you choose.

After following all the steps you should be able to find a program called "IDLE" in your start menu or launchpad if you are on a mac, which when started should display something like this:


Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

>>>_


You are now ready to start telling the computer to do things by giving it instructions! Try entering "5+5" into the terminal window and then press enter. Whatch what happens. Try this out with other numbers and other operators and see what happens. You can even use parentheses to build as complex expressions as you can imagine. After testing some stuff your window might look like this:


Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

>>>5+5

10

>>>14/3

4.666666666666667

>>>3 * 4

12

>>>(23 / 4) * (91 + (90 / 10))

570.0

>>>_


Instead of remembering and typing each value every time you need it you can instead try assigning it to a variable. You might remember from math how a letter can be used to represent a numerical quantity and this is not that different.
Try writing "X = 100" for example and then press enter. It may look like nothing has happend but what you actually have done is created a sort of box in which the computer now stores the value 100.

Now everytime you need that value you can just write X instead. Try adding 20 to X and see what happens!
But how about something a little bit more interessting, ok basically the same thing but just to illustrate how it works, try writing "moneyInMyPocket = 250" and pressing enter. As before nothing happens but now you have a variable representing how much money you have in your pocket!

>>>X = 100

>>>X + 20

120

>>>moneyInMyPocket = 250

>>>_


You can use this variable in the same way as you use regular numbers. Lets add a few variables and try this out.


>>>moneyInMyPocket = 250

>>>priceOfThing = 50

>>>priceOfanotherThing = 25

>>>moneyInMyPocket - ( priceOfThing + priceOfAnotherThing )

175

>>>_


Note however that if just write moneyInMyPocket it would appear you still have 250 and not 175 as the above equation would suggest. What is going on here? Everytime you use a varialbe you are referencing the value that is stored there.