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

What you should know
Once you've read and mastered this chapter, you should know how to edit programs in a text editor or IDLE, save them to the hard disk, 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:

If you are using the command line to run programs then type it in with a text editor, save it as  and run it with

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

When you run this program, the output will look as follows:

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 yourself and run it (as opposed to copy/pasting it). I tend to learn and internalize the learning material better when I type it in, and you will probably too!

Now here is a more complicated program:

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:

so the computer prints:

Jack and Jill went up a hill

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

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
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 function called. The function's name -  - is followed by parentheses containing zero or more arguments. So in this example

there is one argument, which is. 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. The combination of a function and parentheses with the arguments is a function call.

A function and its arguments are one type of statement that python has, so

is an example of a statement. Basically, you can think of a statement as a single line in a program.

That's probably more than enough terminology for now.

\n in Printing
, or newline in printing makes the strings after the \n in a new line, it is also an escape character, here is an example:

Here is the output:

Hello, World! What should I do?

It can be used to put a bunch of strings that are supposed to be on different lines into 1 print statement instead of making multiple print statements

The print statement also sort of uses  even if you do not use it for example:

is actually Well, there is a difference if you do it manually, but python actually adds a newline "behind the scenes" at the end of the string

Expressions
Here is another program:

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 thousand-dollar computer into a five-dollar calculator.

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

The first argument is the string  and the second argument is the arithmetic expression , which is one kind of expression.

What is important to note is that a string is printed as is (without the enclosing double quotes), but an expression is evaluated, or converted to its actual value.

Python has seven basic operations for numbers:

Notice that there are two ways to do division, one that returns the repeating decimal, and the other that can get the remainder and the whole number. The order of operations is the same as in math: So use parentheses to structure your formulas when needed.
 * parentheses
 * exponents
 * multiplication, division  , integer division  , and remainder
 * addition  and subtraction

Commenting in Python
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:

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 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:

Examples
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

Output:

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

School.py

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.56 - 62.12 = 61.44 (4 + 3) * 2 = 14 4 + 3 * 2 = 10 3 ** 2 = 9

Exercises

 * 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 7 arithmetic operations.