🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

1.1 Practical Exercise 1: Unit 1 – Lists Tuples Dictionaries Sets dir() function Exercise 1.1.1: The dir() function is a powerful built-in function in Python3, which returns list of the attributes and methods of any object (example: functions , modules, strings, lists, dictionaries etc.) We will us...

1.1 Practical Exercise 1: Unit 1 – Lists Tuples Dictionaries Sets dir() function Exercise 1.1.1: The dir() function is a powerful built-in function in Python3, which returns list of the attributes and methods of any object (example: functions , modules, strings, lists, dictionaries etc.) We will use the dir() function to remind ourselves which methods we have access to for Lists, Tuples, Dictionaries and Sets. Syntax : dir({object}) Parameters: object [optional]: Takes an object name Copy and paste the following code in a Repl called Prac_Ex1_Unit1. Run the code and inspect the output. print() print("List methods:") print() #List l = [] print(dir(l)) #OR l = list() print(dir(l)) #Methods returned:['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] print() print("Dictionary methods:") print() #Tuple t = () print(dir(t)) #OR t = tuple() print(dir(t)) #Methods returned: ['count', 'index'] print() print("Dictionary methods:") print() print() #Dictionary d = {} print(dir(d)) #OR d = dict() print(dir(d)) #Methods returned:'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] print() print("Set methods:") print() #Dictionary s = {} print(dir(s)) #OR s = set() print(dir(s)) #Methods returned: ['clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] 1.1 Practical Exercise 1: Unit 1 – Lists Tuples Dictionaries Sets Summary of methods returned in the console: l = [] t = () d= {} s= {} print(dir(l)) print(dir(t)) print(dir(d)) print(dir(s)) ['append', 'clear', ['clear', 'copy', ['clear', 'copy', 'copy', 'count', 'fromkeys', 'get', 'fromkeys', 'get', 'extend', 'index', 'items', 'keys', 'items', 'keys', ['count', 'index'] 'insert', 'pop', 'pop', 'popitem', 'pop', 'popitem', 'remove', 'reverse', 'setdefault', 'setdefault', 'sort'] 'update', 'values'] 'update', 'values'] Lists[] Exercise 1.2.1: append() Write a Python program that takes user input for a list of integers and repeatedly adds elements to the list using the 'append' method until the user enters 'done'. Once the user is done adding elements, print the final list. my_list = [] while True: element = input("Enter an integer to add to the list (type 'done' to finish): ") if element.lower() == 'done': break my_list.append(int(element)) print("Final list:", my_list) Exercise 1.2.2: clear() Write a Python program that takes a list of integers as input and uses the 'clear' method to remove all the elements from the list. After clearing the list, print the empty list. sample_list = [1, 2, 3, 4, 5] sample_list.clear() print("Empty list:", sample_list) Exercise 1.2.3: copy() Write a Python program that takes a list of strings as input and creates a new list by using the 'copy' method. Modify the original list by adding a new element, and then print both the original and the copied list to see if the copied list is affected by the modification. sample_list = ["apple", "banana", "cherry"] copied_list = sample_list.copy() sample_list.append("New Element") print("Original List:", sample_list) print("Copied List:", copied_list) Exercise 1.2.4: count() Write a Python program that takes a list of characters as input and asks the user to enter a character to search for. Use the 'count' method to find and print the number of occurrences of that character in the list. sample_list = ['a', 'b', 'a', 'c', 'd', 'a'] target = input("Enter a character to search for: ") count = sample_list.count(target) print(f"Number of occurrences of '{target}': {count}") 1.1 Practical Exercise 1: Unit 1 – Lists Tuples Dictionaries Sets Exercise 1.2.5: extend() Write a Python program that takes two lists of integers as input from the user and uses the 'extend' method to combine the elements of the second list into the first one. Finally, print the updated first list. list1 = [1, 2, 3] list2 = [4, 5, 6] list1.extend(list2) print("Updated first list:", list1) # Test the function append_elements() Exercise 1.2.6: index() Write a Python program that takes a list of strings as input and asks the user to enter a string to search for. Use the 'index' method to find and print the index of the first occurrence of that string in the list. If the string is not found, handle the exception and print an appropriate message. sample_list = ['apple', 'banana', 'orange', 'banana'] target = input("Enter a string to search for: ") try: index = sample_list.index(target) print(f"The first occurrence of '{target}' is at index {index}.") except ValueError: print(f"'{target}' is not found in the list.") Exercise 1.2.7: insert() Write a Python program that takes a list of integers as input and asks the user to enter a number to insert and the index where the number should be inserted. Use the 'insert' method to insert the number at the specified index, and then print the updated list. sample_list = [10, 20, 30, 40] num = int(input("Enter a number to insert: ")) idx = int(input("Enter the index to insert the number: ")) sample_list.insert(idx, num) print("Updated list:", sample_list) Exercise 1.2.8: pop() Write a Python program that takes a list of strings as input and repeatedly asks the user to enter an index to remove an element using the 'pop' method. Keep removing elements until the list is empty. After each removal, print the updated list. sample_list = ['apple', 'banana', 'orange'] while sample_list: print("Current list:", sample_list) index = int(input("Enter the index to remove (0 to exit): ")) if index == 0: break if 0

Use Quizgecko on...
Browser
Browser