0% found this document useful (0 votes)
11 views100 pages

Day2.2 DataAnalyticsLanguages

Data analytics

Uploaded by

Rishav Dhama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views100 pages

Day2.2 DataAnalyticsLanguages

Data analytics

Uploaded by

Rishav Dhama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 100

Data Analytics Languages--

Python

10-08-2022 Slide 1
History

• Python created by Guido van Rossum in the


Netherlands in 1990
• Popular programming language
• Widely used in industry and academia
• Simple, intuitive syntax
• Rich library
• Two versions in existence today Python 2 and
Python 3
10-08-2022 2
Interpreted Language

• Python is an interpreted language as opposed


to being compiled
• An interpreter reads a high level program and
executes it
• A compiler translates the program into an
executable object code first which is
subsequently executed

10-08-2022 3
Numpy

• NumPy is the fundamental package for scientific


computing with Python. It contains among other
things:
• a powerful N-dimensional array object
• sophisticated (broadcasting) functions
• tools for integrating C/C++ and Fortran code
• useful linear algebra, Fourier transform, and random
number capabilities

10-08-2022 4
Matplotlib

• Matplotlib is a Python 2D plotting library


which produces publication quality figures in
a variety of hardcopy formats and interactive
environments across platforms.

10-08-2022 5
pandas

• pandas is an open source, BSD-licensed


library providing high-performance, easy-to-
use data structures and data analysis tools
for Python

10-08-2022 6
Functions

10-08-2022 7
Types of Functions

• Built in functions e.g. print(), type(), input(),


max(),min() etc.
• User defined functions help in code reuse and
in organizing and simplifying code

10-08-2022 8
Types of Functions

• Built in functions e.g. print(), type(), input(),


max(),min() etc.
• User defined functions help in code reuse and
in organizing and simplifying code

10-08-2022 9
Built-in Functions
• >>>max(2,3)
• 3
• >>>max(2,3,4)
• 4
• >>>max(max(2,3),4)
• 4
• >>>max(‘abc’)
• c
• >>>max(‘abc’,’bcd’)
• bcd
• >>>max(2,’two’)
• Error

10-08-2022 10
Function Call

• >>> def plus2(a):


• return(a+2)
• >>>plus2(7)
• 9

10-08-2022 11
Multiple Arguments

• >>> def maxOf3(x, y, z):


max3 = max(max(x,y),z)
return max3

• >>> maxOf3(15, 23, 12)


• 23
• >>> maxOf3(15, 23.1,12)
• 23.1

10-08-2022 12
Multiple Return Values

• >>> def maximinOf3(x, y, z):


max3 = max(max(x,y),z)
min3 = min(min(x,y),z)
return (max3, min3)

• >>> maximinOf3(15, 23, 12)


• (23 12)
• >>> maximinOf3(15, 23.1,12.5)
• (23.1, 12.5)

10-08-2022 13
No Return Values

• >>> def maximinOf3(x, y, z):


max3 = max(max(x,y),z)
min3 = min(min(x,y),z)
print (max3, min3)

• >>> maximinOf3(15, 23, 12)


• 23 12
• This is a void function
• Functions which return values are fruitful

10-08-2022 14
Scope of
parameters and variables
• >>>a=1
• >>>def plus4(x):
• b = x+4
• return b
• >>> a
• 1
• >>>plus4(5)
• 9
• >>>b
• Error
• >>>x
• Error

10-08-2022 15
Recursion
• >>>def fib(n):
• if(n==0):
• return 0
• elif(n==1):
• return 1
• else:
• return(fib(n-1)+fib(n-2))
• >>>fib(6)
• 8

10-08-2022 16
Sanity Check
• >>>def fib(n):
• if(not isinstance(n, int)):
• return -1
• if(n < 0):
return -1
• if(n==0):
• return 0
• elif(n==1):
• return 1
• else:
• return(fib(n-1)+fib(n-2))
• >>>fib(6)
• 8

