Chapter 1
Getting Started
Go to www.python.org and download the latest
version of Python (version 3.5 as of this writing). It
should be painless to install. If you have a Mac or Linux, you may already have
Python on your computer, though
it may be an older version. If it is version 2.7 or earlier, then you should
install the latest version, as many of the programs in this book will not work correctly on older versions.
1.2 IDLE
When you first start IDLE, it starts up in the shell, which is an interactive window where you can type in Python code and see the
output in the same window. I
often use the shell in place of my calculator
or to try out small pieces of code. But
most of the time you will want to open up a new window and type the program in there.
Note At least on Windows, if you click on a Python file on your desktop, your system will run the program,
but not show the code, which is probably not what you want. Instead, if you
right-click on the file, there should be an option called Edit with Idle. To edit an existing Python file, either do that or start up IDLE and open the file through the File menu.
Keyboard shortcuts The following keystrokes work in IDLE and can really speed up your work.
Keystroke |
Result |
CTRL+C |
Copy selected text |
CTRL+X |
Cut selected text |
CTRL+V |
Paste |
CTRL+Z |
Undo the last keystroke or group of keystrokes |
CTRL+SHIFT+Z |
Redo the last keystroke or group of keystrokes |
F5 |
Run module |
Start IDLE and open up a new window (choose New Window under the File Menu). Type in the following program.
Then, under the Run menu, choose Run Module (or press F5). IDLE will ask you to save the file, and you should do so. Be sure to append .py to the filename as IDLE will not automatically append it. This will tell IDLE to use colors to make your program easier to read.
Once you’ve saved the program, it will run in the shell window. The program will ask you for a temperature. Type in 20 and press enter. The program’s output looks something like this:
Let’s examine how the program does what it does. The first line asks the user to enter a tempera- ture. The input function’s
job is to ask the user to type something in and to capture what the user types. The part in quotes is the
prompt that the user sees. It is called a string and it will
appear to the program’s user
exactly as it appears in the code itself. The eval function is
something we use here, but it
won’t be clear exactly why until later. So for now, just remember that we use
it when we’re getting numerical input.
We need to give a name to the value that the user enters so that the program can remember it and use it in the second line. The name
we use is temp and we
use the equals sign to assign the user’s value to temp.
The second line uses the print function to print out the
conversion. The part in quotes is another string and will appear to your program’s user exactly as it appears in quotes here. The second
1.4. TYPING THINGIN
argument to the print function is the calculation. Python will do the calculation and print out the numerical result.
This program may seem too short and simple to be of
much use, but there are many websites that have little utilities that do similar conversions, and their code is not much more complicated than the code here.
A second program Here is a
program that computes the average of two numbers that the user enters:
Python uses indentation of lines for things we’ll learn about soon. On the other hand, spaces in most other places don’t matter. For instance, the following lines have the same effect:
Basically, computers will only do what you tell them,
and they often take things very literally. Python
itself totally relies on things like the placement of commas and parentheses so
it knows what’s what. It is not
very good at figuring out what you mean, so you have to be precise. It will be very frustrating at first, trying to get all of the parentheses and commas in the right places, but after a while it will become more natural. Still, even after you’ve programmed for a long time, you will still miss something. Fortunately, the Python interpreter is pretty good about helping you find your mistakes.
The input function is a simple way for your program to get information from people using your program. Here is an example:
|
The basic structure is
variable name = input(message to user)
The above works for getting text from the user. To get numbers from the user to use in calculations, we need to do something extra. Here is an example:
|
The eval function converts the text entered by the user
into a number. One nice feature
of this is you can enter expressions, like 3*12+5, and eval will compute them for you.
Note If you run your program and nothing seems to be happening, try pressing enter. There is a bit of a glitch in IDLE that occasionally happens with input statements.
Here is a simple example:
print('Hi there')
The print function requires parenthesis around its arguments. In the program
above, its only argument is the string 'Hi there'. Anything
inside quotes will (with a few exceptions) be printed exactly as it appears. In the following, the first
statement will output 3+4,
while the second will output 7.
|
To print several things at once, separate them by commas. Python will automatically insert spaces between them. Below is an example and the output it produces.
|
Optional arguments
There are two optional arguments to the print function. They are not overly important at
this stage of the game, so you
can safely skip over this section, but they are useful for making your output look nice.
sep Python
will insert a space between each of the arguments of the print function. There
is an optional argument called sep, short for separator, that you can use to change that space to some- thing else. For example, using sep=':' would separate the arguments by a
colon and sep='##' would separate the arguments by two pound signs.
|
The value of 3+4 is 7 .
The value of 3+4 is 7.
One particularly useful possibility is to have nothing
inside the quotes, as in sep=''. This says to put no separation between the arguments. Here is an example where sep is useful for getting the output to look nice:
On the first line
On the second line
end The print function will automatically advance to the next line. For instance, the following will print on two lines:
There is an optional argument called end that you can use to keep the print function from advanc- ing to the next line. Here is an example:
Of course, this could be accomplished better with a single print, but we will see later that there are interesting uses for the end argument.
One of the major purposes of a variable is to remember a value from one part of a program so that it can be used in another part of
the program. In the case above, the variable temp stores the value that the
user enters so that we can do a calculation with it in the next line.
In the example below, we perform a calculation and need to use the result of
the calculation in several
places in the program. If we save the result of the calculation in a variable,
then we only need to do the calculation once. This also helps to make the program more readable.
We haven’t discussed if statements yet, but they do exactly what you think they do.
After these four lines of code are executed, x is 4, y is 5 and z is 8. One way to understand something like
this is to take it one line at a time. This is an especially useful technique
for trying to understand more complicated chunks of code. Here is a description of what happens in the code above:
1. x starts
with the value 3 and y starts with the value 4.
2. In line 3, a
variable z is created to equal x+y, which is 7.
3. Then the
value of z is changed to equal one more than it currently equals, changing it
from 7 to 8.
4. Next, x is
changed to the current value of y, which is 4.
5. Finally, y
is changed to 5. Note that this does not affect x.
6. So at the
end, x is 4, y is 5, and z is 8.