Python Data Structures Lecture Notes PDF
Document Details
Uploaded by EliteCarnelian3565
Halmstad University
2024
Tags
Summary
This document is a lecture on Python data structures, focusing on tuples and lists. It covers fundamental concepts, operations, and methods.
Full Transcript
Python a Gateway to Machine Learning (DT8051) Lecture 3: Python Data Structures Halmstad University September 2024 In this Lecture Structured Types More about strings Mutability Data Struct...
Python a Gateway to Machine Learning (DT8051) Lecture 3: Python Data Structures Halmstad University September 2024 In this Lecture Structured Types More about strings Mutability Data Structures in Python Sequence types: String Range Tuple List Set Dictionary Tuples A tuple is a sequence of object elements (elem1, elem2, elem3,..., elemN) To make a tuple with one element you need a comma after the element (elem,) In : a = (1,2,3,4) b = (1,) #tuple with only one item c = (1) #not a tuple, just the number 1 d = () print(a) print(b) print(c) print(d) (1, 2, 3, 4) (1,) 1 () Tuples You can access an manipulate tuples similar to strings In : a = ("a","b","c","d") print(a[2:4]) print(a*2) b = (a,1,2,3) print(b) print(a+b) ('c', 'd') ('a', 'b', 'c', 'd', 'a', 'b', 'c', 'd') (('a', 'b', 'c', 'd'), 1, 2, 3) ('a', 'b', 'c', 'd', ('a', 'b', 'c', 'd'), 1, 2, 3) Tuples Tuples are a Python sequence type Sequence types can be iterated in a for loop Sequences can be indexed and sliced similar to strings In : a = ("c","d","e","f") for c in a: print(c) print("second element in the tupe:", a) # remember indices start at 0 c d e f second element in the tupe: d Tuples Tuples are immutable: you cannot change an element in a tuple In : a = ("c","d","e","f") #a = 1 a = "a" -------------------------------------------------------------------------- - TypeError Traceback (most recent call las t) Cell In, line 3 1 a = ("c","d","e","f") 2 #a = 1 ----> 3 a = "a" TypeError: 'tuple' object does not support item assignment You can compare tuples with each other In : a = (1,2,3,4) print(a == (1,2,4,3)) print(a == (1,2)) print(a == (1,2,3,4)) False False True Lists [elem1, elem2, elem3,..., elemN] Lists are similar to tuples, however they are mutable In : a = [] b = [1,2,3,4] print(b) b = a print(b) [1, 2, 3, 4] [[], 2, 3, 4] Lists are also Python sequences, you can use them in for loops and index/slice them In : a = ["a","b","c","d"] a[1:3] Out: ['b', 'c'] Negative index and step in Pyton In python you can have negative indices. -1 is the index of the last element, -2 the element before the last one, and so on. In : a = ["a","b","c","d","e"] print(a[-1]) print(a[1:-2]) e ['b', 'c'] When slicing sequence, or making a range you can have negative steps In : a = ["a","b","c","d"] print(a[::-1]) print(a[::-2]) ['d', 'c', 'b', 'a'] ['d', 'b'] Assignment by Reference In Python all assignments are by reference Be careful with mutable objects that are assigned by reference: In : a = ["a","b","c","d"] b = a a=1 print(b) [1, 'b', 'c', 'd'] Also avoid using mutable objects as default arguments of functions To solve this problem you can use the copy() method In : a = ["a","b","c","d"] b = a.copy() a=1 print(b) ['a', 'b', 'c', 'd'] List Methods Here are some of the most useful methods of lists: append(element) : adds an element to the end of the list insert(index,element) : puts an element in the given index position of the list remove(element) : removes an element from the list index(element) : give the index location of the input element in the list pop(index) : removes and returns an element from the given index position of the list pop() : removes and returns the last element of the list (index=-1) reverse() : reverses the order of the elements in the list sort() : sorts the elements in the list in increasing order List Methods Example: In : l = [1,2,3,4] print(l) l.append(0) l.insert(1, 5) l.remove(3) print(l) print(f"index of 4 is:{l.index(4)}") print(f"now {l.pop(0)} is popped from the list: {l}") print(f"now {l.pop()} is popped from the list: {l}") l.reverse() print(f"after reversing we have: {l}") l.sort() print(f"after sorting we get: {l}") [1, 2, 3, 4] [1, 5, 2, 4, 0] index of 4 is:3 now 1 is popped from the list: [5, 2, 4, 0] now 0 is popped from the list: [5, 2, 4] after reversing we have: [4, 2, 5] after sorting we get: [2, 4, 5] List Comprehension You can also create a list with list comprehension [ expr for variable in sequence] [ expr for variable in sequence if condition] List comprehension is mainly to shorten the program For example you want to make a list of all odd numbers between 1 and 100: In [157… l = [] for i in range(100): if i%2 != 0: l.append(i) print(l) [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 3 9, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99] List Comprehension Example: list of all odd numbers between 1 and 100 In : l = [i for i in range(10,100) if i%2 != 0] print(l) [11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 4 7, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99] Another correct answer: In [163… l = [i for i in range(1,100,2)] print(l) [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 3 9, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99] Common Sequence Operations Seuences in Python include tuples, lists, ranges, and strings. All sequences have a common set of operations 1. seq[i]: retrieves the elemnt at index position i 2. seq1 + seq2: concatenates seq1 with seq2 in the same order (not for ranges) 3. n* seq: creates a new sequences that the repetition of seq for n times (not for ranges) 3. len(seq): returns the number of elements in the sequence 4. seq[start:end] or seq[start:end:step]: returns a slice of the sequence 5. a in seq: returns True if element a exists in seq, returns False otherwise 6. a not in seq: returns True if element a does not exists in seq, returns False otherwise 7. for a in seq: assigns a to the elements of seq iteratively Common Sequence Operations Example: In [120… a = [1,2,3,4,5] print(f"a: {a}") b = ["a","b","c","d"] print(f"b: {b}") print(f"a+b: {a+b}") print(f"len(a): {len(a)}") print(f"a[2:5:2]: {a[2:5:2]}") if 6 in a: print("6 is in a.") else: print("6 is not in a.") if "d" not in b: print("The character d is not in b.") else: print("The character d is in b.") a: [1, 2, 3, 4, 5] b: ['a', 'b', 'c', 'd'] a+b: [1, 2, 3, 4, 5, 'a', 'b', 'c', 'd'] len(a): 5 a[2:5:2]: [3, 5] 6 is not in a. The character d is in b. Common Sequence Operations When in is used with a string inside an if statement's condition it works slightly differently from other sequence types You can check if any string is contained within any other string using the in operator In : if "wed" in "Sweden": print(f"'wed' is in 'Sweden'") else: print(f"'wed' is not in 'Sweden'") if (2,3,4) in (1,2,3,4,5): print(f"(2,3,4) is in (1,2,3,4,5)") else: print(f"(2,3,4) is not in (1,2,3,4,5)") 'wed' is in 'Sweden' (2,3,4) is not in (1,2,3,4,5) Common Sequence Operations Examples on iteration: In [128… a = range(2) b = range(3) for e1 in a: for e2 in b: print(e1*len(b)+e2) 0 1 2 3 4 5 In [129… l = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunda for day in l: print(day) Monday Tuesday Wednesday Thursday Friday Saturday Sunday Higher order operations on sequences (mapping) When you want to apply the same operation on all elements of a sequence In [169… def apply_operation(operation, seq): l = [] for e in seq: l.append(operation(e)) return l def square(num): return num*num apply_operation(square, [1,2,3]) Out[169… [1, 4, 9] In [171… for e in map(square,[1,2,3]): print (e) 1 4 9 In [183… list(map(lambda x: x*x, [1,2,3])) Out[183… [1, 4, 9] Some string methods s1.count(s2): returns the number of times s2 occurs in s1 s1.find(s2): returns the index location of the first occurance of s2 in s1 s1.rfind(s2): the same as find but returns the last occurance s1.lower(): returns a new string with all characters in s1 as lower case s1.replace(s2,s3): replaces all occurances of s2 with s3 in s1 s1.rstrip(): removes white spaces from the end of the s1 s1.split(d): splits s1 to a list of new string using the separator d Some string methods Examples: In [204… s = "John Smith " print(f"The string: '{s}'") print(f"Number of h occurances: {s.count('h')}") print(f"First occurance of h: index {s.find('h')}") print(f"Last occurance of h: index {s.rfind('h')}") print(f"Lowercased string: '{s.lower()}'") print(f"The surname replaced: '{s.replace('Smith','Jackson')}'") s = s.rstrip() print(f"After removing the white space at the end: '{s}'") print(f"Splitted by space characters: {s.split(' ')}") The string: 'John Smith ' Number of h occurances: 2 First occurance of h: index 2 Last occurance of h: index 9 Lowercased string: 'john smith ' The surname replaced: 'John Jackson ' After removing the white space at the end: 'John Smith' Splitted by space characters: ['John', 'Smith'] About quotation marks and strings You can use both single quote (') and double quote (") to create strings When you want to use single or double quotes within a string you should use the opposite type to define the string: In [205… print("These are 'single quotes' within a double quote string.") print('These are "double quotes" within a single quote string.') These are 'single quotes' within a double quote string. These are "double quotes" within a single quote string. You can also use three single or double quotes for strings that span multiple lines: In : s1 = """Line 1 Line 2 """ s2 = '''Muhammad Ali John Smith ''' print(s1+s2) Line 1 Line 2 Muhammad Ali John Smith Escape characters in Python Within strings you can use some special characters called escape characters Escape characters are denoted by puting a backslash () before them Here are a few useful characters: \n : Jumps to a new line \t : Puts a tab character (used for aligning words) \r : Goes back to the begining of a line \' : Puts a single quote \" : Puts a double quote \\ : Puts a backslash character Escape characters in Python Example: In : # The tabs characters allow for printing a table print("\"First Name\" \t \"Last Name\"") #Inserting double quotes within a doub print("Muhammad \t Ali \nJohn \t\t Smith") #In the same string we have two line print() print("Here is a backslash character: \\") print() print("*******************", end="\r") #changes the end of the line from \n to print("##########") "First Name" "Last Name" Muhammad Ali John Smith Here is a backslash character: \ *******************########## Sets In Python set is a type that keeps a collection of elements in it {elem1, elem2,..., elemN} set() creates an empty set Set has no order therefore it is not a sequence In [217… a = {1, 3.14, "Some String"} a.add(False) print(a) a.remove(3.14) print(a) {False, 1, 3.14, 'Some String'} {False, 1, 'Some String'} Out[217… {} Sets Sets only accept hashable objects as elements (e.g. Lists are not hashable) Hashable object have the __hash__ and __eq__ methods defined for them Some useful set methods: set1.union(set2): returns the union of set1 and set2. You can also write: set1 | set2 set1.intersection(set2): extracts the intersection of the two sets. You can also write: set1 & set2 set1.difference(set2): returns the complement of set2 with respect to set1. You can also write: set1 - set2 set1.issubset(set2): checks if set1 is a subset of set2. Also can be written as: set1