10-08-2022 17
Python Loops

10-08-2022 18
The while loop
• n = 12
• >>>while (n < 20):
• n += 1
• print(n)
• >>>13
• >>>14
• >>>…
• >>>…
• >>>20
• NB: ++ operator does not exist in Python

10-08-2022 19
Counting downwards

• n = 256
• >>>while (n > 1):
• n /= 2
• print(n)
• >>>128.0
• >>>64.0
• >>>…
• >>>…
• >>>1.0

10-08-2022 20
Infinite loop

• n = 12
• >>>while (n > 2):
• n += 1
• print(n)

10-08-2022 21
Exiting loops using break

• >>> n = 2460
• >>>while (n > 1):
• n //= 2
• print (n)
• if(n%2 == 1):
• break
• >>>1230
• >>> 615

10-08-2022 22
The continue statement
• >>> n = 2460
• >>>while (n > 1):
• n //= 2
• if(n%2 == 1):
• continue
• print (n)
• >>>1230
• >>>76
• >>>38
• >>>4
• >>>2
• >>>0

10-08-2022 23
Using for loops

• >>>for i in range(2,8):
• print(i)
• >>>2
• >>>3
• >>>4
• >>>5
• >>>6
• >>>7

10-08-2022 24
Using for loops
• >>>for i in sequence:
• print(i)
• In Python sequences are lists, strings, tuples
• >>>for i in (2,4,6,8):
• print(i)
• >>>for i in [2,4,6,8]:
• print(i)
• >>>for i in “2468”:
• print(i)
• All print 2, 4, 6, 8 in consecutive lines

10-08-2022 25
Strings

10-08-2022 26
Compound data type
• Strings are made up of smaller units – characters and we
may access the whole or its parts
• A character in Python is a string of size 1
• Both single and double quotes can be used e.g. “fruits”
or ‘fruits’
• >>>name = “sachin”
• >>>print(name)
• >>>sachin
• >>>print(name[0])
• >>>s

10-08-2022 27
Length of a string
• Length can be found using the len() function
• fruit = “apple”
• >>>len(fruit)
• 5
• >>>len(“apple”)
• 5
• >>>fruit[5]
• Error
• >>>fruit[len(fruit)-1]
• >>>’e’

10-08-2022 28
String Slices

• >>>fruit = “apple”
• >>>fruit[1:3]
• >>>’pp’
• >>>fruit[1:]
• >>>’pple’
• >>>fruit[:4]
• >>>’appl’
• >>>fruit[:]
• >>>’apple’

10-08-2022 29
String Comparison
• >>>“apple” < “banana”
• True
• >>>”mango” < “banana”
• False
• >>>”mango” < “mango”
• False
• >>>”mango” < “Mango”
• False
• >>>”Mango” < “mango”
• True

10-08-2022 30
Strings are immutable
• >>>fruit = “Mango”
• >>>dance = fruit
• >>>dance
• ‘Mango’
• >>>dance[0]
• ‘M’
• >>>dance[0]=‘T’
• Error
• >>>dance = ‘T’+fruit[1:]
• >>>dance
• ‘Tango’

10-08-2022 31
String methods

• >>>import string
• >>>fruit=‘mango’
• >>>fruit.find(“go”)
• 3
• >>>fruit.upper()
• ‘MANGO’

10-08-2022 32
String methods

• dir(fruit) lists the methods associated with the


object fruit i.e. string methods
• >>>type(fruit)
• <class ‘str’>
• help(str.methodname) gives a description of
the method
• >>>help(str.isupper)

10-08-2022 33
Python Lists

10-08-2022 34
Sequences in Python

• Common sequeces types


• Strings
• Lists
• Tuples
• Others: bytes, bytearray, range

10-08-2022 35
List Values

