Chapter

Python Data Type

Aman. W()
Department of Physics
University of Gondar
Contents

• Introduction
• Python Data Type
• Numeric
• String,
• lists,
• dictionaries and
• tuples
Introduction
• Python has five standard data types:
– Number
– String
– List (entries enclosed in [ ], list methods available)
– Tuple (comma separated values of possible different types).
– Dictionary (un-ordered, key:value sequences in [ ]
• Python supports four different numerical data types
1. int (signed integers)
2. long (long integers [can also be represented in octal and
hexadecimal])
3. float (floating point real values)
4. complex (complex numbers)
String Data Type

• This section introduces a non-numeric data type that
represents text --- string
• A string is a sequence of characters
• Strings in Python are identified as a contiguous set of
characters in between quotation marks.
• Python allows for either pairs of single or double quotes.
• Example
• A string literal uses quotes
• >>>‘Hello’ or
• >>> “Hello”
String Data Type
>>> st1 = "Hello“
• The first two st1, st2
• However, if we(str3) >>> st2 = 'there‘
'that' s not going to work'
• For strings, + means >>> str3= 'that' s not going to
“concatenate” work‘
• When a string contains
>>> st3 = '123‘
numbers, it is still a string >>>
• We can convert numbers in >>> x = int(str3) + 1
a string into a number using
int() >>> print x

124
• The plus (+) sign is the • >>>Print str3
string concatenation • >>> bob = str1 + str2
operator and the asterisk ( *
) is the repetition operator. • >>> print bob
• Subsets of strings can be • Hellothere
taken using the slice
operator ( [ ] and [ : ] ) with
indexes starting at 0 in the
beginning of the string
• and working their way
from -1 at the end.
Looking Inside Strings

• We can get any single
character in a string using b a n a n a
an index specified in square 0 1 2 3 4 5
brackets
• The index value must be an >>> fruit = 'banana‘
integer and starts at zero >>> letter = fruit[1]
• The index value can be an >>> print letter
expression that is a
computed >>> n = 3
>>> w = fruit[n - 1]
>>> print w
n
Looping Through Strings

• we can construct a loop
to look at each of the fruit = 'banana' 0b
letters in a string index = 0 1a
individually 2n
3a
while index < len(fruit) :
4n
• Using a while statement letter = fruit[index] 5a
and an iteration print index, letter
variable, the len index = index + 1
function,
Looping Through Strings

• A definite loop
using a for fruit = 'banana'
statement is much for letter in fruit : b
print letter
more elegant a
n
• The iteration
a
variable is index = 0
n
completely taken while index < len(fruit) :
a
letter = fruit[index]
care of by the for
print letter
loop index = index + 1
String Library
Built in String Method
• Python has a number of string functions(method) which
are in the string library
• These functions are already built into every string - we
invoke them by appending the function to the string
variable
• These functions do not modify the original string, instead
they return a new string that has been altered
>>> stuff = 'Hello world‘
>>> type(stuff)
<type 'str'>
>>> dir(stuff)

['capitalize', 'center', 'count', 'decode', 'encode', 'endswith',
'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha',
'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust',
'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust',
'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip',
'swapcase', 'title', 'translate', 'upper', 'zfill']
String Library

str.capitalize()
str.center(width[, fillchar])
str.endswith(suffix[, start[, end]])
str.find(sub[, start[, end]])
str.lstrip([chars])

str.replace(old, new[, count])
str.lower()
str.rstrip([chars])
str.strip([chars])
str.upper()
String Library
Built in String Method
• Let us look at some of
functions(method) which
are in the string library >>> greet = 'Hello Bob‘
• The function/method >>> zap = greet.lower()
-lower >>> print zap
hello bob
>>> print greet
Hello Bob
>>> print 'Hi There'.lower()
hi there
>>>
• Example:
>>>'The happy cat ran home.'.upper()
'THE HAPPY CAT RAN HOME.’
>>> 'The happy cat ran home.'.find('cat')
10
>>> 'The happy cat ran home.'.find('kitten')
-1
>>> 'The happy cat ran home.'.replace('cat', 'dog')
'The happy dog ran home.'
Method(Len)
 A function is some
>>> fruit = 'banana‘ stored code that we
>>> x = len(fruit) use.
>>> print x  A function takes some
6 input and produces an
output.

'banana' len() 6
(a number)
(a string)
function
Assignment 3

• 1. use print to display the following messages.
My name is Alemu and age is 21 years!

• 2. Str = ‘I am a first year graduate student at
university of gondar’
replace(the smaller letter: u and g with cap U
& G) , index(), split, join
Lists

• Lists are the most versatile of Python's compound data types. A
list contains items separated by commas and enclosed within
square brackets ([]).
• The items belonging to a Python lists can be of different data
type.
• The values stored in a list can be accessed using the slice
operator ( [ ] and [ : ] ) with indexes starting at 0 in the
beginning of the list and working their way to end-1.
• The plus ( + ) sign is the list concatenation operator, and the
asterisk ( * ) is the repetition operator.
• Items may also be inserted or added to lists
List Constants

• List constants are >>> print [1, 24, 76]
surrounded by square [1, 24, 76]
brackets and the elements >>> print ['red', 'yellow', 'blue']
in the list are separated ['red', 'yellow', 'blue']
by commas. >>> print ['red', 24, 98.6]
• A list element can be any ['red', 24, 98.599999999999994]
Python object - even >>> print [ 1, [5, 6], 7]
another list [1, [5, 6], 7]
• A list can be empty >>> print []
[]
Building a list from scratch

• We can create an
empty list and then
add elements using >>> stuff = list()
the append method >>> stuff.append('book')
• The list stays in >>> stuff.append(99)
>>> print stuff
order and new
['book', 99]
elements are added
>>> stuff.append('cookie')
at the end of the list
>>> print stuff
['book', 99, 'cookie']
Concatenating lists using +

• We can create a new list by adding two existing lists
together
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print c
[1, 2, 3, 4, 5, 6]
>>> print a
[1, 2, 3]
Is Something in a List?

• Python provides
two operators that >>> some = [1, 9, 21, 10, 16]
let you check if an >>> 9 in some
item is in a list True
>>> 15 in some
• These are logical
False
operators that >>> 20 not in some
return True or True
False >>>
• They do not
modify the list
A List is an Ordered Sequence

• A list can hold many
items and keeps those
items in the order until we>>> friends = [ 'Joseph', 'Glenn',
do something to change 'Sally' ]
the order >>> friends.sort()
>>> print friends
• A list can be sorted (i.e.
['Glenn', 'Joseph', 'Sally']
change its order)
>>> print friends[1]
• The sort method (unlike
Joseph
in strings) means "sort
>>>
yourself“
• How it sort?
Looking Inside Lists

 Just like strings, we can get at any
single element in a list using an index
specified in square brackets
• The two index mechanism of lists are
• Positive indices
• Negative indices

>>> mylist =
['a','b','c','d','e']
>>> mylist[-2]
>>> mylist[0]
'a' 'd‘
>>> mylist[1]
>>> mylist[-3]
'b' 'c'
>>> mylist[2]
'c'
Lists can be sliced using :

>>> t = [9, 41, 12, 3, 74, 15]
>>> t[1:3]
[41,12] Remember: Just like
>>> t[:4] in strings, the second
[9, 41, 12, 3] number is "up to but
>>> t[3:] not including"
[3, 74, 15]
>>> t[:]
[9, 41, 12, 3, 74, 15]
Python List functions

• Some of the list methods(function)
• cmp(list1, list2)------ Compares elements of both
lists.
• len(list) ------------- Gives the total length of the lists
• max(list)-----------Returns item from the list with max value.
• min(list)-------------Returns item from the list with min value.
• list(seq)--------------Converts a tuple into list.
• The builtin function • > cmp(0,1)
cmp(x,y) compares two -1
elements and returns • By default, sorting uses the
-1, 0, 1 builtin, cmp() method
• x < y --> -1 • >> x = [1,4,2,3,0]
• x == y --> 0 • >> x.sort()
• x > y --> 1 • >> x
• [0, 1, 2, 3, 4]
Built in Functions and Lists

• There are a number
of functions built >>> nums = [3, 41, 12, 9, 74, 15]
into Python that >>> print len(nums)
take lists as 6
>>> print max(nums)
parameters 74
• Some of the other >>> print min(nums)
list methods 3
– Max- maximum >>> print sum(nums)
154
– Min-minimum
>>> print sum(nums)/len(nums)
– Sum-summation 25
– etc
How Long is a List?

• The len() function takes a
list as a parameter and
returns the number of >>> greet = 'Hello Bob‘
elements in the list >>> print len(greet)
• Actually len() tells us the 9
>>> x = [ 1, 2, 'joe', 99]
number of elements of
>>> print len(x)
any set or sequence (i.e.
4
such as a string...)
>>>
Using the range function

• The range function >>> print range(4)
returns a list of numbers [0, 1, 2, 3]
that range from zero to >>> friends = ['Joseph', 'Glenn', 'Sally']
one less than the >>> print len(friends)
3
parameter
>>> print range(len(friends))
• We can construct an [0, 1, 2]
index loop using for and >>>
an integer iterator
List Methods
Python list methods

• >>> x = list()
• >>> type(x)
• <type 'list‘
• >>>> dir(x) ['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove',
'reverse', 'sort']

list.append(obj) Appends object obj to list
list.count(obj) Returns count of how many times obj occurs in list
list.extend(seq) Appends the contents of seq to list
list.index(obj) Returns the lowest index in list that obj appears
list.insert(index, obj) Inserts object obj into list at offset index
list.pop(obj=list[-1]) Removes and returns last object or obj from list
list.remove(obj) Removes object obj from list
list.reverse() Reverses objects of list in place
list.sort([func]) Sorts objects of list, use compare func if given
Lists are Mutable
>>> fruit = 'Banana‘
• Strings are >>> fruit[0] = 'b‘
"immutable" - we Traceback
cannot change the TypeError: 'str' object does not
contents of a string - support item assignment
we must make a new >>> x = fruit.lower()
string to make any >>> print x
change Bannna
• Lists are "mutable" - >>> lotto = [2, 14, 26, 41, 63]
>>> print lotto
we can change an
[2, 14, 26, 41, 63]
element of a list using
>>> lotto[2] = 28
the index operator
>>> print lotto
[2, 14, 28, 41, 63]
Assig 3

• List1 = [1,3,6,7,5,4,9,10, 12, 15]
• List2 = [2,3,4,7,11,12,10, 13, 14, 15]
• Find a new list ,
– listc=[], which contains the common elements
from the two lists above
– listu=[] which contains the union elements from
the two lists above
Python Dictionaries

• Dictionaries are Python’s most powerful data collection
• Dictionaries allow us to do fast database-like operations in Python
• Dictionaries are associative arrays or hash tables, that associate
– each key with a particular value.
• Example
– Dictionary:
– key = word,
– value = definition
– Phone book:
– key = name,
– value = phone number ,…, etc.
Dictionary
• Building directory
value = phone number
key = name
Alex 0918 772200
Bilal 772067
Derje 771044
Mubark 773122
Ted 773156

• Dictionary have a list of key : value pairs. Simply
• Dictionaries are created by putting key/value pairs
inside curly braces .
Dictionary Literals (Constants)
• Let’s make an empty dictionary using empty curly braces

>>> d = {} # empty dictionary
>>> d['Alex'] = 0918 772200
>>> d['Bilal'] = 772067
>>> d
{'Bilal': 772067 , 'Alex': 0918 772200}

• Here Alex and Bilal are keys, 0918 772200 and 772067 are
values of dictionary d

>>> d['Bilal'] = 772077
# Bilal changes phone No
>>> d
{'Bilal': 772077 , 'Alex': 772200}
>>> bag = dict()
>>> bag['money'] = 12 • Lists index their
>>> bag['candy'] = 3 entries based on the
>>> bag['tissues'] = 75 position in the list
>>> print bag • Dictionaries are like
bags - no order
{'money': 12, 'tissues': 75, 'candy': 3} • So we index things
we put in the
>>> purse['candy'] = purse['candy'] + 2 dictionary with a
>>> print purse “lookup tag”
{'money': 12, 'tissues': 75, 'candy': 5}

>>> print purse['candy']
Comparing Lists and Dictionaries
• Dictionaries are like Lists except that they use keys
instead of numbers to look up values(index)
• Keys in a dictionary are unique-immutable

>>> lst = list() >>> dd = dict()
>>> lst.append(21) >>> dd['age'] = 21
>>> lst.append(183) >>> dd['course'] = 182
>>> print lst >>> print dd
[21, 183] {'course': 182, 'age': 21}
>>> lst[0] = 23 >>> dd['age'] = 23
>>> print lst >>> print dd
[23, 183] {'course': 182, 'age': 23}
Built in Dictionary Method

• Some of dict() methods are
['__class__', '__cmp__', '__contains__', '__delattr__',
'__delitem__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__',
'__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__setitem__', '__sizeof__', '__str__', '__subclasshook__',
'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items',
'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop',
'popitem', 'setdefault', 'update', 'values', 'viewitems',
'viewkeys', 'viewvalues']
Built in Dictionary Method

>>> d= {'Bilal': 772067 , 'Alex': 0918 772200, 'Almaz': 772340}
>>> d['Bilal'] # dictionary access
772067
>>> d.keys() # return list of keys
['Bilal', ‘Alex', 'Almaz']
>>> d.values() # return list of values
[772067, 772200, 772340]
>>> d.items() # returns list of (key, value) tuples

[('Bilal', 772067), (‘Alex', 772200), ('Almaz', 772340)]

>>> d.pop('Bilal') # remove (key, value) pair
772067
>>> d
d= { 'Alex': 772200, 'Almaz': 772340}
The get method for dictionary
• Pattern of checking to see if a key is already in a dictionary and assuming a
default value
• if the key is not there that there is a method called get() that does this for
us. Default value if key does not exist (and no Traceback).

• Example:

L = ['a', 'b', 'a', 'c', 'c', 'b', 'a', 'd']

d = {}
for x in L:

d[x] = d.get(x,0) + 1

print d

{'a': 3, 'c': 2, 'b': 2, 'd': 1}
 arabic2roman = { 1 : “I”, 2 : ”II”, 3 : “III”, 4 :
“IV”, 5 : “V” }

• get returns the value associated with a key or returns some user-specified
value if the key isn’t in the dictionary;
 You can use the get( index, default) method to return a default value if
the index does not exist. For example:

arabic2roman.get(1,”None”)

produces 'I‘

arabic2roman.get(5, “None”)

produces 'V‘

arabic2roman.get(500, “None”)

produces 'None'
Simplified counting with get()

• We can use get() and provide a default value of zero
when the key is not yet in the dictionary - and then just
add one
counts = dict()
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']
for name in names :
counts[name] = counts.get(name, 0) + 1
print counts

Default {'csev': 2, 'zqian': 1, 'cwen': 2}
Counting Pattern

counts = dict()
print 'Enter a line of text:‘ • The general pattern to
line = raw_input(‘ ') count the words in a line
words = line.split() of text is to split the line
print 'Words:', words into words,
print 'Counting...‘ • then loop through the
for word in words: words and use a
counts[word] = dictionary to track the
counts.get(word,0) + 1 count of each word
print 'Counts', counts independently.
Definite Loops and Dictionaries
• Even though dictionaries are not stored in order, we
can write a for loop that goes through all the entries in
a dictionary
– actually it goes through all of the keys in the
dictionary and looks up the values
>>> counts = { 'chuck' : 17 , 'fred' : 42, 'jan': 100}
>>> for key in counts:
print key, counts[key]...
jan 100
chuck 17
fred 42
Retrieving lists of Keys and Values

>>> jj = { 'chuck' : 17 , 'fred' : 42, 'jan': 100}
• You can get a list of >>> print list(jj)
keys, values or items ['jan', 'chuck', 'fred']
(both) from a >>> print jj.keys()
dictionary using ['jan', 'chuck', 'fred']
• .items ( ) >>> print jj.values()
function/method [100, 17, 42]
>>> print jj.items()
[('jan', 100), ('chuck', 17), ('fred', 42)]
>>>

What is a 'tuple'?
Bonus: Two Iteration Variables!

• We loop through
the key-value pairs
>>> jj = { 'chuck' : 17 , 'fred' : 42, 'jan': 100}
in a dictionary
>>> for a,b in jj.items() :
using *two*
... print a, b
iteration variables
...
• Each iteration, the jan , 100
first variable is the Chuck, 17
key and the second Fred, 42
variable is the >>>
corresponding
value for the key What is a 'tuple'?
Assigment 4 (Counting Words)
Words: ['the', 'clown', 'ran',
'after', 'the', 'car', 'and',
python word count let the a line of 'the', 'car', 'ran', 'into',
text: 'the', 'tent', 'and', 'the',
'tent', 'fell', 'down', 'on',
the clown ran after the car and the car 'the', 'clown', 'and', 'the',
ran into the tent and the tent fell 'car']
down on the clown and the car Counting...and produce
this
Counts {'and': 3, 'on': 1,
'ran': 2, 'car': 3, 'into': 1,
'after': 1, 'clown': 2,
'down': 1, 'fell': 1, 'the': 7,
'tent': 2}
Tuples

 Tuples are another kind of sequence that function much like a
list
 They are similar to lists:
 the values can be any type
 they are indexed by integers
 Syntactically a tuple is a comma-separated list of values:

t = 'a', 'b', 'c', 'd', 'e'
 Although it is not necessary, it is common to enclose tuples in
parentheses
t = ('a', 'b', 'c', 'd', 'e’)
 With no argument, the tuple ( ) function creates a new empty
tuple
t = tuple( )
 If the argument is a sequence (string, list or tuple), the result is
a tuple with the elements of the sequence:
>>> t = tuple(‘lupins’)
print t
 Note:To create a tuple with a single element, you have to
include a final comma:
>>> t1 = 'a’,
type(t1)
Tuples

 Most list operators also work on tuples:
index, len, min, max, and sum
• Tuples like lists have elements which are indexed starting at 0
• >>> x = ('Glenn', 'Sally', 'Joseph')
• >>> print x[2]
Joseph
• >>> print x[0]

>>> print x[1:3]
Tuple

 Example of max, min and sum
• >>> y = ( 1, 9, 2 )
• >>> print y
(1, 9, 2)
• >>> print max(y)
9
 >>> print min(y)
 >>> print sum(y)
Tuples are "immutable"

• Unlike a list, once you create a tuple, you cannot
alter its contents - similar to a string

>>> x = [9, 8, 7] >>> y = 'ABC‘ >>> z = (5, 4, 3)
>>> x[2] = 6 >>> y[2] = D‘ >>> z[2] = 0
>>> print x Traceback:'str' Traceback:'tuple'
[9, 8, 6] object does object does
>>> not support item not support item
Assignment Assignment
>>> >>>
Things not to do with tuples

>>> x = (3, 2, 1)
>>> x.sort()
Traceback:AttributeError: 'tuple' object has no attribute 'sort‘
>>> x.append(5)
Traceback:AttributeError: 'tuple' object has no attribute 'append‘
>>> x.reverse()
Traceback:AttributeError:
'tuple' object has no attribute 'reverse‘
>>>
Tuples and Assignment

• We can also put a tuple on the left hand
side of an assignment statement
• We can even omit the parenthesis

 It is often useful to swap the
>>> (x, y) = (4, 'fred') values of two variables, e.g.
>>> print y swap “a” with “b”
Fred
>>> (a, b) = (99, 98) >> temp=a
>>> print a >> a=b
99
>> b=temp
Tuples and Dictionaries

• The items() method in
dictionaries returns a list >>> d = dict()
of) tuples >>> d['csev'] = 2
• items ( ) func(key,value) >>> d['cwen'] = 4
used on dictionaries we >>> for (k,v) in d.items():
saw early actually returns print k, v
a list of tuples, e.g.
csev 2 cwen 4
>>> d = {'a':0, 'b':1,
'c':2} >>> tups = d.items()
>>> d.items( ) >>> print tups
[('csev', 2), ('cwen', 4)]
[('a', 0), ('c', 2), ('b', 1)]
Sorting Lists of Tuples
• We can take advantage of the ability to sort a list
of tuples to get a sorted version of a dictionary
• First we sort the dictionary by the key using the
items() method

>>> d = {'a':10, 'b':1, 'c':22}
>>> t = d.items()
>>> t
[('a', 10), ('c', 22), ('b', 1)]
>>> t.sort()
t[('a', 10), ('b', 1), ('c', 22)]
Using sorted()

We can do this even
>>> d = {'a':10, 'b':1, 'c':22}
more directly using
>>> d.items()
the built-in function
[('a', 10), ('c', 22), ('b', 1)]
sorted that takes a >>> t = sorted(d.items())
sequence as a >>> t
parameter and [('a', 10), ('b', 1), ('c', 22)]
returns a sorted
sequence >>> for k, v in sorted(d.items()):
print k, v
a 10 b 1 c 22
Sort by values instead of key

• If we could
construct a list >>> c = {'a':10, 'b':1,
'c':22}
of tuples of
>>> tmp = list()
the form
>>> for k, v in c.items() :
(value, key) tmp.append( (v, k) )
we could sort >>> print tmp
by value [(10, 'a'), (22, 'c'), (1, 'b')]
• We do this >>> tmp.sort(reverse=True)
with a for loop >>> print tmp
that creates a [(22, 'c'), (10, 'a'), (1, 'b')]
list of tuples