Python l2
Python l2
Programming
Module - 1
Container Data Types
- [Link]@[Link]
Python
Programming
Topics to be Covered
list
tuple
set
dictionary
Python
Programming
Advantages of List:
List is dynamic in size
Lists are Hetrogenous
DisAdvantages of List:
Slow Execution
List occupy more space in memory
Python
Programming
Creating a List
Python
Programming
Output
Python
Programming
You can add only one item at a time using insert function
Python
Programming
Characteristics of a List
List: Ordered
Python
Programming
List Functions
Python
Programming
List Functions
Python
Programming
Practice Questions
Grocery Stock Tracker:
available_stock = ["apple", "milk", "bread", "eggs", "rice", "butter"]
Tasks:
Add "flour" and "sugar" at the end.
Replace "bread" with "brown bread".
Remove "milk" and print the updated list.
Display only the middle 3 items using slicing.
Practice Questions
Playlist Management:
You have to create a music playlist.
Make a list of 5 songs.
Swap the first and last song to change the playlist order.
Add a new favorite song in the middle.
Print all songs except the first and last.
Product Prices:
You have prices of 6 products in your shop.
Create a list of product prices.
Increase the price of two products due to tax (1% tax /product).
Remove the lowest-priced product.
Calculate the total price of the remaining products.
Python
Programming
Creating a Tuple:
Python
Programming
Tuple Unpacking
Python
Programming
Tuple Functions
Python
Programming
Practice Questions
Apartment Details:
An apartment listing is stored as a tuple:
apartment = ("Downtown", 3, 2, 45000, "Available")
- Extract and print the location and rent.
- Change the status to "Rented" without modifying the original tuple.
Break
-
Python
Programming
Sets:
A set is an unordered collection
of items. Every Set element is
unique and must be immutable.
However, a set is mutable.
Characterstics:
UnOrdered
Mutable
No Duplicates
Can’t contain mutable data types
Python
Programming
Creating a Set:
Python
Programming
Set Operations:
Python
Programming
Frozenset:
Frosenset is just immutable version
of a python set object.
Characterstics:
Can perform only read operations
Write operation is not supported
Python
Programming
Creating a frosenset:
Python
Programming
Set Functions
Python
Programming
Set Functions
Python
Programming
Practice Questions
A website wants to count how many unique visitors it had in a day.
Every time a user visits, their user_id is logged — but users can visit multiple times.
visits = [101, 102, 103, 101, 104, 102, 105, 103, 105, 106]
Tasks
- Find how many unique visitors visited the site.
- Display the list of unique user_ids (in any order).
- Compare the total visits vs. unique visitors.
Characterstics:
Mutable
Keys can’t be duplicated
Keys can’t be mutable data types
Python
Programming
Creating a Dictionary:
Python
Programming
Creating a Dictionary:
Python
Programming
Editing in a Dictionary:
Python
Programming
Dictionary Functions
Python
Programming
Assignment
Student Performance Tracker using Dictionaries:
Convert the dictionary into a list of tuples and sort it by average marks.
Python
Programming
Assignment
Student Report Generator:
Assignment
Shopping Cart Snapshot:
Instructions: Questions:
Create 3 products.
Each product is a dictionary with: 1. What is the category of the second product?
"info"→ a tuple: (name, category) 2. What is the total price of all products?
"price"→ integer 3. Find the average rating of the first product.
→
"ratings" list of 3 numbers 4. Combine all tags from the products (using |
"tags"→ set of 3 tags operator).
5. Add a new product product4 with your own
Store all products in a list called cart. data and update cart.