• Examples
• [3, 5, 7, 11, 13, 17, 19]
• [2.5, 3, 4, 7.7]
• [2.5, “two”, 3, 4, “five”, 7]
• Mixed types are allowed in a list
• [2.5, 13, “seven”, [8,9,10]]
• Nested lists are also allowed

10-08-2022 36
Constructing List from Ranges

• >>>a = range(3, 7)
• >>>type(a)
• <class ’range’>
• >>>b = list(a)
• >>>b
• [3, 4, 5, 6]
• >>>type(b)
• <class ‘list’>

10-08-2022 37
Access

• >>>b = [3, 4, 5, 6]
• >>>b[0]
• 3
• >>>b[3]
• 6
• >>>b[4]
• Error
• >>>b[3-2]
• 4

10-08-2022 38
Access

• If there are n elements in list b, we can access the


elements as b[0] through b[n-1]
• b[-i] is an alias for b[n-i]
• >>>b[-1]
• 6
• >>>b[-4]
• 3
• >>>b[-5]
• Error

10-08-2022 39
Length, Min, Max, Count
• >>>b
• [3, 4, 5, 6]
• >>>len(b)
• 4
• >>>min(b)
• 3
• >>>max(b)
• 6
• >>>sum(b)
• 18

10-08-2022 40
List Membership

• >>>b
• [3, 4, 5, 6]
• >>>3 in b
• True
• >>>”b” in b
• False
• >>>”3” not in b
• True

10-08-2022 41
List Operations

• Concatenation
• >>>b
• [3, 4, 5, 6]
• >>>b+b
• [3, 4, 5, 6, 3, 4, 5, 6]
• >>>2*b
• [3, 4, 5, 6, 3, 4, 5, 6]
• >>>b*2
• [3, 4, 5, 6, 3, 4, 5, 6]

10-08-2022 42
List Slices

• >>>b
• [3, 4, 5, 6]
• >>>b[0:3]
• [3,4,5]
• b[0:j] with j > 3 and b[0:] are same
• >>>b[:2]
• [3,4]

10-08-2022 43
List Slices

• >>>b[2:2]
• []
• b[i:j:k] is a subset of b[i:j] with elements
picked in steps of k
• >>>b=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
• >>>b[0:10:3]
• [1, 4, 7]

10-08-2022 44
Python Lists are Mutable

• Unlike strings and tuples, lists are mutable in


Python
• >>>b
• [3,4,5,6]
• Insert at a location
• >>>b[0] = 13
• >>>b
• [13, 4, 5, 6]

10-08-2022 45
Python Lists are Mutable

• >>>b
• [13, 4, 5, 6]
• Mutiple assignment
• >>>b[1:3] =[14, 15]
• >>>b
• [13, 14, 15, 6]

10-08-2022 46
Python Lists are Mutable
• Adding at the end
• >>>b[5:5] = 16
• Error
• >>>b[5:5] = [16]
• >>>b
• [13, 14, 15, 6, 16]
• Deletion
• >>>b[3:4] = []
• >>>b
• [13, 14, 15, 16]

10-08-2022 47
Copying
• >>>list1=list2
• >>>list1
• [‘a’,’b’,’c’,’d’]
• >>>list2
• [‘a’,’b’,’c’,’d’]
• >>>list1[0]=‘e’
• >>>list1
• [‘e’,’b’,’c’,’d’]
• >>>list2
• [‘e’,’b’,’c’,’d’]

10-08-2022 48
Cloning
• >>>list1=list2[:]
• >>>list1
• [‘a’,’b’,’c’,’d’]
• >>>list2
• [‘a’,’b’,’c’,’d’]
• >>>list1[0]=‘e’
• >>>list1
• [‘e’,’b’,’c’,’d’]
• >>>list2
• [‘a’,’b’,’c’,’d’]

10-08-2022 49
Lists as Function Arguments
• Passing a list as a parameter actually passes a reference
not a copy
• >>>def deleteFirst(list):
• del(list[0])
• >>>lista=[12, 17, 22]
• >>>lista
• [12, 17, 22]
• >>>deleteFirst(lista)
• >>>lista
• [17, 22]

