





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Introduction to Python Programming
Typology: Lecture notes
1 / 29
This page cannot be seen from the preview
Don't miss anything!






















Comparing Lists and Dictionaries Dictionaries are like lists except that they use keys instead of numbers to look up values
lst = list() lst.append( 21 ) lst.append( 183 ) print(lst) [21, 183] lst[ 0 ] = 23 print(lst) [23, 183] ddd = dict() ddd['age'] = 21 ddd['course'] = 182 print(ddd) {'course': 182 , 'age': 21 } ddd['age'] = 23 print(ddd) {'course': 182 , 'age': 23 }
lst = list() lst.append( 21 ) lst.append( 183 ) print(lst) [21, 183] lst[0] = 23 print(lst) [23, 183] ddd = dict() ddd['age'] = 21 ddd['course'] = 182 print(ddd) {'course': 182 , 'age': 21 } ddd['age'] = 23 print(ddd) {'course': 182 , 'age': 23 }
Many Counters with a Dictionary One common use of dictionaries is counting how often we “see” something Key Value
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 }
ccc = dict() print(ccc['csev']) Traceback (most recent call last): File "
", line 1, in KeyError: 'csev' 'csev' in ccc False
The pattern of checking to see if a key is already in a dictionary and assuming a default value if the key is not there is so common that there is a method called get() that does this for us if name in counts: x = counts[name] else : x = 0 x = counts.get(name, 0 ) Default value if key does not exist (and no Traceback).
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
Writing programs (or programming) is a very creative and rewarding activity. You can write programs for many reasons ranging from making your living to solving a difficult data analysis problem to having fun to helping someone else solve a problem. This book assumes that everyone needs to know how to program and that once you know how to program, you will figure out what you want to do with your newfound skills. We are surrounded in our daily lives with computers ranging from laptops to cell phones. We can think of these computers as our “personal assistants” who can take care of many things on our behalf. The hardware in our current-day computers is essentially built to continuously ask us the question, “What would you like me to do next?” Our computers are fast and have vast amounts of memory and could be very helpful to us if we only knew the language to speak to explain to the computer what we would like it to do next. If we knew this language we could tell the computer to do tasks on our behalf that were repetitive. Interestingly, the kinds of things computers can do best are often the kinds of things that we humans find boring and mind-numbing.