Download Python Core Lecture 2: Values, Types, Variables, Keywords, Statements, Expressions, Commen and more Lecture notes Programming Languages in PDF only on Docsity!
i
CSC326 Python Imperative Core (Lec 2)
ii
REVISION HISTORY
NUMBER DATE DESCRIPTION NAME
1.0 2011-09 JZ
1 Agenda
- Python imperative core
- value, type, variable
- expression, statement
- module
- Incremental development w/t Python
2 Invoking Python
>python
Python 2.6.1 (r261, Mar 18 2009, 15:53:57)
[GCC 3.2.2] on sunos
Type "help", "copyright", "credits" or "license" for more information.
>python foo.py
3 Value, Type, Variable
- Value: Basic unit of data program manipulates
- Type:
- A category of values (or a set of values)
- A type itself is also a value!
- Variable: name that refers to a value
>>> print ’Hello, World!’
’Hello, World’
>>>print 4
>>> type(4)
<type ’int’>
>>> type(’4’)
<type ’str’>
>>> type(’3.2’)
<type ’float’>
>>> message = ’hi’
>>> n = 17
>>> pi = 3.
>>> print n
>>> print pi
>>> type(pi)
<type ’float’>
Note String could be single quoted and double quoted
To print "hey"
In C
printf (""hey" );
print ’"hey"’
- No ; after a statement!
- Hex/Oct number representation the same as C
>>>zipcode = 02492
SyntaxError: invalid token
4 Keywords
5 Statement
- print statement (obsolete for 3.0)
- assignment
- more to come
6 Expression
- functions (same as C)
- type(.) was a function
- function application
- function composition a( b(v1) )
- operators
- special symbols representing builtin functions
- apply on operands (unary or binary)
- arithmetic: — + - / ˆ % (just like C) — 5*2: exponentiation (Not in C)
- relational: == != > < >= ⇐
- logical: and, or, not
- operator precedence
9 Defining Functions
- We see how to use functions
- Let’s create our own
def print_lyrics() : print "I am a lumberjack, and I am OK." print "I sleep all night and I work all day."
Note
- def is a keyword
- paranthesis enclose function parameters
- header (first line end with colon)
- body
- end a function with empty line (if interactive mode)
- function is nothing but a value
Important
- body has to be indented!
- use space, not tab to avoid confusion
- No curly braces! (if indented anyway for readibility, why the waste?)
>>> print print_lyrics
<function print_lyrics at 0xb8e99e9c>
>>> print type(print_lyrics)
<type ’function’>
>>> print_lyrics()
- Function can call functions
def repeat_lyrics() : print_lyrics() print_lyrics()
- Function taking parameters
def print_twice( bruce ) : print bruce print bruce
def cat_twice( part1, part2 ) : cat = part1 + part print_twice( cat )
- variables do NOT need to be declared at all
- variables types are inferred
- However, has to be DEFINED (assigned a value) before USED
- scoping rule applies (just like C)
10 Conditional
- Conditional
- header followed by indented body
- at least ONE statements in body
- If you don’t have one, or wants to come back later, use pass statement
if x > 0 : print ’x is positive’
if x < 0 : pass # does nothing! if 0 < x and x < 10 : pass
if x % 2 : print ’x is even’ else : print ’x is odd’
if choice == ’a’ : draw_a() elif choise == ’b’ : draw_b() elif choice == ’c’ : draw_c()
- Nested conditional
- avoid if you could use chained conditional
if x == y : pass else : if x < y : pass else : pass
Note Contract based programming with preconditions
def factorial( n ) : assert isinstance(n,int) if n == 0 : return 1 else : recurse = factorial( n - 1 ) result = n (^) * recurse return result
- Computing fibnacci series
def fibonacci( n ) : if n < 2 : return n else : recurse1 = fibonacci( n - 1 ) recurse2 = fibonacci( n - 2 ) return recurse1 + recurse
12 Incremental Development
- We have seen how to use pass
- In general, code could be developed incrementally
- compute Euclidean disstance = sqrt( (x2-x1)2 + (y2-y1)2 )
- Make it run first! (it does not have to be right)
def distance( x1, y1, x2, y2 ) : return 0.
- Now you could at least use it with right parameters — Advantage of interpretive language: no recompile
>>> distance( 1, 2, 4, 6 )
- Add more code (including debugging code)
def distance( x1, y1, x2, y2 ) : dx = x2 - x dy = y2 - y print ’dx is’, dx print ’dy is’, dy return 0.
- Add more code (including debugging code: scaffolding)
def distance( x1, y1, x2, y2 ) : dx = x2 - x dy = y2 - y print ’dx is’, dx print ’dy is’, dy dsquared = dx2 + dy print ’dsquared is’, dsquared result = math.sqrt( dsquared ) return result
- Finalize (remove debug code)
def distance( x1, y1, x2, y2 ) : dx = x2 - x dy = y2 - y dsquared = dx2 + dy result = math.sqrt( dsquared ) return result
13 Recap
- Imperative core
- We have covered a small set of Python
- variables: to hold values
- expression: to compute
- statement: state transition
- Turing completeness
- But according to Turing, it is complete
- Infinite tape: function recursion
Important Anything could be computed with this sub-language!
- Development
- Interperative language: no compilation-link-run cycle
- Coding style "enforced": intentatation is part of syntax
- Encourage incremental development
- interface first (contract with outside)
- test first
- algorithm skeleton first (use pass)
- scaffolding: insert debug code