10-08-2022 50
Nested Lists

• >>>lista=[‘a’,’b’,’c’,[‘d’,’e’,’f’]]
• >>>lista[3]
• [‘d’,’e’,’f’]
• >>>lista[3][1]
• ‘e’
• Square brackets evaluate left to right

10-08-2022 51
Python Regex

10-08-2022 Slide 52
Regular Expressions

In computing, a regular expression, also referred to as


"regex" or "regexp", provides a concise and flexible
means for matching strings such as particular
characters, words, or patterns of characters. A regular
expression is written in a formal language that can be
interpreted by a regular expression processor.

http://en.wikipedia.org/wiki/Regular_expression

10-08-2022 53
Python Regular Expressions
^ Matches the beginning of a line
$ Matches the end of the line
. Matches any character
\s Matches whitespace
\S Matches any non-whitespace character
* Repeats a character zero or more times
*? Repeats a character zero or more times (non-greedy)
+ Repeats a chracter one or more times
+? Repeats a character one or more times (non-greedy)
[aeiou] Matches a single character in the listed set
[^XYZ] Matches a single character not in the listed set
[a-z0-9] The set of characters can include a range
( Indicates where string extraction is to start
) Indicates where string extraction is to end

10-08-2022 54
Regular Expressions Module

• Before you can use regular expressions in your


program, you must import the library using
"import re"
• You can use re.search() to see if a string matches
a regular expression similar to using the find()
method for strings
• You can use re.findall() extract portions of a
string that match your regular expression similar
to a combination of find() and slicing:
var[5:10]
10-08-2022 55
Wild-Card Characters

• The dot character matches any character


• If you add the asterisk character, the character
is "any number of times"
X-Sieve: CMU Sieve 2.3
X-DSPAM-Result: Innocent
X-DSPAM-Confidence: 0.8475 ^X.*:
X-Content-Type-Message-Body: text/plain

10-08-2022 56
Wild-Card Characters

• The dot character matches any character


• If you add the asterisk character, the character
is "any number of times"
Match the start of the line Many times
X-Sieve: CMU Sieve 2.3
X-DSPAM-Result: Innocent
X-DSPAM-Confidence: 0.8475 ^X.*:
X-Content-Type-Message-Body: text/plain
Match any character

10-08-2022 57
Wild-Card Characters

• Depending on how "clean" your data is and


the purpose of your application, you may want
to narrow your match down a bit
Match the start of the line Many times
X-Sieve: CMU Sieve 2.3
X-DSPAM-Result: Innocent
X-DSPAM-Confidence: 0.8475 ^X.*:
X-Content-Type-Message-Body: text/plain
Match any character

10-08-2022 58
Greedy Matching

• The repeat characters (* and +) push outward in both


directions (greedy) to match the largest possible string
One or more
>>> import re characters
>>> x = 'From: Using the : character'
>>> y = re.findall('^F.+:', x)
>>> print y
^F.+:
['From: Using the :']
First character in the Last character in the
Why not 'From:'? match is an F match is a :

10-08-2022 59
Non-Greedy Matching

• Not all regular expression repeat codes are greedy!


If you add a ? character - the + and * chill out a bit...
One or more
>>> import re characters but
>>> x = 'From: Using the : character' not greedily
>>> y = re.findall('^F.+?:', x)
>>> print y
^F.+?:
['From:']
First character in the Last character in the
match is an F match is a :

10-08-2022 60
Python Slicing

10-08-2022 Slide 61
String Slices
• >>>fruit = “apple”
• >>>fruit[1:3]
• >>>’pp’
• >>>fruit[1:]
• >>>’pple’
• >>>fruit[:4]
• >>>’appl’
• >>>fruit[:]
• >>>’apple’

