Lesson 4: Standard Types
Introduce standard data types
Numbers
Sequences
Hashing
Operators and built-in functions
Standard Types
Python Types
Standard Types
Numbers
Strings
Lists
Tuples
Dictionaries
Set
Other Types
None
Functions and Methods
Modules
Types/Classes
Iterators
http://docs.python.org/ref/types.html
Boolean Values
Every object has an intrinsic Boolean value
Any numeric zero or empty container is False
False bool
0 int
0L log-(2)
0.0 float
0.0+0.0j complex
"" str
[] list
() tuple
{} dict
None None
All other values are True
Operators
Apply to most data types
Object Value Comparison
< <= > >= == !=
Object Identity Comparison
is is not
Boolean
not and or
Built-in Fucntions
Apply to most data types
dir() object's arrtibutes
help() show help text
type() object type
isinstance() check object type(isinstance(x,int))
cmp() compare 2 objects
str() string rep of object
Numbers
Integers(int)
No size limit (except VM)
Decimal, hexadecimal, octal, binary
42, 0xAE, 0237/0o237, 0b1110
Long integers(long)--(2)
Merged with and replaced by integers
42L, 0xDECADEDEADBEEFBADDEAL
Floating-Point Numbers(float)
IEEE 754 double-precision
3.14, -6e2, 0.1
1.1 == 1.1000000000000001
Bits do not like repeating fractions
Use round() + string formatting
Or use decimal.Decimal type(python 2.4 or uper)
http://docs.python.org/tut/node16.html
Other Numbers
Other numeric types
Complex(complex)
Boolean(bool)
Other numeric type modules
decimal
fractions
numbers
Numeric Operators
Arithmetic Operators
+ - * / // % **
/ in 2.x is classic division(e.g., 1/2 == 0)
/ in 3.x is true division(e.g., 1/2 == 0.5)
//in 3.x (e.g., 1/2 == 0, 1.0/2.0 == 0.0)
Positive and negative sign
+ -
Bit Operators(int only)
& | ^ << >> ~
Numeric Built-ins
Factory
int()
float()
complex()
Operational
abs()
pow()
round()
divmod()
chr() # change number to char(2.x return ASCII,3.x return unicode)
unichar() # change number to unicode char(2.x)
Not "casting", nor conversion*********************
>>> data = []
>>> if data:
print 'data has elements in it'
else:
print 'data is empty'
data is empty
>>> if len(data) > 0:
print 'data has stuff in it'
else:
print 'data is empty'
data is empty
>>> dir(data)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__',
'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> isinstance(data, list)
True
>>> isinstance(data, int)
False
>>> type(data)
<type 'list'>
>>> 1+ 2
3
>>> 5% 2
1
>>> 2 ** 3
8
>>> 9 ** 0.5
3.0
>>> 1 / 2
0
>>> 1.0 / 2
0.5
>>> 1.0 / 2.0
0.5
>>> 1.0 // 2.0
0.0
>>> from __ future__ import division
>>> 1 / 2
0.5
>>> int(3.4)
3
>>> float(1)
1.0
>>> round(1.185, 2)
1.189999999999999
>>> '%.2f' % round(1.185, 2)
'1.19'
>>> str(round(1.185, 2))
'1.19'
Sequences
String(str, unicode--(2))
Bytes(bytes, bytearray)
List(list)
Tuples(tuple)
Array-like collections/containers
Heterogeneous(except strings)
Accessed by index(starts at 0)
Slicing
Access element(s) by "slicing"
Single-element slice: seq[i]
Return item i for sequence seq
Where 0 <= i < len(seq)
Negative indices also allowed, so:
-len(seq) <= i < len(seq)
Slicing Example
s = "Python"
012345
-6-5-4-3-2-1
String example above
Works for all sequence types
s[2]==t
S[0]==P
s[-2]==o
s[len(s)]==ERROR
Multi-Element Slicing
Access multiple element(s) with ":"
Syntax: seq[i:j]
From i up to but not including j
Missing indices also allowed
Means from begining or to end
s = "Python"
012345
-6-5-4-3-2-1
s[2:5]==tho
s[3:}==hon
s[:2]==Py
s[-4:-1]==tho
s[:]==Python
Sequence Operators
Membership
in not in
Concatenation
+
Repetition/Duplication
*
Sequence Built-ins
Factory
Str()
list()
tuple()
unicode()--(2)
bytes()
bytearray()
Operational
len()
max()
min()
Strings
Like an array of characters...
No char(acter) type in Python
Immutable, as are numbers
Value cannot be changed
Flexible string formatting
% operator or format() method
Triple quotes allow special chars
>>> s = 'Python'
>>> s * 2
'PythonPython'
>>> s *= 2
>>> s
'PythonPython'
>>> s[1]
'y'
>>> s[-1]
'n'
>>> s[:4]
'Pyth'
>>> s[2:6]
'thon'
>>> s[-4:-1]
'tho'
>>> s[:6] + 'is cool'
'Pythonis cool'
>>> s[:6] + ' is cool'
'Python is cool'
>>> '-' * 20
'-------------------'
>>> 'ton' in s
False
>>> 'thon' in s
True
>>> 'thon' not in s
False
>>> hi = '''hi
there!'''
>>> hi
'hi\nthere!'
>>> print hi
hi
there!
String Formatting
>>> "Name: %s, age: %d" % ('John', 35)
'Name: John, age: 35'
>>> "Name: {0}, age: {1}".format('John', 35) #---------->(2.6 or upper)
'Name: John, age: 35'
>>> d = {'user': 'wes', 'page': 42}
>>> 'http://web/%(user)s/%(page)d.html' % d
'http://web/wes/42.html'
>>> 'http://web/{user}/{page}.html'.format(**d)
'http://web/wes/42.html'
String Methods
Built-in String Method Sampler
split() split on dilimiter to list
join() join list with delimiter
find() substring search
strip() remove leading/trailing whitespace
lower() lowercased version of stirng
replace() search and replace(not RE)
ljust() create left-justified string
>>> s = 'Python is cool.'
>>> s
'Python is cool'
>>> s.find('is not')
-1
>>> s.find('is ')
7
>>> s.rfind('o')
12
>>> s.center(25)
' Python is cool. '
>>> s.center(25).strip()
'Python is cool.'
>>> s.split()
['Python', 'is', 'cool.']
>>> '::'.join(s.split())
'Python::is::cool.'
>>> '.'.join(reverse(s.split()))
'cool..is.Python'
>>> s.title()
'Python Is Cool'
>>> s.upper()
'PYTHON IS COOL.'
>>> s.title().swqpcase()
'pYTHON iS cOOL'
>>> s.isalpha()
False
>>> s.endswith('.')
True
>>>> s.replace('.', '!')
'Python is cool!'
String Miscellany
Operators and Operational Built-in Functions
raw_input() user input
ord() ASCII/Unicode value
r'c:\tmp\new' raw string
u'n\xe9' Unicode string ---------(2)
b'n\xc3\xa9' bytes string------------(2.6 or 3.x)
String and Bytes
Unicode default string type in 3.x
Introduce byte types for 8-bit chars
2.x 3.x *Mutable?
str("") bytes(b"") no
unicode(u"") str("") no
N/A bytearray yes
*-Ability to change its value
Lists and Tuples
Ordered heterogeneous containers
Lists
Mutable, resizable
Feature many helper methods
Tuples
Immutable
Transport data to/from functions
Not truly meant for user manipulation (no methods)
List Methods
Built-in Methods
append() add object to end
insert() add object anywhere
remove(),pop() delete objects
reverse() reverse in-place
sort() sort in-place
extend() merge another list
*count() number of occurrences
*index() first index of value
>>> m = ['Core', 'Programming', 9, 2006]
>>> m
['Core', 'Programming', 9, 2006]
>>> m.append('Prentice Hall')
>>> m
['Core', 'Programming', 9, 2006, 'Prentice Hall']
>>> m.insert(1, 'Pytho')
>>> m
['core', 'Pytho', 'Programming', 9, 2006, 'Prentice Hall']
>>> m[1] = 'Python'
['core', 'Python', 'Programming', 9, 2006, 'Prentice Hall']
>>> 2006 in m
True
>>> m.pop(3)
9
>>> m
['core', 'Python', 'Programming', 2006, 'Prentice Hall']
>>> m[2:4] = ['Fundamentals', 2008]
>>> m
['core', 'Python', 'Fundamentals', 2008, 'Prentice Hall']
>>> m.remove('Core')
>>> m
['Python', 'Fundamentals', 2008, 'Prentice Hall']
>>> m.remove('C')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
List Comprehensions
Lists:
Highly-used data type
Most common operations:
Choose elements that fit criteria
Apply function to each element
List Comprehensions("listcomps")
Construct lists using logic
Basic syntax:
[expr for var in iterable]
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [i for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [i*2 for i in range(10)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>> [i for i in range(10) if i % 2 == 0]
[0, 2, 4, 6, 8]
>>> [i*3 for i in range(10) if i % 2 == 0]
[0, 6, 12, 18 ,24]
>>> [i//3 for i in range(10) if i % 2 == 0]
[0, 0, 1, 2, 2]
>>> f = open(r'c:\autoexec.bat', 'r')
>>> data = [line.strip() for line in f]
>>> f.close()
>>> data
[]
Generator Expression
List Comprehension
Not as efficient with memory
Entire list must be created
Generator Expressions
Alternative to list comprehensions
"Generate" successive values
nearly identical basic syntax:
(expr for var in iterable)
Hashing Types
Dictionaries(dict)
Mutable Sets(set)
Immutable Sets(frozenset)
Hashed collection/container objects
Accessed by iterator(or key for dicts)
Heterogeneous
Unordered by definition
Dictionaries
Python's only mapping type
Map keys to values
Mutable, resizable hash tables
Heterogeneous
Values are arbitrary Python objects
Keys are scalar(must be hashable)
Strings, numbers, etc.
No(key) collisions allowed
Common Built-in Methods
get() get value given key
update() merge another dict
setdefault() return value, set if necessary
pop() remove & return value
items() all key-value pairs
clear() clear dictionary
fromkeys() make new dict with keys
Dictionary Operations
General Usage
Number of key-value pairs:
len()
Key membership:
if k in d: # or not in
Loop through all keys:
for k in d:
>>> d = {'title': 'Core Python Programming'}
>>> d['title'] = 'Core Python Programming'
>>> d['year'] = 2006
{'year': 2006, 'title': 'Core Python Programming'}
>>> 'year' in d
True
>>> 'pub' in d
False
>>> d['title']
'Core Python Programming'
>>> d['pub']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'pub'
>>> d.get('pub', 'N/A')
'N/A'
>>> d.setdefault('pub', 'Prentice Hall')
'Prentice Hall'
>>> d
{'year': 2006, 'pub': 'Prentice Hall', 'title': 'Core Python Programming'}
>>> d['pub']
'Prentice Hall'
>>> for k in d:
print k.ljust(
, d[k]
year 2006
pub Prentice Hall
title Core Python Programming
>>> for k, v in d.item():
print k.ljust(
, v
year 2006
pub Prentice Hall
title Core Python Programming
>>> d.items()
[('year', '2006'), ('pub', 'Prentice Hall'), ('title', 'Core Python Programming')]
>>> d.iteritems()
<dictionary-itemiterator object at 0x011ABFA0>
Sets and Frozensets
def: a set is a finite collection of objects
Mutable(set) or immutable(frozenset)
No key-value pairs, just values
Unordered and heterogeneous
Hashed, like dictionary keys
No collisions means no duplicates
Remember Venn diagrams?
>>> a = [1, 4, 6, 3]
>>> b = (9, 5, 2, 7, 5, 3)
>>> a, b
([1, 4, 6, 3],(9, 5, 2, 7, 5, 3))
>>> c = set(a)
>>> d = set(b)
>>> c
set([1, 3, 4, 6])
>>> d
set([9, 2, 3, 5, 7])
>>> c = {1, 3, 4, 6} # python 3.x set(())
>>> c.intersection(d)
set([3])
>>> c & d
set([3])
>>> c | d
set([1, 2, 3, 4, 5, 6, 7, 9])
>>> c |= d
>>> c
set([1, 2, 3, 4, 5, 6, 7, 9])
>>> c.add( 
>>> c
set[(1, 2, 3, 4, 5, 6, 7, 8, 9)]
>>> c - d
set([8, 1, 4, 6])