Python List and Dictionary Methods Practice
Python List and Dictionary Methods Practice
To modify the list of fruits, the following Python list methods are necessary: 'append()' to add 'grape' to the end of the list, 'insert()' to add 'mango' at position 2, and 'remove()' to delete 'banana' from the list. Finally, 'print()' is used to display the final list .
Implementing a number guessing game uses a 'while' loop to repeatedly ask users for input until a correct guess is made. Track player guesses and provide feedback on whether guesses are high or low. Initialize a secret number and increment a guess counter with each iteration until correctly guessed, providing final feedback on guesses taken .
Extract sublists using slicing, e.g., 'letters[:3]' obtains the first three elements, while 'letters[-2:]' the last two. Use 'index()' to find the position of an item like 'c'. Replace elements by directly assigning a new value to a specific index, e.g., 'letters[3] = 'z'', to replace 'd' .
Designing a discount calculator involves using 'if' statements to apply percentage-based discounts based on conditions: purchase amount thresholds, loyalty status, day (weekend), or holiday season triggers additional discounts. Calculate cumulative adjustments and use variables to track original and final prices, displaying the result via 'print()' for each discount applied .
Use 'for' loops in Python to generate number patterns. For counting numbers in a line, loop from 1 to 10 using 'range()'. To create a multiplication table for a number, iterate through 'range()' outputs and multiply each by the base number, displaying results with 'print()'. Each iteration showcases continuous patterns based on loop logic .
In a Python dictionary, use 'update()' to modify an existing key value, such as changing 'age' to 21 in the student dictionary. To add a new key-value pair, such as 'email', use direct assignment like 'dictionary[key] = value'. Access values by key, like using 'dictionary[key]', e.g., 'student['name']', prints a name. 'print()' displays updated dictionaries .
For a word frequency dictionary, use 'keys()' to extract all keys, 'values()' to obtain values, and 'items()' for key-value pairs. To duplicate the dictionary, use 'copy()'. Remove all entries with 'clear()', then verify emptiness by checking the length as zero. Contrast with the copied dictionary to ensure data integrity .
To combine two lists in Python, use the '+' operator; for example, combine 'numbers' and 'more_numbers' to create 'all_numbers'. To create a reversed copy of 'all_numbers', first make a copy using the 'copy()' method, then reverse it with 'reverse()'. Finally, use 'print()' to display both 'all_numbers' and the reversed 'numbers_copy' .
To sort a list in Python, use the 'sort()' method with default parameters for ascending order or with 'reverse=True' for descending order. To count a specific element, say '92', use the 'count()' method. For example, applying these methods to 'scores' helps identify the highest and lowest values using 'min()' and 'max()' functions .
Create a list where each element is a dictionary representing a student with 'name', 'id', and 'grades' fields. 'grades' itself is a dictionary with courses as keys and grades as values. Add multiple students and their data, then iterate over this structure to calculate averages and find students based on various conditions, such as those with an average above 80 or the highest average grade .