1
CSE 130 : Fall 2006
Programming Languages
Ranjit Jhala
UC San Diego
Lecture 11:
Crash course in Python (contd.)
A crash course in Python
Interpreted, imperative, OO Language
• Everything is an
object
• Dynamic
Typing
Programs are made up of:
• Expressions
• Statements
– Assignment
– if/elif/else
– while-loops
– Functions
• Classes
(still to come)
Today: Revisit some objects
• Exploit features and build powerful
expressions
Base
: int, float, complex
Sequence
: string, tuple,
list
What can sequences do ?
Select
• i-th element:
s[i]
• subsequence (“slice”):
s[i:j]
Update -- For
mutable
sequences (e.g. Lists)
• Update i-th element:
s[i] = e
• Update subsequence:
s[i:j] = e
Sequence “contains”
x
in
s
>>> z = [1,2,3,4,5,6,7,8,9,10]
>>> z[3:6] = [“a”,“b”,“c”]
>>> z
[1,2,3,”a”,”b”,”c”,7,8,9,10]
>>> z[3:6] = [“a”, “b”] * 2
>>> z
[1,2,3,”a”,”b”,”a”,”b”,7,8,9,10]
>>> z[4:]=[]
>>> z
[1,2,3,”a”]
>>> z[:0] = [“al”, “be”]
>>> z
[“al”,”be”,1,2,3,”a”,”b”,”a”,”b”,7,8,9,10]
s[i:j]=e
Update subsequence
Update subsequence:
s[i:j] = e
• Changes the “object” referred to by s
• May change the length of the sequence
– Increase: if RHS length >
j-i
– Decrease: if RHS length <
j-i
s[i:j]=e