The Python Programming Language - PowerPoint PPT Presentation

About This Presentation
Title:

The Python Programming Language

Description:

Title: The Python Programming Language Author: Matthew Campbell Last modified by: Matthew Campbell Created Date: 1/11/2005 2:32:26 AM Document presentation format – PowerPoint PPT presentation

Number of Views:1987
Avg rating:3.0/5.0
Slides: 31
Provided by: MatthewC47
Learn more at: https://www.cs.rit.edu
Category:

less

Transcript and Presenter's Notes

Title: The Python Programming Language


1
The Python Programming Language
  • Matt Campbell Steve Losh

2
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!

3
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

4
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

5
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

6
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

7
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

8
Literals
  • Integers 2, 4, -3
  • Floats 2.0e10, 3.5, .03
  • Boolean True, False
  • Strings cat, cat
  • Lists 12, 3.4, cat, lambda x x3
  • Sets set(12, 3.4, cat, lambda x x3)
  • 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

9
Variable Typing
  • Variables in Python do not need to be declared as
    a specific type
  • Example
  • A, B 3, cat
  • A variables type is dynamic, and will changed
    whenever it is reassigned
  • Example
  • a, b 1, cat
  • a, b .3, lambda x xx
  • No such thing as const in Python

10
Quick Dirty Input
  • gtgtgt x int(raw_input("Please enter an integer
    "))

11
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
  • gtgtgt animals cat, dog, mouse, bird
  • gtgtgt print animals01
  • cat, dog
  • gtgt print animals1
  • dog, mouse, bird
  • gtgtgt tmp list(Shrubbery)
  • gtgtgt tmp1 tmp-7
  • gtgtgt tmp
  • r, u, b, b, e, r, y, S, h,
    r, u, b, b, e, r, y

12
Ranges
  • Python has a range function to easily form lists
    of integers.
  • gtgtgt range(5)
  • 0, 1, 2, 3, 4
  • gtgtgt range(2,5)
  • 2, 3, 4
  • gtgtgt range(0, 10, 2)
  • 0, 2, 4, 6, 8
  • gtgtgt range(5, 0, -1)
  • 5, 4, 3, 2, 1

13
in
  • The in keyword checks if the given object is
    contained within the aggregate.
  • gtgtgt p cat
  • gtgtgt j cat, dog
  • gtgtgt p in j
  • True
  • gtgtgt a in p
  • True
  • gtgtgt t in p2
  • False

14
Subroutines
  • Python supports both procedures and functions
  • Procedure
  • def proc1()
  • print Hi!
  • Function
  • def func1()
  • return Hi!

15
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

16
Scope
  • Lexical
  • Global/local scope
  • Similar to Scheme
  • No names need to be declared before use

17
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

18
Control Structures
  • if statements work as expected
  • gtgtgt if x lt 0
  • print Negative
  • elif x 0
  • print Zero
  • else
  • print Positive

19
Control Structures continued
  • for loops differ from c and/or java. They
    iterate over an aggregate.
  • gtgtgt animals cat, dog, horse
  • gtgtgt for x in animals
  • print x

20
Control Structures Continued
  • for loops can iterate over multiple lists at the
    same time
  • gtgtgt questions 'name', 'quest', 'favorite
    color'
  • gtgtgt answers 'lancelot', 'the holy grail',
    'blue'
  • gtgtgt 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.

21
Pass
  • The pass command does nothing.

22
Functions
  • gtgtgt def fib(n)
  • ... a, b 0, 1
  • ... while b lt n
  • ... print b,
  • ... a, b b, ab
  • ...

23
Functions continued
  • gtgtgt def makeIncFunc ( n 1 )
  • return lambda x x n
  • gtgtgt tmp makeIncFunc()
  • gtgtgt print tmp(3)
  • 4
  • gtgtgt tmp makeIncFunc(2)
  • gtgtgt print tmp(3)
  • 5

24
Default Value Side Effects
  • gtgtgt def f(a, L)
  • L.append(a)
  • return L
  • gtgtgt print f(1)
  • 1
  • gtgtgt print f(2)
  • 1, 2
  • gtgtgt print f(3)
  • 1, 2, 3

25
Classes
  • Python implements classes in a similar way to
    Java and C
  • gtgtgt class Complex
  • ... def __init__(self, realpart, imagpart)
  • ... self.r realpart
  • ... self.i imagpart
  • ...
  • gtgtgt x Complex(3.0, -4.5)
  • gtgtgt x.r, x.i
  • (3.0, -4.5)

26
Inheritance
  • Of course, a language feature would not be
    worthy of the name class'' without supporting
    inheritance.
  • class DerivedClassName(BaseClassName)
  • ltstatement-1gt
  • . . .
  • ltstatement-Ngt

27
Multiple Inheritance!
  • class DerivedClassName(Base1, Base2, Base3)
  • ltstatement-1gt
  • . . .
  • ltstatement-Ngt

28
Odds and Ends
  • class Employee
  • pass
  • john Employee()
  • john.name 'John Doe
  • john.dept 'computer lab
  • john.salary 1000

29
Pickling
  • Pythons equivalent to Serialization
  • gtgtgt pickle.dump( anyobject, fileopenedforwriting
    )
  • gtgtgt objecttoloadto pickle.load(
    fileopenedforreading )

30
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'
Write a Comment
User Comments (0)
About PowerShow.com