Google's automatic chat logging is nice too. My first online python
tutorial for someone who never saw it before (sorry for not being in
english):
14/09/08
00:50 KALEL: I'm on Phyton Shell
00:52 me: cool
let's go
type it: 2
just to get rid of your fears... :)
KALEL: Hah hah hah hah
me: really
1+2
00:53 KALEL: Stupendous
AND???
me: type( 1 )
type( "foobar" )
00:54 KALEL: Good
me: Python is dinamically typed, you don't declare types: a = 2
a+3
KALEL: Ok... how do I refer to the values????
00:55 me: a
?
KALEL: I mean, define some constant???
me: no constants, all variable. of course, there are scope rules
00:56 modules, classes and functions
let's go by steps
KALEL: Ok Morpheus
me: type there: dir() to know what it is in the current scope
KALEL: ['__builtins__', '__doc__', '__name__', 'a']
me: exact
it's a list: []
00:57 KALEL: I see
me: type dir( [] ) to know the methods of a list object
KALEL: Cool
me: show
00:58 KALEL: Waitaminute:
dir is a list, right?
me: it's a function returning a list
ls = dir()
KALEL: OK.
me: ls.sort
00:59 KALEL: Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
ls.sort
NameError: name 'ls' is not defined
me: you did not define it?
ls = dir()
01:00 KALEL: <built-in method sort of list object at 0x00C29878>
me: yes
ls.sort()
forgot the parentheses to call
;)
ls.index('a')
01:01 for i in dir(): print i
01:02 KALEL: stopped
01:03 me: one more ENTER
BTW: blocks in python are idented by tabs
KALEL: ok
me: no {} or begin end
wait
01:04 KALEL: wonderful
01:05 Ok, but when you have a single command, you don't need {} nor
begin end
01:06 me: true, for that same reason I put print in the same line
for and if end in :
KALEL: 0x0
me: if foo:
this
else:
01:07 that
not forgetting tab indenting
KALEL: good
me: if you get another nested if, more indenting
KALEL: This is very good
01:08 The guy does not get lost
me: let's define a function
KALEL: And you don't need to worry about non-practical details
me: def fact( n ):
if n < 2: return 1
else:
return n+fact(n-1)
01:10 you can, of course, write a program in a text file and import it
01:11 KALEL: good
Guess I can already solve an Euler
01:12 me: say
type
def fact( n ):
"""Factorial of n"""
if n < 2:
return 1
else:
return n*fact(n-1)
into a text file and save as euler.py
01:13 notice documentation as part of a function
01:14 KALEL: Just did it.
Opened a python instance
01:15 me: still open?
KALEL: Yes
me: so, type this
euler.fact( 5 )
01:16 KALEL: FAIL
me: what?
KALEL: typed on the shell and didnt recongnize
me: import euler
now should go
euler.fact(5)
01:22 KALEL: Now it works!
me: I forgot the import, no? :P
hahahaha
KALEL: yep
me: sorry
KALEL: Will get more coffee
me: huhauhua, javaman
01:24 KALEL: Got coke
me: good
KALEL: while is in the same vein???
01:26 me: i = 1
while i<10:
... print i
... i += 1
without the dots, sure
01:30 KALEL: any mod function??????
01:32 me: %
3%2
KALEL: Yes, thanks... I looked up on help
01:33 Any easy way to sum var to itself + new result??? say: sp = sp + p1
01:34 me: sp += 3
KALEL: sp +=p1
That?????
me: yep
KALEL: Awesome.
01:35 me: converting a previous problem to Python? :)
KALEL: No... Solving the second one
me: ok
01:39 KALEL: how to end while???
indenting???
I mean, how do I close the block?
01:40 Forget
me: ident all commands inside while
ok
ending it is newline, plainly
01:43 KALEL: finally!!!!
Very good Phyton
GOT IT!!!!!!
me: way to go
very easy, ain't it?
01:44 nothing like removing away everything non-essential
KALEL: while sp<=4000000:
p1=p1+p2
if p1%2 == 0: sp += p1
p2=p2+p1
if p2%2 == 0: sp += p2
me: hehe
KALEL: of course, defining p1 = 2
p1 = 1 and p2 = 2
me: I was worried you would use = to compare
KALEL: sp = 2
me: exactly
KALEL: a bug
01:45 me: you don't even need a fib function
KALEL: Then I remembered = was used for attribution
me: hehe
common mistake among Pascal programmers
KALEL: COOL
me: not even in need for OO too! :O
haha
01:46 KALEL: too much...
Used OOPS!!
me: OOPS?
KALEL: OOPS
01:47 you try, if it fails: OOPS!
01:50 KALEL: Thank you.
I'll continue studying... you wait for me... now the force if with me
too.
me: we're right here
huhauhuha
01:51 nice, young Padawan
01:52 KALEL: Thanks, Jedi Master!!!!
01:55 me: BTW, my solution will make you go nuts:
def p2():
print "Find the sum of all the even-valued terms in the Fibonacci
sequence which do not exceed 4 million."
sum = 0
fib = 2
pfib = 1
while (fib<4000000):
if fib%2==0: sum+=fib
fib, pfib = fib+pfib,fib
return sum
01:56 check out the excellent variables swap near the last line... :)
01:57 KALEL: AWESOME!
me: yeah, no need for intermediary temp variables
01:58 I see you have a strange gleaming in the eyes, young disciple
:)
01:59 KALEL: huah huah huah
02:01 me: you may also reload a module after each change
reload( euler )
--
a game sig: http://tinyurl.com/d3rxz9
--
http://mail.python.org/mailman/listinfo/python-list