Python 1
Python 1
• Data types
• Sequences types: Lists, Tuples, and
Strings
• Mutability
• Understanding Reference Semantics in
Some material adapted Python
from Upenn cmpe391
slides and other sources
1
• Integers (default for numbers) Whitespace is meaningful in Python, especially
z = 5 / 2 # Answer 2, integer division indentation and placement of newlines
• Floats •Use a newline to end a line of code
x = 3.456 Use \ when must go to next line prematurely
• Strings •No braces {} to mark blocks of code, use
• Can use ”…" or ’…’ to specify, "foo" == 'foo’ consistent indentation instead
• Unmatched can occur within the string • First line with less indentation is outside of the block
“John’s” or ‘John said “foo!”.’ • First line with more indentation starts a nested block
• Use triple double-quotes for multi-line strings or •Colons start of a new block in many constructs,
strings than contain both ‘ and “ inside of them:
e.g. function definitions, then clauses
“““a‘b“c”””
2
• Names are case sensitive and cannot start The Python community has these
with a number. They can contain letters, recommended naming conventions
numbers, and underscores.
• joined_lower for functions, methods and,
bob Bob _bob _2_bob_ bob_2 BoB
attributes
• There are some reserved words:
and, assert, break, class, continue, • joined_lower or ALL_CAPS for constants
def, del, elif, else, except, exec, • StudlyCaps for classes
finally, for, from, global, if,
import, in, is, lambda, not, or, • camelCase only to conform to pre-existing
pass, print, raise, return, try, conventions
while
• Attributes: interface, _internal, __private
• Where do such conventions come from? • You can assign to multiple names at the
• The community of users same time
• Codified in PEPs >>> x, y = 2, 3
• Python's development is done via the Python >>> x
Enhancement Proposal (PEP) process 2
>>> y
• PEP: a standardized design document, e.g. 3
proposals, descriptions, design rationales,
and explanations for language features
• This makes it easy to swap values
>>> x, y = y, x
• Similar to IETF RFCs
• See the PEP index • Assignments can be chained
>>> a = b = x = 2
• PEP 8: Style Guide for Python Code
3
Accessing a name before it’s been properly
created (by placing it on the left side of an
assignment), raises an error
>>> y
4
• Sequences are containers that hold objects
• Finite, ordered, indexed by integers
• Tuple
• An immutable ordered sequence of items
• Items can be of mixed types, including collection
types
• Strings
• An immutable ordered sequence of chars
• Conceptually very much like a tuple
• List
• A Mutable ordered sequence of items of mixed
types
5
• Access individual members of a tuple, list, or
string using square bracket “array” notation
>>> t = (23, ‘abc’, 4.56, (2,3), ‘def’)
• Note that all are 0 based…
Positive index: count from the left, starting with 0
>>> tu = (23, ‘abc’, 4.56, (2,3), ‘def’)
>>> tu[1] # Second item in the tuple. >>> t[1]
‘abc’ ‘abc’
>>> li = [“abc”, 34, 4.34, 23] Negative index: count from right, starting with –1
>>> li[1] # Second item in the list.
34 >>> t[-3]
>>> st = “Hello World” 4.56
>>> st[1] # Second character in string.
‘e’
>>> t = (23, ‘abc’, 4.56, (2,3), ‘def’) >>> t = (23, ‘abc’, 4.56, (2,3), ‘def’)
• Returns copy of the container with a subset of • Omit 1st index to make a copy starting from
the original members. Start copying at the first the beginning of container
index, and stop copying before the second >>> t[:2]
index. (23, ‘abc’)
>>> t[1:4] • Omit 2nd index to make a copy starting at 1st
(‘abc’, 4.56, (2,3)) index and going to end of the container
• You can also use negative indices >>> t[2:]
>>> t[1:-1] (4.56, (2,3), ‘def’)
(‘abc’, 4.56, (2,3))
6
>>> l1 = l2 = ['a','b','c'] >>> l1 = ['a','b','c']
• [ : ] makes a copy of an entire sequence
>>> l1 >>> l2 = l1[:]
>>> t[:] ['a', 'b', 'c'] >>> l1
(23, ‘abc’, 4.56, (2,3), ‘def’) >>> l2 ['a', 'b', 'c']
• Note the difference between these two lines ['a', 'b', 'c'] >>> l2
for mutable sequences >>> l1[1] = 'x' ['a', 'b', 'c']
>>> l2 = l1 # Both refer to same ref, >>> l1 >>> l1[1] = 'x'
# changing one affects both ['a', 'x', 'c'] >>> l1
>>> l2 = l1[:] # Independent copies, two >>> l2 ['a', 'x', 'c']
refs ['a', 'x', 'c'] >>> l2
>>> ['a', 'b', 'c']
>>>
• Boolean test whether a value is inside a container: • The + operator produces a new tuple, list, or
>>> t = [1, 2, 4, 5]
>>> 3 in t string whose value is the concatenation of its
False arguments.
>>> 4 in t
True
>>> 4 not in t
False
>>> (1, 2, 3) + (4, 5, 6)
• For strings, tests for substrings (1, 2, 3, 4, 5, 6)
>>> a = 'abcde'
>>> 'c' in a
True >>> [1, 2, 3] + [4, 5, 6]
>>> 'cd' in a [1, 2, 3, 4, 5, 6]
True
>>> 'ac' in a
False
>>> “Hello” + “ ” + “World”
• Be careful: the in keyword is also used in the syntax ‘Hello World’
of for loops and list comprehensions
7
• The * operator produces a new tuple, list, or
string that “repeats” the original content.
>>> (1, 2, 3) * 3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> “Hello” * 3
‘HelloHelloHello’
8
• + creates a fresh list with a new memory ref
>>> li = [1, 11, 3, 4, 5] • extend operates on list li in place.
>>> li.extend([9, 8, 7])
>>> li.append(‘a’) # Note the method syntax >>> li
>>> li [1, 2, ‘i’, 3, 4, 5, ‘a’, 9, 8, 7]
[1, 11, 3, 4, 5, ‘a’]
• Potentially confusing:
• extend takes a list as an argument.
>>> li.insert(2, ‘i’) • append takes a singleton as an argument.
>>>li >>> li.append([10, 11, 12])
[1, 11, ‘i’, 3, 4, 5, ‘a’] >>> li
[1, 2, ‘i’, 3, 4, 5, ‘a’, 9, 8, 7, [10,
11, 12]]
count, remove, reverse, sort >>> li.reverse() # reverse the list *in place*
>>> li = [‘a’, ‘b’, ‘c’, ‘b’] >>> li
>>> li.index(‘b’) # index of 1st occurrence [8, 6, 2, 5]
9
• The comma is the tuple creation operator, not parens • Lists slower but more powerful than tuples
>>> 1,
(1,) • Lists can be modified, and they have lots of
• Python shows parens for clarity (best practice) handy operations and mehtods
>>> (1,)
(1,)
• Tuples are immutable and have fewer
• Don't forget the comma! features
>>> (1) • To convert between tuples and lists use the
1
list() and tuple() functions:
• Trailing comma only required for singletons others
• Empty tuples have a special syntactic form li = list(tu)
>>> () tu = tuple(li)
()
>>> tuple()
()
10
Understanding Reference Semantics
• There’s a lot going on with x = 3 • The data 3 we created is of type integer –
• An integer 3 is created and stored in memory objects are typed, variables are not
• A name x is created • In Python, the datatypes integer, float, and
• An reference to the memory location storing string (and tuple) are “immutable”
the 3 is then assigned to the name x • This doesn’t mean we can’t change the value
• So: When we say that the value of x is 3, we of x, i.e. change what x refers to …
mean that x now refers to the integer 3 • For example, we could increment x:
>>> x = 3
Name: x Type: Integer
Ref: <address1> Data: 3 >>> x = x + 1
>>> print x
name list memory 4
>>> x = x + 1 >>> x = x + 1
11
So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3
>>> y = x # Creates name y, refers to 3
>>> y = 4 # Creates ref for 4. Changes y
>>> print x # No effect on x, still ref 3
3
Type: Integer
Name: x Data: 3
Ref: <address1>
Type: Integer
Data: 4
>>> x = x + 1
So, for simple built-in datatypes (integers, floats, So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3 >>> x = 3 # Creates 3, name x refers to 3
>>> y = x # Creates name y, refers to 3 >>> y = x # Creates name y, refers to 3
>>> y = 4 # Creates ref for 4. Changes y >>> y = 4 # Creates ref for 4. Changes y
>>> print x # No effect on x, still ref 3 >>> print x # No effect on x, still ref 3
3 3
Name: x Name: x
Ref: <address1> Type: Integer Ref: <address1> Type: Integer
Data: 3 Data: 3
Name: y
Ref: <address2>
12
So, for simple built-in datatypes (integers, floats, So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3 >>> x = 3 # Creates 3, name x refers to 3
>>> y = x # Creates name y, refers to 3 >>> y = x # Creates name y, refers to 3
>>> y = 4 # Creates ref for 4. Changes y >>> y = 4 # Creates ref for 4. Changes y
>>> print x # No effect on x, still ref 3 >>> print x # No effect on x, still ref 3
3 3
Name: x Name: x
Ref: <address1> Type: Integer Ref: <address1> Type: Integer
Data: 3 Data: 3
Name: y Name: y
Type: Integer Type: Integer
Ref: <address2> Ref: <address2>
Data: 4 Data: 4
So, for simple built-in datatypes (integers, floats, So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3 >>> x = 3 # Creates 3, name x refers to 3
>>> y = x # Creates name y, refers to 3 >>> y = x # Creates name y, refers to 3
>>> y = 4 # Creates ref for 4. Changes y >>> y = 4 # Creates ref for 4. Changes y
>>> print x # No effect on x, still ref 3 >>> print x # No effect on x, still ref 3
3 3
Name: x Name: x
Ref: <address1> Type: Integer Ref: <address1> Type: Integer
Data: 3 Data: 3
Name: y Name: y
Type: Integer Type: Integer
Ref: <address2> Ref: <address2>
Data: 4 Data: 4
13
For other data types (lists, dictionaries, user-defined
types), assignment work the same, but some a = [1, 2, 3] a 1 2 3
methods change the objects
• These datatypes are “mutable”
• Change occur in place a
• We don’t copy them to a new memory address each time
• If we type y=x, then modify y, both x and y are changed b=a 1 2 3
b
immutable
>>> x = 3 mutable
x = some mutable object
>>> y = x y = x a
>>> y = 4 make a change to y
>>> print x look at x a.append(4) 1 2 3 4
3 x will be changed as well b
14