Introduction to Python
LinuxWorld - New York City - January 2002
Guido van Rossum Director of PythonLabs at Zope Corporation guido@python.org guido@zope.com
Why Python?
• Have your cake and eat it, too: Productivity and readable code • VHLLs will gain on system languages (John Ousterhout) • "Life's better without braces" (Bruce Eckel)
Slide 2
©2001, 2002 Guido van Rossum
Tutorial Outline
• interactive "shell" • basic types: numbers, strings • container types: lists, dictionaries, tuples • variables • control structures • functions & procedures • classes & instances • modules & packages • exceptions • files & standard library • what's new in Python 2.0 and beyond
Slide 3
©2001, 2002 Guido van Rossum
Try It Out!
• If you brought a laptop into the classroom, feel free to play along • Download Python from www.python.org • Any version will do for this class
– By and large they are all mutually compatible – Recommended version: 2.1.1 or 2.2 – Oldest version still in widespread use: 1.5.2 – Avoid 1.6/1.6.1 if you can – When using 2.0 or 2.1, upgrade to 2.0.1 / 2.1.1 – 2.1.2 is coming soon!
• Use IDLE if you can
Slide 4 ©2001, 2002 Guido van Rossum
Interactive “Shell”
• Great for learning the language • Great for experimenting with the library • Great for testing your own modules • Two variations: IDLE (GUI), python (command line) • Type statements or expressions at prompt:
>>> print "Hello, world" Hello, world >>> x = 12**2 >>> x/2 72 >>> # this is a comment
Slide 5
©2001, 2002 Guido van Rossum
Numbers
• The usual suspects
• 12, 3.14, 0xFF, 0377, (-1+2)*3/4**5, abs(x), 0 1267650600228229401496703205376L
– In Python 2.2 and beyond, 2**100 does the same thing
• 1j**2 -> (-1+0j)
Slide 6
©2001, 2002 Guido van Rossum
Strings
• "hello"+"world" • "hello"*3 • "hello"[0] • "hello"[-1] • "hello"[1:4] • len("hello") • "hello" < "jello" • "e" in "hello" "helloworld" "h" "o" "ell" 5 1 1 # concatenation # indexing #...