Python MiniCourse - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Python MiniCourse

Description:

{key:expr} dictionary creation (exp,...) tuple creation or normal parentheses ... Raw strings r'stuff' escape sequences not interpreted ... – PowerPoint PPT presentation

Number of Views:127
Avg rating:3.0/5.0
Slides: 19
Provided by: KenHa98
Category:

less

Transcript and Presenter's Notes

Title: Python MiniCourse


1
Python Mini-Course
  • Part 1 Intro to Imperative Python
  • Prof Ken Hawick, 2009

2
Python
  • Script language
  • Normally interpreted
  • Imperative and OO and has support for other
    paradigms such as functional too
  • Has large collection of library packages
  • Useful for sys admin tasks as a glue
    language for non-critical performance programs
    for integration of other software

3
Python Lexical Structure
  • Sequence of logical lines made up of one or
    more physical lines with \ to join them or if
    bracket, parentheses etc are still open
  • No delimiter at end of lines
  • Whitespace is significant (!)
  • Indentation is normally 4 spaces for each block
    which must all have same indents
  • Indent rules do not apply to continuation lines
  • is used for trailing comment

4
Python Keywords
del actually means unbind yield used in
generators raise for raising exceptions lambda
for functional lambda expressions
5
Python Operators
is numerical exponentiation // is
truncating division
6
Python Delimiters
a12 is for slicing a3 is for normal
indexing expr, list creation keyexpr
dictionary creation (exp,) tuple creation
or normal parentheses
7
Python Data Types
  • type( obj ) returns the type of an object
  • isinstance(obj, type) is built-in boolean
    function
  • True, False are built-in names, bool is type
  • Numbers
  • Sequences string, tuple, lists
  • Dictionaries dict
  • built-in type None denotes a null object
  • Callable types functions
  • No declarations in Python variables are
    implicit

8
Python Numbers
  • Integer, floating-point and complex
  • Integer literal decimal (normal) octal with
    leading 0 or hex with leading 0x
  • Suffix L or l to denote a long otherwise
    limited by machine word size
  • Floats will have a decimal point
  • complex z has (read only) z.real and z.imag
  • Use (engineers) convention of 1.0j for sqrt of
    -1

9
Python Sequences
  • Includes strings, tuples and lists
  • Support Coercion and conversion
  • Support Concatenation
  • Sequence membership test using in
  • Indexing using
  • Slicing using

10
Python Strings
  • Python strings are immutable
  • String literals can be quoted or triple-quoted
  • a very long string thats not at all
    interesting to the reader with comment
    only on this line
  • Usual string escapes \ \ \\ \t \n etc
  • Raw strings rstuff escape sequences not
    interpreted
  • Built-in function str() converts other types to
    a string
  • \uABCD etc for Unicode literals

11
Python Tuples
  • Immutable ordered sequences of items
  • Items of arbitrary (different) type(s)
  • (100,200,300,400) or (1.0,) or ()
  • Trailing comma needed to distinguish from
    ordinary parentheses situations
  • Built-in tuple(abcdef) creates tuple
    (a,b,)
  • tuple() with no args returns an empty tuple
  • t(1.0,) followed by t (2.0,) gives
    (1.0,2.0)

12
Python Lists
  • Mutable ordered sequences of items
  • Items may be of arbitrary type(s)
  • 1,2,3 or apples, pears, 3.4159 or
  • Can have optional trailing comma
  • list(fred) builds f, r, e, d
  • list() with no arguments creates an empty list
  • works as concatenation operator

13
Python Dictionaries
  • Built-in mutable and unordered mapping
  • Keys can be different hashable types
  • Values are arbitrary objects
  • Like associative array or hash of key/value pairs
  • d "apples"1, "pears"42, "bananas" 0
  • print d"apples"
  • dict() returns an empty dictionary

14
Python print
  • Built-in function
  • print expressions
  • print gtgt fileobject, expressions
  • print puts in trailing \n by default unless
    you specify a trailing comma
  • sys.stdout etc are available but need import sys
  • s raw_input(mypromptgt )
  • print s, str(myfloat), a b

15
Python Control Flow
  • ifelifelifelse
  • while expression block1 else block2
  • for var in iterable block1 else block2
  • list comprehensions
  • break and continue and pass
  • try, except, finally, else

16
Python Functions - Callables
  • def func-name( parameters ) block
  • uses call-by-value
  • Can have optional parameters with default values
  • Optional multiple return statement(s)
  • Nested functions allowed
  • Lambda expressions
  • Generators (iterators)
  • Recursion allowed

17
Python function definitions
  • Indent of block is highly significant
  • def myfunc(a,b,c)
  • return a b c
  • f myfunc( "apples", "", "pears" )
  • x myfunc( 1,2,3)
  • print f,x
  • Output is applespears 6

18
Python assert
  • assert condition
  • assert condition,expression
  • expression appears in the output message
  • Running Python with the optimize flag -O
    switches the asserts off
Write a Comment
User Comments (0)
About PowerShow.com