The Python Programming Language
description
Transcript of The Python Programming Language
![Page 1: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/1.jpg)
The Python Programming Language
Matt Campbell | Steve Losh
![Page 2: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/2.jpg)
From the Creators…
“The language is named after the BBC show ``Monty Python's Flying Circus''
and has nothing to do with nasty reptiles. Making references to Monty Python skits in documentation is not
only allowed, it is encouraged! “
![Page 3: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/3.jpg)
Origins• Created in the early 1990s by Guido van
Rossum at Stichting Mathematisch Centrum (CWI ) in the Netherlands
• Successor language to ABC• Rossum remains the principle author of
the language today
![Page 4: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/4.jpg)
Overview of the Language• Python is an interpreted language• Like Scheme, it is an interactive
language• Very high-level data types• Code is very human readable
![Page 5: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/5.jpg)
Extensibility• Python is a very extensible language• You can write modules in C to link
python to other binary libraries• You can even link the interpreter itself
into an application written in C and use python as an extension or command language for that application
![Page 6: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/6.jpg)
Lexical Aspects• Input Format:
– Line oriented– White space is not ignored
• Comments:– Denoted by hash mark (#) to end of line
• Delimiters:– End of line
• Keywords:– Reserved
• Names:– Case sensitive– Variable names can consist of letters, numbers, and/or
underscores– Underscores sometimes have special meaning, so their use
is not highly recommended
![Page 7: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/7.jpg)
Data Types• Scalars:– Integer, Float, Boolean
• Aggregate Types– Complex Number, String, List, Dictionary,
Tuple, File, Set• Python is not strongly typed• Python does not require declaration of
variables before their use
![Page 8: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/8.jpg)
Literals• Integers: 2, 4, -3• Floats: 2.0e10, 3.5, .03• Boolean: True, False• Strings: ‘cat’, “cat”• Lists: [12, 3.4, ‘cat’, lambda x: x+3]• Sets: set([12, 3.4, ‘cat’, lambda x: x+3])• Dictionaries: dict = {‘cat': 2, 6: ‘dog’} • Functions: Can be mapped to names via ‘def’ and
‘lambda’ just as in Scheme. They can be returned by functions, placed in lists, etc.
• Files: open('/path/file', ‘r+') • Null: None• ‘_’: holds the most recently returned value
![Page 9: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/9.jpg)
Variable Typing• Variables in Python do not need to be
declared as a specific type– Example:
• A, B = 3, ‘cat’
• A variable’s type is dynamic, and will changed whenever it is reassigned– Example:
• a, b = 1, ‘cat’• a, b = .3, lambda x: x*x
• No such thing as “const” in Python
![Page 10: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/10.jpg)
Quick & Dirty Input
>>> x = int(raw_input("Please enter an integer: "))
![Page 11: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/11.jpg)
Slicing• Aggregate slicing syntax is similar to ICON
Think of indices as pointing between elements in a list.[ ‘cat’, ‘dog’, 3, 4.5 ] 0 1 2 3 4
>>> animals = [‘cat’, ‘dog’, ‘mouse’, ‘bird’]>>> print animals[0:1][‘cat’, ‘dog’]>> print animals[1:][‘dog’, ‘mouse’, ‘bird’]
>>> tmp = list(“Shrubbery”)>>> tmp[:1] = tmp[-7:]>>> tmp[‘r’, ’u’, ’b’, ’b’, ’e’, ’r’, ’y’, ’S’, ’h’, ’r’, ’u’, ’b’, ’b’, ’e’, ’r’, ’y’]
![Page 12: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/12.jpg)
Ranges• Python has a range function to easily
form lists of integers.
>>> range(5) [0, 1, 2, 3, 4]>>> range(2,5) [2, 3, 4]>>> range(0, 10, 2) [0, 2, 4, 6, 8]>>> range(5, 0, -1) [5, 4, 3, 2, 1]
![Page 13: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/13.jpg)
in• The in keyword checks if the given object is
contained within the aggregate.
>>> p = “cat”>>> j = [‘cat’, ‘dog’]>>> p in jTrue>>> ‘a’ in pTrue>>> ‘t’ in p[:2]False
![Page 14: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/14.jpg)
Subroutines• Python supports both procedures and
functions– Procedure:• def proc1():
print ‘Hi!’– Function:• def func1():
return ‘Hi!’
![Page 15: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/15.jpg)
Subroutines (continued)• Python does not support name
mangling as in C++• Anything can be returned from a
function, including None and other functions
• Recursion is allowed• Python has support for calling
subroutines in modules written in C• Parameters are passed by value
![Page 16: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/16.jpg)
Scope• Lexical• Global/local scope• Similar to Scheme• No names need to be declared before
use
![Page 17: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/17.jpg)
Lifetime / Actions• Variables are alive as long as they can
be referenced, similar to Scheme• Python supports standard arithmetic
precedence and association with ()’s• Result type is defined the more
descriptive of the operands
![Page 18: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/18.jpg)
Control Structures• if statements work as expected
>>> if x < 0:… print ‘Negative’… elif x == 0:… print ‘Zero’… else:… print “Positive”…
![Page 19: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/19.jpg)
Control Structures continued• for loops differ from c++ and/or java.
They iterate over an aggregate.
>>> animals = [‘cat’, ‘dog’, ‘horse’]>>> for x in animals:… print x…
![Page 20: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/20.jpg)
Control Structures Continued• for loops can iterate over multiple lists at the
same time
>>> questions = ['name', 'quest', 'favorite color']>>> answers = ['lancelot', 'the holy grail', 'blue']>>> for q, a in zip (questions, answers):... print 'What is your %s? It is %s.' % (q, a)...What is your name? It is lancelot.What is your quest? It is the holy grail.What is your favorite color? It is blue.
![Page 21: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/21.jpg)
Pass• The pass command does nothing.
![Page 22: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/22.jpg)
Functions>>> def fib(n): ... a, b = 0, 1... while b < n:... print b,... a, b = b, a+b...
![Page 23: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/23.jpg)
Functions continued>>> def makeIncFunc ( n = 1 )… return lambda x: x + n…>>> tmp = makeIncFunc()>>> print tmp(3)4>>> tmp = makeIncFunc(2)>>> print tmp(3)5
![Page 24: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/24.jpg)
Default Value Side Effects
>>> def f(a, L=[]):… L.append(a)… return L…>>> print f(1)[1]>>> print f(2)[1, 2]>>> print f(3)[1, 2, 3]
![Page 25: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/25.jpg)
Classes• Python implements classes in a similar
way to Java and C++
>>> class Complex:... def __init__(self, realpart, imagpart):... self.r = realpart... self.i = imagpart...>>> x = Complex(3.0, -4.5)>>> x.r, x.i(3.0, -4.5)
![Page 26: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/26.jpg)
Inheritance• “Of course, a language feature would
not be worthy of the name ``class'' without supporting inheritance. “
class DerivedClassName(BaseClassName):<statement-1> . . .<statement-N>
![Page 27: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/27.jpg)
Multiple Inheritance!
class DerivedClassName(Base1, Base2, Base3):<statement-1>. . .<statement-N>
![Page 28: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/28.jpg)
Odds and Endsclass Employee:
pass
john = Employee() john.name = 'John Doe‘john.dept = 'computer lab‘john.salary = 1000
![Page 29: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/29.jpg)
Pickling• Python’s equivalent to Serialization
>>> pickle.dump( anyobject, fileopenedforwriting )
>>> objecttoloadto = pickle.load( fileopenedforreading )
![Page 30: The Python Programming Language](https://reader035.fdocuments.in/reader035/viewer/2022062521/56814c3a550346895db9404a/html5/thumbnails/30.jpg)
What this has to do with Legos
• A python library calls Pylnp which allows remote control of your robot through the IR tower
import lnp lnp.iwrite('hello')lnp.iread() 'world'