10-08-2022 62
List Slices
• >>>b
• [3, 4, 5, 6]
• >>>b[0:3]
• [3,4,5]
• b[0:j] with j > 3 and b[0:] are same
• >>>b[:2]
• [3,4]

10-08-2022 63
List Slices
• >>>b[2:2]
• []
• b[i:j:k] is a subset of b[i:j] with elements
picked in steps of k
• >>>b=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
• >>>b[0:10:3]
• [1, 4, 7]

10-08-2022 64
NumPy array slicing
• 1-d array slicing and indexing is similar to
Python lists
• import numpy as np
• arr1=np.array([1,2,5,6,4,3])
• arr1[2:4]=99

• arr1
• Out[8]: array([ 1, 2, 99, 99, 4, 3])

10-08-2022 65
NumPy array slicing

• Slicing in ndarrays is different from Python lists in that


data is not copied
• Slices are views on the original array!
• arr2=arr1[2:4]

• arr2[0]=88

• arr1
• Out[13]: array([ 1, 2, 88, 99, 4, 3])

10-08-2022 66
Sets

10-08-2022 Slide 67
in and notin
• >>>setA= {1,3,5,7}
• >>>3 in setA
• True
• >>>3 not in setA
• False
• >>>4 not in setA
• True

10-08-2022 68
Subset
• >>>setA= {1,3,5,7}
• >>>setB= {1, 3, 5, 7, 9}
• >>>setC = {1,3,5,9,10}
• >>>setA issubset setB
• True
• >>> setA issubset setC
• False

10-08-2022 69
Superset
• >>>setA= {1,3,5,7}
• >>>setB= {1, 3, 5, 7, 9}
• >>>setC = {1,3,5,9,10}
• >>>setA issuperset setB
• False
• >>> setB issuperset setA
• True
• >>> setC issuperset setA
• False

10-08-2022 70
Set Union

• >>>setA= {1,3,5,7}
• >>>setB= {7, 5, 9}
• >>>setA.union(setB)
• {1,3,5,7,9}
• >>>setA | setB
• {1, 3, 5, 7, 9}

10-08-2022 71
Set Intersection

• >>>setA= {1,3,5,7}
• >>>setB= {7, 5, 9}
• >>>setA.intersection(setB)
• {5,7}
• >>>setA & setB
• {5, 7}

10-08-2022 72
Dictionaries

10-08-2022 Slide 73
Chapter 9
Python
Dictionaries
What is a Collection?

• A collection is nice because we can put more than one value in it


and carry them all around in one convenient package

• We have a bunch of values in a single “variable”

• We do this by having more than one place “in” the variable

• We have ways of finding the different places in the variable


What is Not a “Collection”?

Most of our variables have one value in them - when we put a new
value in the variable - the old value is overwritten

$ python
>>> x = 2
>>> x = 4
>>> print(x)
4
A Story of Two Collections ..
• List

- A linear collection of values that stay in order

• Dictionary

- A “bag” of values, each with its own label


Dictionaries

calculator tissue

perfume
money
candy

http://en.wikipedia.org/wiki/Associative_array
Dictionaries

• Dictionaries are Python’s most powerful data collection

• Dictionaries allow us to do fast database-like operations in Python

• Dictionaries have different names in different languages

- Associative Arrays - Perl / PHP

- Properties or Map or HashMap - Java

- Property Bag - C# / .Net


Dictionaries

• Lists index their entries >>> purse = dict()


based on the position in the >>> purse['money'] = 12
>>> purse['candy'] = 3
list >>> purse['tissues'] = 75
>>> print(purse)
{'money': 12, 'tissues': 75, 'candy': 3}
• Dictionaries are like bags - >>> print(purse['candy'])
no order 3
>>> purse['candy'] = purse['candy'] + 2
>>> print(purse)
• So we index the things we {'money': 12, 'tissues': 75, 'candy': 5}
put in the dictionary with a
“lookup tag”
Comparing Lists and Dictionaries

