0% found this document useful (0 votes)
20 views7 pages

Python List and Dictionary Methods Practice

The document outlines multiple Python programming assignments focusing on list and dictionary methods, a shopping discount calculator, number patterns using loops, a number guessing game, and a student grade management system. Each section provides specific tasks to complete, emphasizing the manipulation of data structures and control flow in Python. The assignments aim to enhance understanding of fundamental programming concepts.

Uploaded by

nawazibrahim453
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

Python List and Dictionary Methods Practice

The document outlines multiple Python programming assignments focusing on list and dictionary methods, a shopping discount calculator, number patterns using loops, a number guessing game, and a student grade management system. Each section provides specific tasks to complete, emphasizing the manipulation of data structures and control flow in Python. The assignments aim to enhance understanding of fundamental programming concepts.

Uploaded by

nawazibrahim453
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

List Methods Practice

Assignment Overview:

In this assignment, you will practice using different list methods in Python. Lists are a
fundamental data structure, and understanding how to manipulate them is essential for
Python programming.

Instructions:

Complete the following tasks using Python list methods. Write your code in a single
Python file.

Tasks:

1. Create and Modify Lists


o Create a list called fruits with the items: "apple", "banana", "orange"
o Add "grape" to the end of the list using an appropriate method
o Insert "mango" at position 2 in the list
o Remove "banana" from the list
o Print the final list
2. List Operations
o Create a list numbers with values: 10, 20, 30, 40, 50
o Create a second list more_numbers with values: 60, 70, 80
o Combine both lists into a new list called all_numbers
o Make a copy of all_numbers called numbers_copy
o Reverse the order of numbers_copy
o Print both all_numbers and numbers_copy
3. Sorting and Counting
o Create a list scores with these values: 85, 92, 78, 65, 92, 85, 74
o Sort the list in ascending order
o Sort the list in descending order
o Count how many times 92 appears in the list
o Find and print the highest and lowest scores
o Print the sorted list
4. List Manipulation
o Create a list letters with values: "a", "b", "c", "d", "e"
o Use slicing to create a new list with only the first three letters
o Use slicing to create another list with only the last two letters
o Find and print the index of "c" in the list
o Replace "d" with "z" in the original list
o Print the modified list

Dictionary Methods Practice

Assignment Overview:

In this assignment, you will practice using different dictionary methods in Python.
Dictionaries are essential data structures that store key-value pairs and are widely used
in Python programming.

Instructions:

Complete the following tasks using Python dictionary methods. Write your code in a
single Python file.

Tasks:

1. Create and Access Dictionaries


o Create a dictionary called student with these key-value pairs:
 "name": "John"
 "age": 20
 "grade": "A"
 "courses": ["Math", "Science", "History"]
o Print the student's name
o Print the list of courses
o Add a new key-value pair: "email": "john@[Link]"
o Print the entire dictionary
2. Modify Dictionary Values
o Change the student's age to 21
o Add a new course "Computer Science" to the courses list
o Update the grade to "A+"
o Print the modified dictionary
3. Dictionary Operations
o Create a second dictionary student_address with these key-value pairs:
 "street": "123 College Ave"
 "city": "New York"
 "zip": "10001"
o Combine both dictionaries into a new dictionary called student_info
o Remove the "zip" key from student_info
o Check if "phone" exists in the dictionary
o Print the final dictionary
4. Dictionary Methods
o Create a dictionary word_count with these key-value pairs:
 "hello": 5
 "world": 10
 "python": 15
o Get all keys and print them as a list
o Get all values and print them as a list
o Get all key-value pairs and print them
o Make a copy of the dictionary
o Clear the original dictionary
o Print both dictionaries to verify one is empty and one is a copy
Shopping Discount Calculator

Assignment Overview

In this assignment, you will create a simple shopping discount calculator that
determines discounts based on purchase amount, customer loyalty status, and
seasonal promotions. This program will use if statements to make decisions similar to
those in real-world retail systems.

Instructions

Create a Python program that calculates the final price after applicable discounts. Do
not use any functions - just use variables and if statements.

Tasks

1. Set up the initial variables:


o purchase_amount: The total cost of items in the shopping cart
o is_loyalty_member: Boolean (True/False) indicating if the customer is a
loyalty member
o day_of_week: A string with the current day (e.g., "Monday", "Tuesday", etc.)
o is_holiday_season: Boolean indicating if it's currently a holiday shopping
season
2. Apply discount rules using if statements:
o If purchase amount is greater than $100, apply a 10% discount
o If purchase amount is greater than $50 (but less than or equal to $100),
apply a 5% discount
o If the customer is a loyalty member, apply an additional 5% discount
o If it's a weekend (Saturday or Sunday), apply an additional 2% discount
o If it's a holiday season, apply an additional 8% discount
3. Calculate and display:
o The original purchase amount
o Each discount that was applied and its amount
o The final price after all discounts

For Loop Assignment - Number Patterns

Assignment Overview:

In this assignment, you will create number patterns using for loops in Python.

Instructions:

Write a Python program that generates the following patterns using for loops:

1. Pattern 1: Counting Numbers


o Print numbers from 1 to 10 on a single line.

Pattern 2: Multiplication Table

 Create a multiplication table for the number 5 (from 1 to 10).

While Loop Assignment - Interactive Game

Assignment Overview:

Create a simple number guessing game using a while loop.

Instructions:
Write a Python program that:

1. Sets a "secret number" (you can choose any number between 1 and 50)
2. Asks the player to guess the number
3. Tells the player if their guess is too high or too low
4. Keeps asking until the player guesses correctly
5. Keeps track of how many guesses it took

Student Grade Management System

Assignment Overview:

In this assignment, you will create a simple student grade management system using
Python lists and dictionaries. This will help you understand how these data structures
work together to organize and manage information.

Requirements:

Create a Python program that does the following:

1. Create a list to store information about students


2. Each student should be represented as a dictionary with the following keys:
o name: The student's full name
o id: A unique student ID number
o grades: Another dictionary containing course names as keys and grades as
values
3. Your program should:
o Add at least 3 students to the list
o Add grades for at least 3 courses for each student
o Display all students with their grades
o Calculate and display the average grade for each student
o Find and display the student with the highest average grade
o List all students with an average grade above 80
o Calculate and display the average grade for a specific course
o Add a new student to the system

Common questions

Powered by AI

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 .

You might also like