Subsections

Hello, World

What you should know

You should know how to edit programs in a text editor or IDLE, save them to disk (floppy or hard or flash) and run them once they have been saved.

Printing

Programming tutorials since the beginning of time have started with a little program called Hello, World! So here it is:


print("Hello, World!")

If you are using the command line to run programs then type it in with a text editor, save it as hello.py and run it with “python hello.py” (or “python3 hello.py” if using Python 3).

Otherwise go into IDLE, create a new window, and create the program as in section 1.4.

When this program is run here's what it prints:

Hello, World!

Now I'm not going to tell you this every time, but when I show you a program I recommend that you type it in and run it. I learn better when I type it in and you probably do too.

Now here is a more complicated program ( note that there are two underscores before the future and two after):


from __future__ import division, print_function
print("Jack and Jill went up a hill")
print("to fetch a pail of water;")
print("Jack fell down, and broke his crown,")
print("and Jill came tumbling after.")

When you run this program it prints out:

Jack and Jill went up a hill
to fetch a pail of water;
Jack fell down, and broke his crown,
and Jill came tumbling after.

When the computer runs this program, first it sees the line:

from __future__ import division, print_function

This line tells Python 2.6 and 2.7 to act more like Python 3. Most of the later programs in this tutorial will not work in Python 2 without this line. Feel free to copy this line from program to program instead of typing it in each time. If your program only needs to run in Python 3 and up, this line is not needed. If you are doing more complicated programs, there are other future statements available.

When the computer runs this program, next it sees the line:

print("Jack and Jill went up a hill")
so the computer prints:
Jack and Jill went up a hill

Then the computer goes down to the next line and sees:

print("to fetch a pail of water;")

So the computer prints to the screen:

to fetch a pail of water;

The computer keeps looking at each line, follows the command and then goes on to the next line. The computer keeps running commands until it reaches the end of the program.

Expressions

Here is another program:
from __future__ import division, print_function
print("2 + 2 is", 2+2)
print("3 * 4 is", 3 * 4)
print(100 - 1, " = 100 - 1")
print("(33 + 2) / 5 + 11.5 = ", (33 + 2) / 5 + 11.5)

And here is the output when the program is run:


2 + 2 is 4
3 * 4 is 12
99  = 100 - 1
(33 + 2) / 5 + 11.5 =  18.5

As you can see Python can turn your thousand dollar computer into a 5 dollar calculator.

Python has seven basic operations for numbers:

Operation Symbol Example
Exponentiation ** 5 ** 2 == 25
Multiplication * 2 * 3 == 6
Division / 15 / 2 == 7.5
Integer Division // 14 // 3 == 4
Remainder % 14 % 3 == 2
Addition + 1 + 2 == 3
Subtraction - 4 - 3 == 1

Notice that there are two different division rules. If you use // then it returns the integer result (15 // 2 == 7). If you use / then it returns the decimal result (15 / 2 == 7.5). The following program show this:

from __future__ import division, print_function
print("15 / 2 = ", 15 / 2)
print("15 // 2 = ", 15 // 2)
print("15 % 2 = ", 15 % 2)
print()
print("15.0 / 2.0 =", 15.0 / 2.0)
print("15.0 // 2.0 =", 15.0 // 2.0)
print("15.0 % 2.0 =", 15 % 2.0)
print()
With the output:
15 / 2 =  7.5
15 // 2 =  7
15 % 2 =  1

15.0 / 2.0 = 7.5
15.0 // 2.0 = 7.0
15.0 % 2.0 = 1.0

The order of operations is the same as in math:

  1. parentheses ()
  2. exponents **
  3. multiplication *, division \, integer division \\, and remainder %
  4. addition + and subtraction -

Talking to humans (and other intelligent beings)

Often in programming you are doing something complicated and may not in the future remember what you did. When this happens the program should probably be commented. A comment is a note to you and other programmers explaining what is happening. For example:

from __future__ import division, print_function
#Not quite PI, but an incredible simulation
#repr forces the full precision to be displayed
print(repr(22/7))
Notice that the comment starts with a #. Comments are used to communicate with others who read the program and your future self to make clear what is complicated.

Examples

Most chapters contain examples of the programming features introduced in the chapter. You should at least look over them see if you understand them. If you don't, you may want to type them in and see what happens. Mess around them, change them and see what happens.

Denmark.py

from __future__ import division, print_function
print("Something's rotten in the state of Denmark.")
print("                -- Shakespeare")

Output:

Something's rotten in the state of Denmark.
                -- Shakespeare

School.py

from __future__ import division, print_function
#This is not quite true outside of USA
# and is based on my dim memories of my younger years
print("Firstish Grade")
print("1+1 =", 1+1)
print("2+4 =", 2+4)
print("5-2 =", 5-2)
print()
print("Thirdish Grade")
print("243-23 =", 243-23)
print("12*4 =", 12*4)
print("12/3 =", 12//3)
print("13/3 =", 13//3, "R", 13%3)
print()
print("Junior High")
print("123.25-62.75 =", 123.25-62.75)
print("(4+3)*2 =", (4+3)*2)
print("4+3*2 =", 4+3*2)
print("3**2 =", 3**2)
print()

Output:

Firstish Grade
1+1 = 2
2+4 = 6
5-2 = 3

Thirdish Grade
243-23 = 220
12*4 = 48
12/3 = 4
13/3 = 4 R 1

Junior High
123.25-62.75 = 60.5
(4+3)*2 = 14
4+3*2 = 10
3**2 = 9

Exercises

Write a program that prints your full name and your birthday as separate strings.

Write a program that shows the use of at least 4 of the 7 math functions.