PWP Notes3
PWP Notes3
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.
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]
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.
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]
Here are some of the basic operations you can perform on lists in Python:
3.
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 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.
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)