0% found this document useful (0 votes)
2 views

unit-2 - More data types- List, tuples, set, dict

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

unit-2 - More data types- List, tuples, set, dict

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

More Data Types

28
Everything is an object

 Everything means
>>> x = 7
everything, >>> x
including functions 7
and classes (more >>> x = 'hello'
on this later!) >>> x
'hello'
 Data type is a
>>>
property of the
object and not of
the variable
Numbers: Integers

 Integer – the
equivalent of a C long >>> 132224
 Long Integer – an 132224
unbounded integer >>> 132323 **
2
value. 17509376329L
>>>
Numbers: Floating Point

 int(x) converts x to >>> 1.23232


1.2323200000000001
an integer
>>> print 1.23232
 float(x) converts x 1.23232
to a floating point >>> 1.3E7
13000000.0
 The interpreter
>>> int(2.0)
shows 2
a lot of digits >>> float(2)
2.0
Numbers: Complex

 Built into Python


 Same operations are >>> x = 3 + 2j
>>> y = -1j
supported as integer
>>> x +y
and float (3+1j)
>>> x *y
(2-3j)
String Literals

 + is overloaded to do
concatenation >>> x = 'hello'
>>> x = x + ' there'
>>> x
'hello there'
String Literals
 Can use single or double quotes, and
three double quotes for a multi-line
string

>>> 'I am a string'


'I am a string'
>>> "So am I!"
'So am I!'
Substrings and Methods

•len(String) – returns the


>>> s = '012345' number of characters in
>>> s[3] the String
'3'
>>> s[1:4] •str(Object) – returns a
'123' String representation of
>>> s[2:] the Object
'2345'
>>> len(x)
>>> s[:4]
6
'0123'
>>>
>>> s[-2]
str(10.3)
'4'
'10.3'
String Formatting
 Similar to C’s printf
 <formatted string> % <elements to
insert>
 Can usually just use %s for everything,
it will convert the object to its String
representation.
>>> "One, %d, three" % 2
'One, 2, three'
>>> "%d, two, %s" % (1,3)
'1, two, 3'
>>> "%s two %s" % (1, 'three')
'1 two three'
>>>
Types for Data Collection
List, Set, and Dictionary

 List

 Ordered Pairs of values


 Unordered list
Lists
 Ordered collection of
data
>>> x = [1,'hello', (3 + 2j)]
 Data can be of
>>> x
different types [1, 'hello', (3+2j)]
 Lists are mutable >>> x[2]
(3+2j)
 Issues with shared
>>> x[0:2]
references and [1, 'hello']
mutability
 Same subset

operations as Strings
List Functions
 list.append(x)
 Add item at the end of the list.

 list.insert(i,x)
 Insert item at a given position.

 Similar to a[i:i]=[x]

 list.remove(x)
 Removes first item from the list with value x

 list.pop(i)
 Remove item at position I and return it. If no index I is given then
remove the first item in the list.
 list.index(x)
 Return the index in the list of the first item with value x.

 list.count(x)
 Return the number of time x appears in the list

 list.sort()
 Sorts items in the list in ascending order

 list.reverse()
 Reverses items in the list
Lists: Modifying Content

 x[i] = a reassigns >>> x = [1,2,3]


the ith element to the >>> y = x
value a >>> x[1] = 15
>>> x
 Since x and y point to
[1, 15, 3]
the same list object, >>> y
both are changed [1, 15, 3]
 The method append >>> x.append(12)
also modifies the list >>> y
[1, 15, 3, 12]
Lists: Modifying Contents
>>> x = [1,2,3]
 The method >>> y = x
append >>> z = x.append(12)
modifies the list >>> z == None
and returns True
None >>> y
[1, 2, 3, 12]
 List addition
>>> x = x + [9,10]
(+) returns a
>>> x
new list [1, 2, 3, 12, 9, 10]
>>> y
[1, 2, 3, 12]
>>>
Using Lists as Stacks
 You can use a list as a stack
>>> a = ["a", "b", "c“,”d”]
>>> a
['a', 'b', 'c', 'd']
>>> a.append("e")
>>> a
['a', 'b', 'c', 'd', 'e']
>>> a.pop()
'e'
>>> a.pop()
'd'
>>> a = ["a", "b", "c"]
>>>
Tuples

 Tuples are immutable


versions of lists
 One strange point is >>> x = (1,2,3)
>>> x[1:]
the format to make a
(2, 3)
tuple with one >>> y = (2,)
element: >>> y
‘,’ is needed to (2,)
differentiate from the >>>

mathematical
expression (2)
Sets
 A set is another python data structure that is an unordered
collection with no duplicates.
>>> setA=set(["a","b","c","d"])
>>> setB=set(["c","d","e","f"])
>>> "a" in setA
True
>>> "a" in setB
False
Sets
>>> setA - setB
{'a', 'b'}
>>> setA | setB
{'a', 'c', 'b', 'e', 'd', 'f'}
>>> setA & setB
{'c', 'd'}
>>> setA ^ setB
{'a', 'b', 'e', 'f'}
>>>
Dictionaries

 A set of key-value pairs


 Dictionaries are mutable

>>> d= {‘one’ : 1, 'two' : 2, ‘three’ : 3}


>>> d[‘three’]
3
Dictionaries: Add/Modify
 Entries can be changed by assigning to
that entry
>>> d
{1: 'hello', 'two': 42, 'blah': [1, 2, 3]}
>>> d['two'] = 99
>>> d
{1: 'hello', 'two': 99, 'blah': [1, 2, 3]}

• Assigning to a key that does not exist


adds an entry
>>> d[7] = 'new entry'
>>> d
{1: 'hello', 7: 'new entry', 'two': 99, 'blah': [1, 2, 3]}
Dictionaries: Deleting Elements

 The del method deletes an element from a


dictionary

>>> d
{1: 'hello', 2: 'there', 10: 'world'}
>>> del(d[2])
>>> d
{1: 'hello', 10: 'world'}
Iterating over a dictionary

>>>address={'Wayne': 'Young 678', 'John': 'Oakwood 345',


'Mary': 'Kingston 564'}
>>>for k in address.keys():
print(k,":", address[k])

Wayne : Young 678


John : Oakwood 345
Mary : Kingston 564
>>>

>>> for k in sorted(address.keys()):


print(k,":", address[k])

John : Oakwood 345


Mary : Kingston 564
Wayne : Young 678
>>>
Copying Dictionaries and Lists

 The built-in >>> l1 = [1] >>> d = {1 : 10}


list function >>> l2 = list(l1) >>> d2 = d.copy()
>>> l1[0] = 22 >>> d[1] = 22
will copy a list
>>> l1 >>> d
 The dictionary [22] {1: 22}
has a method >>> l2 >>> d2
called copy [1] {1: 10}
Data Type Summary
Integers: 2323, 3234L
Floating Point: 32.3, 3.1E2
Complex: 3 + 2j, 1j
Lists: l = [ 1,2,3]
Tuples: t = (1,2,3)
Dictionaries: d = {‘hello’ : ‘there’, 2 : 15}

 Lists, Tuples, and Dictionaries can store


any type (including other lists, tuples,
and dictionaries!)
 Only lists and dictionaries are mutable

 All variables are references

You might also like