Dictionaries are like lists except that they use keys instead of
numbers to look up values

>>> lst = list() >>> ddd = dict()


>>> lst.append(21) >>> ddd['age'] = 21
>>> lst.append(183) >>> ddd['course'] = 182
>>> print(lst) >>> print(ddd)
[21, 183] {'course': 182, 'age': 21}
>>> lst[0] = 23 >>> ddd['age'] = 23
>>> print(lst) >>> print(ddd)
[23, 183] {'course': 182, 'age': 23}
>>> lst = list() List
>>> lst.append(21) Key Value
>>> lst.append(183)
>>> print(lst) [0] 21
[21, 183] lst
>>> lst[0] = 23 [1] 183
>>> print(lst)
[23, 183]

>>> ddd = dict()


Dictionary
>>> ddd['age'] = 21
>>> ddd['course'] = 182 Key Value
>>> print(ddd)
{'course': 182, 'age': 21} ['course'] 182
>>> ddd['age'] = 23 ddd
>>> print(ddd) ['age'] 21
{'course': 182, 'age': 23}
Dictionary Literals (Constants)

• Dictionary literals use curly braces and have a list of


key : value pairs
• You can make an empty dictionary using empty curly
braces

>>> jjj = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}


>>> print(jjj)
{'jan': 100, 'chuck': 1, 'fred': 42}
>>> ooo = { }
>>> print(ooo)
{}
>>>
Most Common Name?

marquard cwen cwen


zhen marquard zhen
csev
csev zhen
marquard
zhen csev zhen
Most Common Name?

marquard cwen cwen


zhen marquard zhen
csev
csev zhen
marquard
zhen csev zhen
Many Counters with a Dictionary

Key Value
One common use of dictionaries is
counting how often we “see” something
>>> ccc = dict()
>>> ccc['csev'] = 1
>>> ccc['cwen'] = 1
>>> print(ccc)
{'csev': 1, 'cwen': 1}
>>> ccc['cwen'] = ccc['cwen'] + 1
>>> print(ccc)
{'csev': 1, 'cwen': 2}
Dictionary Tracebacks

It is an error to reference a key which is not in the dictionary

We can use the in operator to see if a key is in the dictionary

