So first head on over to http://www.python.org/getit/
If you are on a Windows or a Mac just scroll down and download the appropriate build.
For Linux users, bring up your terminal, in Ubuntu the shortcut should be CTRL+ALT+T and type:
sudo apt-get install python2.7
Okay once you are finished with that I want you to bring up the interactive python shell. For Windows users, you should be able to find it in your start menu. For Mac and Linux users, you should be able to open up the Terminal and type 'python' without quotes. You'll know once you are in the Python Interactive Interpreter because you'll see some text and then a prompt that looks like this:
>>>
In programming, like math, we store information in variables. So for example if I wanted to store the number 5 I would have to give it a name first, so I'll name it number. The format must be as follows, the name on the left and equals sign and then the value to be assigned on the right, like so:
>>> number = 5
Once I enter that in.. nothing happens! Well actually there is more going on here than meets the eye. Try this:
>>> print number
Ah, now we can see! It saved our number. Let's try a string (a string is what we call text in programming). In programming, it is traditional to create a 'Hello, World!' program for the first program. Which is basically just a program that prints 'Hello, World!.' Well let's change it up a bit, try this:
Open up a text file and save it as 'whatsup.py' and enter the following into the file:
#!/usr/bin/env python
yourname = raw_input('Enter your name: ')
print "What's up " + yourname + "? Welcome to Python!"
Next make sure you save the file, also make sure (this step is a MUST) that all your text in your whatsup.py program is aligned to the left margin, if not the program will not work. This is because in Python, spacing is used to separate various parts of a program (we will go into this more in a future post).
Okay now let's test our first program. On Windows just double-click your file, on Linux and Mac you should be able to do the same. What happened? Can you guess why?
First we asked the user for their name and stored it in a variable using the function raw_input(). Then we concatenated the string using + signs. This is what we must do if we want to edit a string together with a variable. Try and experiment with this a bit in the Python Interpreter. First place a string in a variable and then try and concatenate it in a sentence. For example:
>>> sometext = 'Bill'
>>> print "What's up " + sometext
See the new text goes in parenthesis and the variable does not. If you have any questions or comments feel free to leave me some feedback!
No comments:
Post a Comment