The first lesson about PythonProgramming by RadomirDopieralski
The lesson will be in english, I can also explain things in polish if there are any polish students.
Where?
gobby 0.3 x c141-46.icpnet.pl port 6522
When?
What will you need?
Rough roadmap:
python 01
2006-06-28 14:30 CEST
Participants
sh: great, by the bell
ma: saved 
sh: Let’s get it rolling.
First of all, computers are very stupid. Really dummy. They’ll do whatever you tell them to do, but they will do it literally, without checking anything.
A computer program is really a list of things to do. Like a recipe for your favorite sauce, for example.
ma: I’m a bit slow at the moment, gimme some minutes to groove in, k?
sh: Ok, do you have python isntalled? Open a terminal and type ‘python’.
ma: got kubuntu. 2.4’s in it I guess.
You should see this: Python 2.4.3 (#2, Apr 27 2006, 14:43:58) [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
ma: I see that. 
test cc&p
mattis@COMP1:~$ python Python 2.4.3 (#2, Apr 27 2006, 14:43:58) [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
Ok, now you can start giving your computer commands.
>>> print 'Hello world!'
I’ll mark the things to type in the window with ‘>>>’, ok?
ma: that’s what I did with Lion once and we’ll do it again, but: Where is the participants list. I wanna see if Dude joins.
Maybe we let Dude play around a bit and explain. sign at the beginning of a new contrivution makes sense, as when exporting the page to wiki we lose the colors. Generally wikiformating makes sense here, bold = bold for example.
sh: Yeah
ed: nice idea. how do i?
sh: just edit what’s there already, type something, add comments, move text around, whatever
ed: you mean grammatical errors and typos and stuff? But how do i edit if i dont kow what i’m editing? I think i’m a bit confused with the whole principle behind this.
sh: it’s just a text document, you edit it normally. We will try to make it into a programming lesson in a while, but until then – just play. Want me to draw a sheep? 
_@**@ Bee!
{ (^^) '
VvVvVVvVvVvVvVvVvvvma: ed, you’re in England? Me Italy at the moment.
ed: Nope. guess the 2nd biggest friggin populated country in the world? Nice sheep 
ma: India?
ed: Yep
ma: Cool! What hour is it? early - Confused, bur likes big balls. Let’s try to keep the chronological sequence for the chat.It gets beterunderstandable for readers of the later wiki-page theis is likely to be copied in afterwards.
ed: 1825hrs or 06:25PM
ed: 
ma: gobby is not perfect. It crashes sometimes, maybe in 1 second maybe not in 5 hours. Not to loose too much, everybody should like every five mintes save the page on his hard disk. You can restore it after reloging in then.
ed: OK. Now what?
sh: Let’s continue the lesson. EvilDude?, do you have the python running?
ed: I installed the 3 links on the site. That’s it. And I have currently opened this document.
sh: open a terminal and type ‘python’ please
ed: Where is the terminal window? I’m in Windows XP right now
ma: Here we have another reason for getting your vids on Ubuntu work ed 
sh: Start → run… , then type ‘cmd.exe’, and a black window will appear in the window, type ‘python’.
ma: blush didn’t remember windows has a terminal. hihi.
ed: Oh that. Used to calling it DOS prompt. heh
ed: OK. It says not recognised command. Where do I install it from if its needed?
sh: you installed python? in C
program files\python ? Try ‘cd “program files\python”’ and then ‘python’ again. checking the links
ed: Not specifically. I only installed the 3 installers on the site which were linked to. Checked it. The python dir doesn’t exist.
ed: Any help?
http://oddwiki.org/odd/TeachMe/Python_01 I added a link to the python page from where you can download it and then install. http://www.python.org/ftp/python/2.4.3/python-2.4.3.msi ← you need this
ed: Downloading it right now.
ma: just added my location on top. Noone ha to, but I think it creates a good feel.
ed: What the heck!
ma: New Delhi. dream - MoonEdit, which is the pretty reliable, supersmall, rather ugly and proprietary collab-editor has a type sound function (in the win version). You would have heard me editing on the top of the document without seeing the edit.
ed: Which reminds me to ask you a question. Have you seen the underground secret (supposedly) tunnels in Venice? Are they real?
ed: Why dream? It isnt so great. Too urbanised. Rural India is a place to travel.
sh: Is there really any underground in Venice? it would be flooded…
ed: I know. But I’ve heard a lot that there are miles of underground mazes there. And in such a way that they’re not exactly flooded with the water.
ma: I don’t know about “mazes”.
ed: Downloaded. But it’s installing in C
Python24\ OK?
sh: Anywhere is fine. Run python.exe.
ed: OK. Did. Now?
sh: do you see the ‘>>>’ prompt?
ed: Yep
sh: type:
>>> print 'Hello world!' and press enter
the computer just did what you told it to do, right? this is called a ‘statement’, and it consists of two parts, the command, which is ‘print’, and the data, or paramenter, which is ‘Hello world!’.
ed: Kind of like C++, except there it is cout command. Got a basic knowledgde of C and C++. Not C# though.
sh: ok, then I’ll increase the level 
ok, next are variables.
>>> x = 2 >>> print x
ed: Did. How long does it store the variable there?
sh: since it’s in the global scope now, it stores it until you exit python
ma: how to? Not that I wanna finish.
sh: ctrl+d exits the console. Python has two modes of operation. The console, which you see now, where you can type the statements and they get executed as you press enter. ANd there is the normal mode, in which it executes commands read from a file.
whatever you write in the console, you can write a text file with this, and when you do ‘python name-of-the-file’, the python will read that file and execute all the commands inside, one by one, just as if you entered it in the console.
let’s try it.
make a file hello.py (python files have usually .py extension)
and write “print ‘Hello world!’” in it.
ed: How do I create a file?
sh: Use notepad or any other text editor. Word is a little big for that, I think.
ed: Ok. So i type all the commands there and save it and run through Python, right?
sh: yes, in Windows you can also double-click the file, the extension .py is assosiated with python already.
it’s best to run the hello.py in a terminal, because otherwise the window will disappear too fast to see anything.
ed: Ok. Now how do I run it. I placed the file in C
Python24. If I type the file name will it check that folder as default?
sh: don’t know, if it’s in your PATH, then yes, but don’t know if windows python installer sets the path. For now, just use ‘python hello.py’ while being in that directory. Mattis, have you written hello.py? 
ed: Ran the file. Is ok.
ma: yes. How do i exeute it
sh: mattis, ‘python hello.py’, if you add ‘#!/usr/bin/python’ as the first line of the file, then you can run it directly, ‘./hello.py’.
ma: #!/usr/bin/python? added that as 1tr line. Doesn’t run. wait … neither ./hello.py. Second line is: print hello world!
sh: you have to make it executable too, ‘chmod +x hello.py’. What does it say?
ed: When I type the chmod command it’s not working. Where do I type it?
sh: Windows doesn’t have this
In windows, all the files are always executable, provided they have the right extension. Linux doesn’t look at the extensions at all – it’s just a name for it.
In Python , everything after a # is a comment, just like // in c++. But the #! as the first line is a special comment in Linux. It’s called ‘hashbang’ and it tells what program to use to run the file.
sheep@ghostwheel:~$ vim hello.py #!/usr/bin/python print 'Hello world!' sheep@ghostwheel:~$ chmod +x hello.py sheep@ghostwheel:~$ ./hello.py Hello world!
ed: Is #! like defining libraries in C like <stdio.h> ?
sh: no, it’s not something specific to python, it’s a Linux thing, you can put it in any text file to make it a script, for python it’s just a comment. For example you can use ‘#!/bin/cat’ to make the file list itself when executed.
Mattis, are you in the directory where the file is?
ma: yes. chmod was accepted but the script doesn’t run.
sh: what does it say when you try to run the file? does it display anything at all? Like somthing like this:
File "hello.py", line 2
print hello world!
^
SyntaxError: invalid syntaxed: There are libraries in Python, right. because I just noticed the error said this → File “<stdin>”, line 1, in ?
sh: There are libraries, of course, but <stdin> has nothing to do with it, it’s a name of a ‘fake’ file, standard input, which is in fact your keyboard. When you run python in the console mode, it thinks your keyboard is a file from which it reads your program.
ma: >>> ./hello.py File “<stdin>”, line 1 ./hello.py ^ SyntaxError?: invalid syntax
>>>
sh: run it in terminal, not in python
just open a second terminal. you can also use your file manager to browse to that directory and double-click the file.
Maybe I’ll write ‘$ command’ to show that the command is to be executed in terminal, and ‘>>> command’ when it is to be executed in python.
To run hello.py, use:
$ chmod +x hello.py $ ./hello.py
ma: File “./hello.py”, line 3 print Hello world! ^
Yes, that’s a python error, one of the few real ones. You see it, because you forgot to quote the text. It should be:
print 'Hello world!'
ed: What is chmod exactly? Does it create an executable or for that particular time just run the file.
sh: it sets the file permissions. In windows you have read and write permissions, in Linux you have read, write and execute.
python uses quotes to delimit text, you can use any quotes you like:
'like this' or "like this", you can also use 'triple' quotes: """like this""" and '''like this'''. Triple quotes allow you to use the quote character or newline inside: """like "this" """
ed: 1 quote “ is taken as a command, but 2 quotes should actually be 1 command and 1 quote, why 3?
sh: commands are not quoted. Only values are. “” ← this is an empty string, empty text. You need “”” to show that you start a triply-quoted text.
ma: mattis@COMP1:~/mein_Zeugs/python$ ./hello.py Hello world! yo.
sh: great. now, let’s go back to the python for a while.
>>> x = "this is sample text" >>> y = 3 >>> z = 3.1 >>> print x, y, z
We have three variables there. x is a string, y is an integer, and z is a floating point number. Python treats all of them in a similar way. The variables themselves don’t have ‘type’.
>>> x = "some text" >>> print x >>> x = 4 >>> print x
You can assign values to the same variable no matter what value it had before.
>>> x = 1 >>> y = 2 >>> print x+y >>> x = "text" >>> print x+y
But the values have types! The last command in this example will fail. You can’t add a number and a string!
This is a second kind of error message, an “exception”:
Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: cannot concatenate 'str' and 'int' objects
ed: But what’s the point if the values get overwritten. or do they?
ed: Ok. Let me try with an example to understand.
>>> x = "text1" >>> y = 2 >>> x = "text2" >>> y = "texty" >>> print x+y
ed: Ok. I don’t know the command to print these. But tell me if I’m correct. The X variable with “text2” will overwrite the variable with value “text1” but y will remain 2 different variables cos they are of 2 different types. Is that it?
sh: variables don’t have types, that’s the point. It’s values that have types.
ed: Hmm.. Ok. But what I don’t understand is two values linking to one x. Now what is python supposed to pick and print?
sh: well, the value of x changes, obviously, the old value is lost. Python will print the last value assigned to the variable.
>>> x = "some text" >>> y = x >>> x = 3 >>> print x, y
You can use the variables in assignments, but what is passed is the value, not the ‘reference’, for example, when you change x to 3, the y doesn’t change anymore. It’s like when you do ‘y = x’, the value of x is copied. It’s not, but it seems so.
You can do some pretty advance math with python. In fact, if you skip the ‘print’ and just enter any value directly, it will evaluate it and print out:
>>> 2+2 4
You can use python as a calculator. There are some operations: + add - substract * multiply ** power / divide % modulo etc.
>>> 3**2 9
>>> 8**(1.0/3) 2.0
You have to be careful, because 1/2 is something different than 1.0/2.
>>> 1/2 0 >>> 1.0/2 0.5
The first one operates on integers, and can only give integer results, so the result is rounded down to the nearest integer.
ed: Aaah. I was thinking that, only that both the calculation seem the same. So if I type 1.0 then Python will only compute in integers?
sh: 1.0 is a floating point number, so it will compute in floating point numbers, if you type 1 (which is an integer) then it will compute in integers. Sorry, not the first, 1/2.0 gives you 0.5 too.
Python makes a pretty comfortable and powerful calculator, I often use it as one 
Now, let’s see how to use libraries. Python needs libraries almost for everything. Print is pretty much the only thing you get without libraries 
ma: It’s interesting. Good we export to teach-me-wiki and can go through it later again.
books on python:
Define external redirect: EvilDude SyntaxError