ccc = dict()
>>> print(ccc['csev'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'csev'
>>> 'csev' in ccc
False
When We See a New Name

When we encounter a new name, we need to add a new


entry in the dictionary and if this the second or later time
we have seen the name, we simply add one to the count
in the dictionary under that name

counts = dict()
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']
for name in names :
if name not in counts: {'csev': 2, 'zqian': 1, 'cwen': 2}
counts[name] = 1
else :
counts[name] = counts[name] + 1
print(counts)
The get Method for Dictionaries

The pattern of checking to see if a if name in counts:


key is already in a dictionary and x = counts[name]
assuming a default value if the key else :
is not there is so common that there x = 0
is a method called get() that does
this for us
x = counts.get(name, 0)

Default value if key does not exist


(and no Traceback). {'csev': 2, 'zqian': 1, 'cwen': 2}
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}


Simplified Counting with get()

counts = dict()
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']
for name in names :
counts[name] = counts.get(name, 0) + 1
print(counts)
Counting Pattern
counts = dict()
print('Enter a line of text:')
The general pattern to count the
line = input('')
words in a line of text is to split
words = line.split() the line into words, then loop
through the words and use a
print('Words:', words) dictionary to track the count of
print('Counting...') each word independently.
for word in words:
counts[word] = counts.get(word,0) + 1
print('Counts', counts)
python wordcount.py
Enter a line of text:
the clown ran after the car and the car ran into the tent
and the tent fell down on the clown and the car

Words: ['the', 'clown', 'ran', 'after', 'the', 'car',


'and', 'the', 'car', 'ran', 'into', 'the', 'tent', 'and',
'the', 'tent', 'fell', 'down', 'on', 'the', 'clown',
'and', 'the', 'car']
Counting…

Counts {'and': 3, 'on': 1, 'ran': 2, 'car': 3, 'into': 1,


'after': 1, 'clown': 2, 'down': 1, 'fell': 1, 'the': 7,
'tent': 2}

http://www.flickr.com/photos/71502646@N00/2526007974/
python wordcount.py
counts = dict() Enter a line of text:
line = input('Enter a line of text:') the clown ran after the car and the car ran
words = line.split()
into the tent and the tent fell down on the
print('Words:', words) clown and the car
print('Counting...’)
Words: ['the', 'clown', 'ran', 'after', 'the', 'car',
for word in words: 'and', 'the', 'car', 'ran', 'into', 'the', 'tent', 'and',
counts[word] = counts.get(word,0) + 1 'the', 'tent', 'fell', 'down', 'on', 'the', 'clown',
print('Counts', counts)
'and', 'the', 'car']
Counting...

Counts {'and': 3, 'on': 1, 'ran': 2, 'car': 3,


'into': 1, 'after': 1, 'clown': 2, 'down': 1, 'fell':
1, 'the': 7, 'tent': 2}
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' : 1 , 'fred' : 42, 'jan': 100}
>>> for key in counts:
... print(key, counts[key])
...
jan 100
chuck 1
fred 42
>>>
Retrieving Lists of Keys and Values

>>> jjj = { 'chuck' : 1 , 'fred' :


42, 'jan': 100}
>>> print(list(jjj))
You can get a list ['jan', 'chuck', 'fred']
of keys, values, or >>> print(jjj.keys())
items (both) from ['jan', 'chuck', 'fred']
a dictionary >>> print(jjj.values())
[100, 1, 42]
>>> print(jjj.items())
[('jan', 100), ('chuck', 1),
('fred', 42)]
What is a “tuple”? - coming soon...
>>>
Bonus: Two Iteration Variables!

• We loop through the


key-value pairs in a jjj = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}
for aaa,bbb in jjj.items() :
dictionary using *two* print(aaa, bbb)
iteration variables aaa bbb

• Each iteration, the first jan 100 [jan] 100


chuck 1
variable is the key and fred 42 [chuck] 1
the second variable is
the corresponding [fred] 42
value for the key
name = input('Enter file:')
handle = open(name)
python words.py
counts = dict() Enter file: words.txt
for line in handle:
words = line.split()
to 16
for word in words:
counts[word] = counts.get(word,0) + 1

bigcount = None
bigword = None
python words.py
for word,count in counts.items(): Enter file: clown.txt
if bigcount is None or count > bigcount: the 7
bigword = word
bigcount = count

print(bigword, bigcount)
Using two nested loops
Dictionaries
>>>
>>> purse = dict()
>>>purse['money'] = 12
• Lists index their entries >>> purse['candy'] = 3
based on the position >>> purse['tissues'] = 75
in the list >>> print(purse)
{'money': 12, 'tissues':
• Dictionaries are like 75, 'candy': 3}
>>> print(purse['candy'])
bags - no order 3
>>> purse['candy'] =
• So we index the things purse['candy'] + 2
we put in the dictionary >>> print(purse)
with a “lookup tag” {'money': 12, 'tissues':
75, 'candy': 5}

10-08-2022 99
Comparing Lists and
Dictionaries
Dictionaries are like lists except that they use keys instead of
numbers to look up values
>>> lst = >>> ddd = dict()
list() >>> ddd['age'] = 21
>>> >>> ddd['course'] =
lst.append(21) 182
>>> >>> print(ddd)
lst.append(183) {'course': 182,
>>> print(lst) 'age': 21}
[21, 183] >>> ddd['age'] = 23
>>> lst[0] = 23 >>> print(ddd)
>>> print(lst) {'course': 182,
[23, 183] 'age': 23}
10-08-2022 100

You might also like