Python Data Types Lecture Notes PDF
Document Details
Uploaded by Ameera
University of Sharjah
Dr. Ala Altaweel
Tags
Summary
These lecture notes cover data types in Python, focusing on Lists and Dictionaries. The notes include examples and operations for each data type.
Full Transcript
Network Programming: Data Types in Python Dr. Ala Altaweel University of Sharjah Department of Computer Engineering [email protected] 1 Agenda Introduction Running Python Python Programming Data types Cont...
Network Programming: Data Types in Python Dr. Ala Altaweel University of Sharjah Department of Computer Engineering [email protected] 1 Agenda Introduction Running Python Python Programming Data types Control flows Classes, functions, modules Hands‐on Exercises 2 Compound Data Type: List List: A container that holds a number of other objects, in a given order Defined in square brackets a = [1, 2, 3, 4, 5] print a # number 2 some_list = [] some_list.append("foo") some_list.append(12) print len(some_list) # 2 3 List a = [99, "bottles of water ", ["on", "the", "wall"]] Flexible arrays, not like linked lists Same operators as for strings a+b, a*3, a, a[‐1], a[1:], len(a) Item and slice assignment a = 98 a[1:2] = ["bottles", "of", “water"] ‐> [98, ["bottles", "of", “water“], ["on", "the", "wall"]] del a[‐1] a Above line will print: [98, "bottles", "of", “water"] 4 More list operations >>> a = range(5) # [0,1,2,3,4] >>> a.append(5) # [0,1,2,3,4,5] >>> a.pop() # [0,1,2,3,4] 5 >>> a.insert(0, 5.5) # [5.5,0,1,2,3,4] >>> a.pop(0) # [0,1,2,3,4] 5.5 >>> a.reverse() # [4,3,2,1,0] >>> a.sort() # [0,1,2,3,4] 5 Operations in List append Indexing e.g., L[i] insert Slicing e.g., L[1:5] index Concatenation e.g., L + L count Repetition e.g., L * 5 sort Membership test e.g., ‘a’ in L reverse Length e.g., len(L) remove pop extend 6 Methods in list remove() Takes a single element as an argument and removes it from the list. If the element doesn't exist, it throws ValueError: list.remove(x): x not in list exception. The remove() function doesn't return any value. a = [1, 2, 3, 4, 5] a.remove(2) a a.remove(100) a Above code will print [1,3,4,5] then it will throw ValueError Exception 7 Methods in list pop() Takes a single argument (index). The argument passed to the method is optional. If not passed, the default index ‐1 is passed as an argument (index of the last item). If the index passed to the method is not in range, it throws IndexError: pop index out of range exception. Returns the item present at the given index. This item is also removed from the list. a = [1, 2, 3, 4, 5] a.pop() a a.pop(100) a Above code will print 5, then [1,2,3,4] then, it will throw IndexError Exception, then [1,2,3,4] 8 Nested List List in a list E.g., s = [1,2,3] t = ["begin", s, "end"] t t Above code will print ["begin", [1, 2, 3], "end"] 2 What if we add the below code, what will it print? Homework u = [t, 2,t] u 9 Dictionaries Dictionaries: curly brackets What is dictionary? A collection which is unordered, changeable and does not allow duplicates. Dictionaries are used to store data values in key:value pairs. Dictionaries are written with curly brackets, and have keys and values: Refer value through key; “associative arrays” Like an array indexed by a string An unordered set of key: value pairs Values of any type; keys of almost any type 10 Dictionary example thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(thisdict) Above code will print {‘brand’ : ‘ford’, ‘model’: ‘ Mustang’, ‘year’: 1964} 11 Dictionary items Dictionary items are unordered, changeable, and does not allow duplicates. Dictionary items are presented in key:value pairs, and can be referred to by using the key name. thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(thisdict[“brand”]) Above code will print Ford 12 Dictionary items Dictionary items are changeable, meaning that we can change, add or remove items after the dictionary has been created. Dictionaries cannot have two items with the same key: thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 "year": 2020 } print(thisdict) Above code will print {‘brand’ : ‘ford’, ‘model’: ‘ Mustang’, ‘year’: 2020} 13 Methods in Dictionary clear() copy() get(key) items() keys() values() pop() setdefault(key,value) update(key,value) has_key(key) 14 Dictionary methods clear(): Removes all the elements from the dictionary thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict.clear() print(thisdict) Above code will print {} 15 Dictionary methods copy(): returns a copy of the specified dictionary thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } x= thisdict.copy() print (x) Above code will print {'brand': 'Ford', 'model': 'Mustang', 'year': 1964} Homework: Change x, and then print thisdict 16 Dictionary methods get(): returns the value of the item with the specified key thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } x= thisdict.get("model") print (x) Above code will print Mustang 17 Dictionary methods items(): returns a list containg a tuple of each key-value pair thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } x= thisdict.items() print (x) Above code will print [('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)] 18 Dictionary methods keys(): returns a list containg the dictionary’s keys thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } x= thisdict.keys() print (x) Above code will print ['brand', 'model', 'year'] 19 Dictionary methods values(): returns a list of all the values in the dictionary thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } x= thisdict.values() print (x) Above code will print ['Ford', 'Mustang', 1964] 20 Dictionary methods pop(): removes element with a specified key thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict.pop("model") print (thisdict) Above code will print {'brand': 'Ford', 'year': 1964} 21 Dictionary methods setdefault(): returns the value of the specified key. If the key does not exist: insert the key, with the specified value thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } x = thisdict.setdefault("model", "Toyota") print (x) print(thisdict) Above code will print Mustang {'brand': 'Ford', 'model': 'Mustang', 'year': 1964} 22 Dictionary methods setdefault(), another example thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } x = thisdict.setdefault("country", "UAE") print (x) print(thisdict) Above code will print UAE {'country': 'UAE', 'brand': 'Ford', 'model': 'Mustang', 'year': 1964} 23 Dictionary methods update(): update the dictionary with the specified key- value pairs thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict.update({"year" : 1990}) print(thisdict) Above code will print {'brand': 'Ford', 'model': 'Mustang', 'year': 1990} 24 Dictionary methods has_key(): returns true if a given key is available in the dictionary, otherwise it returns a false. thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print (thisdict.has_key("brand")) print(thisdict.has_key("country")) print(thisdict.has_key("Ford")) Above code will print True False 25 False