0% found this document useful (0 votes)
5 views50 pages

Python l2

This document covers Python's container data types, including lists, tuples, sets, and dictionaries. It explains their characteristics, advantages, disadvantages, and provides examples of operations such as adding, accessing, and deleting items. Additionally, it includes practice questions and assignments to reinforce understanding of these data types.
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)
5 views50 pages

Python l2

This document covers Python's container data types, including lists, tuples, sets, and dictionaries. It explains their characteristics, advantages, disadvantages, and provides examples of operations such as adding, accessing, and deleting items. Additionally, it includes practice questions and assignments to reinforce understanding of these data types.
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

Python

Programming

Module - 1
Container Data Types

- [Link]@[Link]
Python
Programming

Topics to be Covered
list
tuple
set
dictionary
Python
Programming

Container Data Type

They can store multiple items (objects), within a


single variable.
Python
Programming

Sequence Data Type

They store collections of elements in a specific


order. It’s also a type of a container
Python
Programming

Mapping Data Type

A mapping data type is a container that


associates keys with values
Python
Programming

List: Sequence Data Type


List is a data type where
you can add multiple
items under one variable
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

How List Store Values in Memory?


\0 shows null character.
This representation uses in
C Language
Python Dont Need this kind
of terminator to show, the
string is end here
Python identifies where
string is going to be end
through its lenght
Python
Programming

Creating a List
Python
Programming

Dynamic Size of List Example

Output
Python
Programming

Accessing items of a List


There are two ways
of accessing items
from a list:
Indexing
Slicong
Python
Programming

Adding items in a List


There are three ways of adding items in a list:
Append
Extend
Insert

You can add only one item at a time using insert function
Python
Programming

Editing items in a List


Python
Programming

Deleting items from a List


There are four ways of deleting items from a list:
del
remove
pop
clear
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.

Grocery Stock Tracker:


ticket_prices = [250, 300, 200, 400, 150]
Tasks:
Find the highest and lowest ticket price.
Sort the list in descending order.
Add a new ticket price 350 while keeping the list sorted.
Display only the top 3 expensive tickets.
Python
Programming

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

Tuple: Sequence Data Type


A tuple is an immutable
list and it can not be
changed in any way
once it created. Characterstics:
Ordered
Unchangeble
Allows Duplicate
Python
Programming

Creating a Tuple:
Python
Programming

Accessing items in a Tuple


There are two ways
of accessing items
from a tuple:
Indexing
Slicong
Python
Programming

Deleting items from a Tuple


Only One way to delete tuple is using DEL keyword
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.

Employee Payroll Record:

Payroll system keeps a tuple: (employee_id, name, base_salary, month, net_pay).


Design data for multiple employees and print the one with the highest salary for a given
month.

Note: Atleast 3 employee records must be created


Python
Programming

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

accessing items by indexing or


slicing is not allowed as well as
editing items is not allowed in
sets
Python
Programming

Add/ Update in Set:


Python
Programming

Deleting Items in 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.

Count Distinct Words in a Sentence:


- A user enters a sentence (as a single string).
- Your task is to count how many distinct words are in the sentence.
Python
Programming

Dictionary: Mapping Data Type


it is a python data type where we store
data as collection of keys and values,
used to store data values like a map

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

Accessing items in a Dictionary:


Python
Programming

Adding items in a Dictionary:


Python
Programming

Removing items in a Dictionary:


Python
Programming

Removing items in a Dictionary:


Python
Programming

Editing in a Dictionary:
Python
Programming

Dictionary Functions
Python
Programming

Assignment
Student Performance Tracker using Dictionaries:

Create a dictionary where:


Keys are student names.
Values are lists of their marks in 3 subjects.

Write a code to:


Add three new students with their marks.
Calculate average marks for each student.
Then add student(s) with the average marks into the dictionary.

Convert the dictionary into a list of tuples and sort it by average marks.
Python
Programming

Assignment
Student Report Generator:

Create a system where users can:


Add student info (name, age, subjects, marks) using input function.
Store info in a dictionary.
Use sets to ensure unique subject names.
Use lists to maintain ordered scores.
Use tuples for immutable personal info (name, age).

Print a summary report at the end.


Python
Programming

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.

You might also like