DOC PREVIEW
MIT 6 893 - Introduction to Python

This preview shows page 1-2-3-20-21-40-41-42 out of 42 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Introduction to PythonLinuxWorld - New York City - January 2002Guido van RossumDirector of PythonLabs at Zope [email protected]@zope.comSlide ©2001, 2002 Guido van RossumWhy 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 ©2001, 2002 Guido van RossumTutorial 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 beyondSlide ©2001, 2002 Guido van RossumTry 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 canSlide ©2001, 2002 Guido van RossumInteractive “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/272>>> # this is a commentSlide ©2001, 2002 Guido van RossumNumbers• The usual suspects• 12, 3.14, 0xFF, 0377, (-1+2)*3/4**5, abs(x), 0<x<=5• C-style shifting & masking• 1<<16, x&0xff, x|1, ~x, x^y• Integer division truncates :-(• 1/2 -> 0 # 1./2. -> 0.5, float(1)/2 -> 0.5• Will be fixed in the future• Long (arbitrary precision), complex• 2L**100 -> 1267650600228229401496703205376L– In Python 2.2 and beyond, 2**100 does the same thing• 1j**2 -> (-1+0j)Slide ©2001, 2002 Guido van RossumStrings• "hello"+"world" "helloworld" # concatenation• "hello"*3 "hellohellohello" # repetition• "hello"[0] "h" # indexing• "hello"[-1] "o" # (from end)• "hello"[1:4] "ell" # slicing• len("hello") 5 # size• "hello" < "jello" 1 # comparison• "e" in "hello" 1 # search• "escapes: \n etc, \033 etc, \if etc"• 'single quotes' """triple quotes""" r"raw strings"Slide ©2001, 2002 Guido van RossumLists• Flexible arrays, not Lisp-like linked lists• a = [99, "bottles of beer", ["on", "the", "wall"]]• Same operators as for strings• a+b, a*3, a[0], a[-1], a[1:], len(a)• Item and slice assignment• a[0] = 98• a[1:2] = ["bottles", "of", "beer"]-> [98, "bottles", "of", "beer", ["on", "the", "wall"]]• del a[-1] # -> [98, "bottles", "of", "beer"]Slide ©2001, 2002 Guido van RossumMore List Operations>>> a = range(5) # [0,1,2,3,4]>>> a.append(5) # [0,1,2,3,4,5]>>> a.pop() # [0,1,2,3,4]5>>> a.insert(0, 42) # [42,0,1,2,3,4]>>> a.pop(0) # [0,1,2,3,4]5.5>>> a.reverse() # [4,3,2,1,0]>>> a.sort() # [0,1,2,3,4]Slide ©2001, 2002 Guido van RossumDictionaries• Hash tables, "associative arrays"• d = {"duck": "eend", "water": "water"}• Lookup:• d["duck"] -> "eend"• d["back"] # raises KeyError exception• Delete, insert, overwrite:• del d["water"] # {"duck": "eend", "back": "rug"}• d["back"] = "rug" # {"duck": "eend", "back": "rug"}• d["duck"] = "duik" # {"duck": "duik", "back": "rug"}Slide ©2001, 2002 Guido van RossumMore Dictionary Ops• Keys, values, items:• d.keys() -> ["duck", "back"]• d.values() -> ["duik", "rug"]• d.items() -> [("duck","duik"), ("back","rug")]• Presence check:• d.has_key("duck") -> 1; d.has_key("spam") -> 0• Values of any type; keys almost any• {"name":"Guido", "age":43, ("hello","world"):1, 42:"yes", "flag": ["red","white","blue"]}Slide ©2001, 2002 Guido van RossumDictionary Details• Keys must be immutable:– numbers, strings, tuples of immutables• these cannot be changed after creation– reason is hashing (fast lookup technique)– not lists or other dictionaries• these types of objects can be changed "in place"– no restrictions on values• Keys will be listed in arbitrary order– again, because of hashingSlide ©2001, 2002 Guido van RossumTuples• key = (lastname, firstname)• point = x, y, z # parentheses optional• x, y, z = point # unpack• lastname = key[0]• singleton = (1,) # trailing comma!!!• empty = () # parentheses!• tuples vs. lists; tuples immutableSlide ©2001, 2002 Guido van RossumVariables• No need to declare• Need to assign (initialize)• use of uninitialized variable raises exception• Not typedif friendly: greeting = "hello world"else: greeting = 12**2print greeting• Everything is a "variable":• Even functions, classes, modulesSlide ©2001, 2002 Guido van RossumReference Semantics• Assignment manipulates references• x = y does not make a copy of y• x = y makes x reference the object y references• Very useful; but beware!• Example:>>> a = [1, 2, 3]>>> b = a>>> a.append(4)>>> print b[1, 2, 3, 4]Slide ©2001, 2002 Guido van Rossuma1 2 3ba1 2 3b4a = [1, 2, 3]a.append(4)b = aa1 2 3Changing a Shared ListSlide ©2001, 2002 Guido van Rossuma1ba1ba = 1a = a+1b = aa12Changing an Integerold reference deletedby assignment (a=...)new int object createdby add operator (1+1)Slide ©2001, 2002 Guido van RossumControl Structuresif condition: statements[elif condition: statements] ...else: statementswhile condition: statementsfor var in sequence: statementsbreakcontinueSlide ©2001, 2002 Guido van RossumGrouping IndentationIn Python:for i in range(20): if i%3 == 0: print i if i%5 == 0: print "Bingo!" print "---"In C:for (i = 0; i < 20; i++){ if (i%3 == 0) { printf("%d\n", i); if (i%5 == 0) { printf("Bingo!\n"); } } printf("---\n");}0Bingo!---------3---------6---------9---------12---------15Bingo!---------18------Slide ©2001, 2002 Guido van RossumFunctions, Proceduresdef name(arg1, arg2, ...): """documentation""" # optional doc string statementsreturn # from procedurereturn expression # from functionSlide ©2001, 2002 Guido van RossumExample Functiondef gcd(a, b): "greatest common divisor" while a != 0: a, b = b%a, a # parallel assignment return b>>> gcd.__doc__'greatest common divisor'>>> gcd(12, 20)4Slide ©2001, 2002 Guido van RossumClassesclass name: "documentation"


View Full Document

MIT 6 893 - Introduction to Python

Documents in this Course
Toolkits

Toolkits

16 pages

Cricket

Cricket

29 pages

Quiz 1

Quiz 1

8 pages

Security

Security

28 pages

Load more
Download Introduction to Python
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Python and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Python 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?