Non-Programmer's Tutorial for Python 2.6/Hello, World

From Wikibooks, open books for an open world
Jump to navigation Jump to search

What you should know[edit | edit source]

You should know how to edit programs in a text editor or IDLE, save the file and run the file once the files have been saved to your disk.

Printing[edit | edit source]

Programming tutorials since the beginning of time have started with a little program called "Hello, World!"[1] The syntax changed in Python 3.0. If you are using Python 3.0, you should be reading Non-Programmer's Tutorial for Python 3 instead. So here is the Python 2.6 example:

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

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

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:

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 it first 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.

Terminology[edit | edit source]

Now is probably a good time to give you a bit of an explanation of what is happening - and a little bit of programming terminology.

What we were doing above was using a command called print. The print command is followed by one or more arguments. So in this example

print "Hello, World!"

there is one argument, which is "Hello, World!". Note that this argument is a group of characters enclosed in double quotes ("). This is commonly referred to as a string of characters, or string, for short. Another example of a string is "Jack and Jill went up a hill".

A command and its arguments are collectively referred to as a statement, so

print "Hello, World!"

is an example of a statement.

That's probably more than enough terminology for now.

Expressions[edit | edit source]

Here is another program:

print "2 + 2 is", 2 + 2
print "3 * 4 is", 3 * 4
print "100 - 1 is", 100 - 1
print "(33 + 2) / 5 + 11.5 is", (33 + 2) / 5 + 11.5

And here is the output when the program is run:

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

As you can see, Python can turn your six hundred dollar computer into a 2 dollar calculator.

In this example, the print command is followed by two arguments, with each of the arguments separated by a comma. So with the first line of the program

print "2 + 2 is", 2 + 2

The first argument is the string "2 + 2 is" and the second argument is the mathematical expression 2 + 2, which is commonly referred to as an expression.

What is important to note is that a string is printed as is (the string is what is within the double quotes but doesn't include the double quotes themselves. So the string is printed without the enclosing double quotes.) But an expression is evaluated, (in other words, converted) to its actual value.

Python has six basic operations for numbers:

Operation Symbol Example
Power (exponentiation) ** 5 ** 2 == 25
Multiplication * 2 * 3 == 6
Division / 14 / 3 == 4
Remainder (modulo) % 14 % 3 == 2
Addition + 1 + 2 == 3
Subtraction - 4 - 3 == 1

Notice that division follows the rule, if there are no decimals to start with, there will be no decimals to end with. The following program shows this:

print "14 / 3 = ", 14 / 3
print "14 % 3 = ", 14 % 3
print
print "14.0 / 3.0 =", 14.0 / 3.0
print "14.0 % 3.0 =", 14.0 % 3.0
print
print "14.0 / 3 =", 14.0 / 3
print "14.0 % 3 =", 14.0 % 3
print
print "14 / 3.0 =", 14 / 3.0
print "14 % 3.0 =", 14 % 3.0
print

With the output:

14 / 3 = 4
14 % 3 = 2

14.0 / 3.0 = 4.66666666667
14.0 % 3.0 = 2.0

14.0 / 3 = 4.66666666667
14.0 % 3 = 2.0

14 / 3.0 = 4.66666666667
14 % 3.0 = 2.0

Notice how Python gives different answers for some problems depending on whether or not decimal values are used.

The order of operations is the same as in math:

  • parentheses ()
  • exponents **
  • multiplication *, division /, and remainder %
  • addition + and subtraction -

So use parentheses to structure your formulas when needed.

Talking to humans (and other intelligent beings)[edit | edit source]

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:

# Not quite PI, but an incredible simulation
print 22.0 / 7.0    # 355/113 is even more incredible rational approx to PI

Which outputs

3.14285714286

Notice that the comment starts with a hash: #. Comments are used to communicate with others who read the program and your future self to make clear what is complicated.

Note that any text can follow a comment, and that when the program is run, the text after the # through to the end of that line is ignored. The # does not have to be at the beginning of a new line:

# Output PI on the screen
print 22.0 / 7.0 # Well, just a good approximation

Examples[edit | edit source]

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

Denmark.py

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

Output:

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

School.py

# This is not quite true outside of USA
# and is based on my dim memories of my younger years
print "First Grade"
print "1 + 1 =", 1 + 1
print "2 + 4 =", 2 + 4
print "5 - 2 =", 5 - 2
print
print "Third 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.56 - 62.12 =", 123.56 - 62.12
print "(4 + 3) * 2 =", (4 + 3) * 2
print "4 + 3 * 2 =", 4 + 3 * 2
print "3 ** 2 =", 3 ** 2
print

Output:

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

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

Junior High
123.56 - 62.12 = 61.44
(4 + 3) * 2 = 14
4 + 3 * 2 = 10
3 ** 2 = 9

Exercises[edit | edit source]

  1. Write a program that prints your full name and your birthday as separate strings.
  2. Write a program that shows the use of all 6 math functions.


Solution

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

print "Ada Lovelace", "born on", "November 27, 1852"

2. Write a program that shows the use of all 6 math operations.

#Anything along these lines is acceptable:

#Addition
print "2 + 5 = ", 2 + 5

#subtraction
print "9 - 3 = ", 9 - 3

#multiplication
print "3 * 3 = ", 3 * 3

#division
print "90 / 5 = ", 90 / 5

#exponents
print "7 to the power of 2 (squared) = ", 7 ** 2

#remainder
print "the remainder when doing 22 / 9 = ", 22 % 9



Footnotes[edit | edit source]

  1. List of "Hello, world!" programs in many programming languages