0% found this document useful (0 votes)
11 views19 pages

PWP Notes3

The document covers various data structures in Python, including lists, tuples, sets, and dictionaries. It explains their characteristics, operations, and built-in functions, along with examples and sample programs for practical understanding. Key topics include differences between lists and tuples, indexing and slicing in lists, and creating and manipulating dictionaries.

Uploaded by

milee1722
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)
11 views19 pages

PWP Notes3

The document covers various data structures in Python, including lists, tuples, sets, and dictionaries. It explains their characteristics, operations, and built-in functions, along with examples and sample programs for practical understanding. Key topics include differences between lists and tuples, indexing and slicing in lists, and creating and manipulating dictionaries.

Uploaded by

milee1722
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
You are on page 1/ 19

Unit 3 Data Structures in Python

Q.1.Give two differences between list and tuple - 2mks

Q3: What is a dictionary? Answer:


A dictionary is an unordered collection of key-value pairs where each key is
unique. Values can be of any data type.
Example:

Q.2. Explain indexing and slicing in list with example - 4 mks


Q.3. Write a Python Program to accept values from user in a list
and find the largest number and smallest number in a list. - 6 mks
# Step 1: Take input from the user and convert to integers directly
numbers = list(map(int, input("Enter numbers separated by spaces: ").split())) #map() function
in Python is used to apply a function to each item in an iterable

# Step 2: Find the largest and smallest numbers


largest = max(numbers)
smallest = min(numbers)

# Step 3: Print the results


print("Largest number:", largest)
print("Smallest number:", smallest)

Q.4. Explain two ways to add objects / elements to list - 2 mks

Q.5. Explain four built-in list functions

a) Defining lists, accessing values in list, deleting values in list, updating lists.

In Python, lists are one of the most versatile and commonly used data structures. They can hold items of
any type, including other lists, and they allow you to perform various operations such as accessing,
updating, deleting, and modifying values.

1. Defining Lists in Python

A list is defined using square brackets [], and the elements are separated by commas.

# Defining a list
my_list = [1, 2, 3, 4, 5]
print(my_list) # Output: [1, 2, 3, 4, 5]

2. Accessing Values in a List

You can access the values in a list using indexing. Python uses zero-based indexing, so the first element of
a list has an index of 0.

# Accessing individual elements


print(my_list[0]) # Output: 1 (First element)
print(my_list[2]) # Output: 3 (Third element)

# Negative indexing (from the end of the list)


print(my_list[-1]) # Output: 5 (Last element)
print(my_list[-2]) # Output: 4 (Second to last element)
3. Deleting Values in a List

You can delete values from a list in several ways.

●​ Using del: Removes an element by index.


●​ Using remove(): Removes the first occurrence of a value.
●​ Using pop(): Removes an element at a specified index and returns it.

4. Updating Lists

You can update the elements of a list by assigning a new value to a specific index
or using slicing to update multiple elements at once.
# Updating a single element by index
my_list[0] = 10 # Changing the value at index 0
print(my_list) # Output: [10, 5]

b) Basic List Operations in Python

Here are some of the basic operations you can perform on lists in Python:
3.

4.Sorting : Sort the list in ascending or descending order.

5.
c) Built-in List Functions in Python

Python provides several built-in functions specifically designed for working with lists. Here are some of
the most commonly used functions:
3.2 Tuples:
Q.1.Explain four built-in tuple functions python with example
Q.2.Write a python program to input any two tuples and interchange the tuple variables.

A tuple is a collection of ordered, immutable elements. Unlike lists, tuples cannot be changed after
creation (i.e., they are immutable), but they allow you to store multiple items in a single variable.

a) Accessing values in Tuples, deleting values in Tuples, and updating Tuples.


You can access elements in a tuple using indexing, which works similarly to lists (zero-based indexing).
You can also use negative indexing to access elements from the end of the tuple.
b) Basic Tuple operations.
3. Length (len()): Get the number of elements in the tuple.​

my_tuple = (1, 2, 3)
print(len(my_tuple)) # Output: 3

4. Indexing: Access elements at a specific index.​



my_tuple = (10, 20, 30, 40)
print(my_tuple[2]) # Output: 30 (third element)

5. Slicing: Access a subset of elements (subtuple).​



my_tuple = (10, 20, 30, 40, 50)
sliced_tuple = my_tuple[1:4]
print(sliced_tuple) # Output: (20, 30, 40)

c) Built-in Tuple functions


3.3 Sets:
Q.1.Explain any six set function with example- 6 mks
Q.2.Explain any four set operations with example- 4 mks
Q.3.List and explain any four built-in functions on set - 6 mks
Q 4. Write python program to perform following operations on set.
i)Create set of five elements
ii) Access set elements
iii) Update set by adding one element
iv) Remove one element from set. - 4 mks

A set is an unordered collection of unique elements. Sets are useful when you need to store multiple items
but are only concerned with uniqueness and do not care about the order of the items.

a) Accessing values in Set, deleting values in Set and updating Sets.


Unlike lists or tuples, sets are unordered collections, which means you cannot access elements by index.
However, you can loop through the set or convert it to a list if you need to access specific elements.
b) Basic Set operations.

c) Built-in Set functions


3.4 Dictionaries in Python
Q.1.Write a program to create dictionary of students that includes their ROLL NO. and NAME.
i)Add three students in above dictionary
ii) Update name = ‘Shreyas’ of ROLL NO = 2
iii) Delete information of ROLL NO = 1 - 4mks

Q.2.Explain creating Dictionary and accessing Dictionary Elements with example.- 4mks
Q.3.Explain different functions or ways to remove key : value pair from Dictionary - 4mks
Q.4. What is dictionary? - 2mks

A dictionary is an unordered collection of key-value pairs. Each key must be unique, and values can be
of any type. Dictionaries are useful for storing data in a way that allows for fast lookups by key.
2. Deleting Values in a Dictionary

3. Updating Dictionaries
b) Basic Dictionary operations.

Accessing Values:
3

4.update():
my_dict = {'name': 'Alice', 'age': 25}
my_dict.update({'age': 26, 'city': 'New York'})
print(my_dict)

Output: {'name': 'Alice', 'age': 26, 'city': 'New York'}

c) Built-in Dictionaries functions


3

You might also like