Eris
07-29-2009, 01:02 PM
This is the first part of the python tutorial. It will cover
Python, what to install and how.
Printing text.
Numbers.
This part will be pretty short and not actually get very far in explaining the language. The main focus will be on getting you guys set up and familiarized with IDLE. The tutorial will be really slow and easy these first parts, but be patient, we'll get to the good stuff soon enough.
1. How to install Python
We will be using python 3 in this tutorial. To download it, go to http://python.org/download/releases/3.1/, and under the "Download" heading, select the appropriate file. If you are using Windows, you'll want the one that says "Windows x86 MSI Installer" in all likelihood. After you've downloaded it, run it and install python.
Once installation is done, sift through your start menu or equivalent, and under a newly created Python folder you should find something called "IDLE". IDLE is a development environment for Python, so you'll want to start this program.
You will be presented with a window that says something along the lines of
Python 3.1 (r31:73572, Jul 29 2009, 13:15:15)
[GCC 4.3.2] on linux2
Type "copyright", "credits" or "license()" for more information.
>>>
Great, you're all set up.
2. Printing text.
Let's write a program. According to time honored tradition, we want it to say "Hello World". In IDLE, after ">>>", type
>>> print("Hello World");
Press return, and you should see the following:
>>> print("Hello World");
Hello World
>>>
You have now finished your first program. Great.
While the interactive prompt is all good and well for short 2 or 3 line programs, you will want to use the editor in the future. That way you can also save your programs. To start writing a new program, select "New Window" in the "File" menu. Let's print some more strings in this new program. Copy the following into the window that just opened.
print("Hello world again");
print("I've got amnesia");
print("I can't remember who I've greeted");
print("Hello world");
print("Did I say hello world?");
print("Oh, I almost forgot...");
print("Hello world");
To run it, press F5. The first time you press F5, it will ask you to save it somewhere. Name the file "tutorial0a.py", and save it somewhere. After you've saved it, it will run the program. The output appears in the window you first said "Hello World" in. It should say
>>> ================================ RESTART ================================
>>>
Hello world again
I've got amnesia
I can't remember who I've greeted
Hello world
Did I say hello world?
Oh, I almost forgot...
Hello world
>>>
To dissect this program, ponder the last line:
print("Hello World");
The print part is called a function. In this case, you're sending "Hello World" to this function, which prints the message on the screen. We'll talk about writing custom functions later, but for now, we'll stick with the built in stuff.
"Hello World" is a string. Anything between two citation marks is a string.
Functions can take several arguments separated by commas. Append the following to your program, to play with this feature:
print("---");
print("One");
print("One", "Two");
print("One", "Two", "Three");
Again, press F5 to run it. Press OK when it asks you to save it, and in the main IDLE window, you should now see...
>>> ================================ RESTART ================================
>>>
Hello world again
I've got amnesia
I can't remember who I've greeted
Hello world
Did I say hello world?
Oh, I almost forgot...
Hello world
---
One
One Two
One Two Three
>>>
3. Numbers
Let's learn how to do something actually useful with python. It turns out that just out of the box, it's a pretty powerful calculator. You can just type expressions straight into the IDLE main window, and find the results automatically. Example:
>>> 1+3;
4
>>>
That's nice, but we'll work in a file as usual. Open a new window, and enter the following:
print("Addition");
print("1+2 =", 1+2);
print("Subtraction");
print("1-2 =", 1-2);
print("Multiplication");
print("1*2 =", 1*2);
print("Division");
print("1/2 =", 1/2);
print("Integer division");
print("1//2 =", 1//2);
print("Remainder of division");
print("1%2 =" , 1%2);
print("Power");
print("2**3 = 2 * 2 * 2 =" , 2**3);
Name the file "tutorial0b.py" and run it. You now have a cheat sheet for all mathematical operations in python.
Note that it is fully possible to print numbers as well as strings. As in regular mathematics, expressions like these are evaluated in the following order:
**
* and / and % and //
+ and -
You can use parentheses if you want to change this order. Append the following to your program to illustrate this fact:
print("---");
print("2 * 3 + 1 =", 2 * 3 + 1);
print("2 * (3 + 1) =", 2 * (3 + 1));
When the program sees these lines, they it will evaluate them as following:
2 * 3 + 1 = 6 + 1 = 7
2 * (3 + 1) = 2 * 4 = 8
Problems
In which order will python evaluate the following:
1 + 2 * 3;
1 + 2 + 3 / 4;
1 / 4 * 2;
1 + 2 - 1;
If you don't know, test them. But give them a chance before you do.
In closing, I want to discuss types. I've already mentioned strings, and in the previous part, I've discussed numbers. These are two of the types in python*. You can't do mathematics with a string, even if the string contains a number. Some mathematical operators do work, but they probably do not do what you expect them to do.
Try running the following program, for example:
print("4" * 3);
print("5" + "3");
It will print "444" and "53"
* this is not strictly true, but I'll elaborate on that in some later section.
That's it for now. Unless I'll get to variables and code flow in the next section. If I've been unclear about something, feel free to ask questions.
Python, what to install and how.
Printing text.
Numbers.
This part will be pretty short and not actually get very far in explaining the language. The main focus will be on getting you guys set up and familiarized with IDLE. The tutorial will be really slow and easy these first parts, but be patient, we'll get to the good stuff soon enough.
1. How to install Python
We will be using python 3 in this tutorial. To download it, go to http://python.org/download/releases/3.1/, and under the "Download" heading, select the appropriate file. If you are using Windows, you'll want the one that says "Windows x86 MSI Installer" in all likelihood. After you've downloaded it, run it and install python.
Once installation is done, sift through your start menu or equivalent, and under a newly created Python folder you should find something called "IDLE". IDLE is a development environment for Python, so you'll want to start this program.
You will be presented with a window that says something along the lines of
Python 3.1 (r31:73572, Jul 29 2009, 13:15:15)
[GCC 4.3.2] on linux2
Type "copyright", "credits" or "license()" for more information.
>>>
Great, you're all set up.
2. Printing text.
Let's write a program. According to time honored tradition, we want it to say "Hello World". In IDLE, after ">>>", type
>>> print("Hello World");
Press return, and you should see the following:
>>> print("Hello World");
Hello World
>>>
You have now finished your first program. Great.
While the interactive prompt is all good and well for short 2 or 3 line programs, you will want to use the editor in the future. That way you can also save your programs. To start writing a new program, select "New Window" in the "File" menu. Let's print some more strings in this new program. Copy the following into the window that just opened.
print("Hello world again");
print("I've got amnesia");
print("I can't remember who I've greeted");
print("Hello world");
print("Did I say hello world?");
print("Oh, I almost forgot...");
print("Hello world");
To run it, press F5. The first time you press F5, it will ask you to save it somewhere. Name the file "tutorial0a.py", and save it somewhere. After you've saved it, it will run the program. The output appears in the window you first said "Hello World" in. It should say
>>> ================================ RESTART ================================
>>>
Hello world again
I've got amnesia
I can't remember who I've greeted
Hello world
Did I say hello world?
Oh, I almost forgot...
Hello world
>>>
To dissect this program, ponder the last line:
print("Hello World");
The print part is called a function. In this case, you're sending "Hello World" to this function, which prints the message on the screen. We'll talk about writing custom functions later, but for now, we'll stick with the built in stuff.
"Hello World" is a string. Anything between two citation marks is a string.
Functions can take several arguments separated by commas. Append the following to your program, to play with this feature:
print("---");
print("One");
print("One", "Two");
print("One", "Two", "Three");
Again, press F5 to run it. Press OK when it asks you to save it, and in the main IDLE window, you should now see...
>>> ================================ RESTART ================================
>>>
Hello world again
I've got amnesia
I can't remember who I've greeted
Hello world
Did I say hello world?
Oh, I almost forgot...
Hello world
---
One
One Two
One Two Three
>>>
3. Numbers
Let's learn how to do something actually useful with python. It turns out that just out of the box, it's a pretty powerful calculator. You can just type expressions straight into the IDLE main window, and find the results automatically. Example:
>>> 1+3;
4
>>>
That's nice, but we'll work in a file as usual. Open a new window, and enter the following:
print("Addition");
print("1+2 =", 1+2);
print("Subtraction");
print("1-2 =", 1-2);
print("Multiplication");
print("1*2 =", 1*2);
print("Division");
print("1/2 =", 1/2);
print("Integer division");
print("1//2 =", 1//2);
print("Remainder of division");
print("1%2 =" , 1%2);
print("Power");
print("2**3 = 2 * 2 * 2 =" , 2**3);
Name the file "tutorial0b.py" and run it. You now have a cheat sheet for all mathematical operations in python.
Note that it is fully possible to print numbers as well as strings. As in regular mathematics, expressions like these are evaluated in the following order:
**
* and / and % and //
+ and -
You can use parentheses if you want to change this order. Append the following to your program to illustrate this fact:
print("---");
print("2 * 3 + 1 =", 2 * 3 + 1);
print("2 * (3 + 1) =", 2 * (3 + 1));
When the program sees these lines, they it will evaluate them as following:
2 * 3 + 1 = 6 + 1 = 7
2 * (3 + 1) = 2 * 4 = 8
Problems
In which order will python evaluate the following:
1 + 2 * 3;
1 + 2 + 3 / 4;
1 / 4 * 2;
1 + 2 - 1;
If you don't know, test them. But give them a chance before you do.
In closing, I want to discuss types. I've already mentioned strings, and in the previous part, I've discussed numbers. These are two of the types in python*. You can't do mathematics with a string, even if the string contains a number. Some mathematical operators do work, but they probably do not do what you expect them to do.
Try running the following program, for example:
print("4" * 3);
print("5" + "3");
It will print "444" and "53"
* this is not strictly true, but I'll elaborate on that in some later section.
That's it for now. Unless I'll get to variables and code flow in the next section. If I've been unclear about something, feel free to ask questions.