ca0ed347-54d5-4ff2-ae50-828f4b10614a

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Unit -2 (8m): https://awaisahmed.notion.site/Unit-2-8m-07957688c19d43eea4460e4e...

Unit -2 (8m):
Explain the different ways to create a dictionary in python.

In Python, there are several ways to create a dictionary. Here are some
examples:

1. Using curly braces {} :

pythonCopy code my_dict = {'key1': 'value1', 'key2': 'va


lue2', 'key3': 'value3'}

In this example, we create a dictionary with three key-value pairs.

2. Using the dict() constructor:

pythonCopy code my_dict = dict(key1='value1', key2='valu


e2', key3='value3')

This creates a dictionary using the dict() constructor, which


takes keyword arguments for the keys and values of the
dictionary.

3. Using a list of tuples:

pythonCopy code my_dict = dict([('key1', 'value1'), ('ke


y2', 'value2'), ('key3', 'value3')])

In this example, we create a list of tuples, where each tuple


represents a key-value pair in the dictionary. We then pass this list
to the dict() constructor to create the dictionary.

4. Using dictionary comprehension:

cssCopy code my_dict = {k: v for k, v in [('key1', 'valu


e1'), ('key2', 'value2'), ('key3', 'value3')]}

1 of 13 12-12-2023, 19:10
Unit -2 (8m): https://awaisahmed.notion.site/Unit-2-8m-07957688c19d43eea4460e4e...

This example creates a dictionary using a dictionary


comprehension, where we loop through a list of tuples and create
a new dictionary with the key-value pairs.
5. Using fromkeys() method:

pythonCopy code my_dict = dict.fromkeys(['key1', 'key2',


'key3'], 'default_value')

This creates a dictionary using the fromkeys() method, which


takes an iterable as the first argument and a default value as the
second argument. This method creates a new dictionary with the
keys from the iterable and the default value for each key.

These are just a few examples of the different ways to create a


dictionary in Python. The best way to create a dictionary depends on
the specific requirements of your program.
Differentiate the difference between list and tuple and dictionary in
python?

In Python, lists, tuples, and dictionaries are three different types of


data structures. Here are the main differences between them:

1. Lists:

• Lists are mutable, which means you can add, remove, or


change items after the list has been created.

• Lists are ordered, which means the items in the list have a
specific order and can be accessed by index.

• Lists are created using square brackets [] .

Example: my_list = [1, 2, 3]

2. Tuples:

• Tuples are immutable, which means once a tuple is created,


you cannot add, remove, or change items.

• Tuples are ordered, which means the items in the tuple have a
specific order and can be accessed by index.

• Tuples are created using parentheses ()

2 of 13 12-12-2023, 19:10
Unit -2 (8m): https://awaisahmed.notion.site/Unit-2-8m-07957688c19d43eea4460e4e...

• Tuples are created using parentheses () .

Example: my_tuple = (1, 2, 3)

3. Dictionaries:

• Dictionaries are mutable, which means you can add, remove,


or change key-value pairs after the dictionary has been
created.

• Dictionaries are unordered, which means the key-value pairs


do not have a specific order.

• Dictionaries are created using curly braces {} or the dict()


constructor.

Example: my_dict = {'key1': 'value1', 'key2': 'value2'} or


my_dict = dict(key1='value1', key2='value2')

In summary, lists and tuples are similar in that they are ordered and
can be accessed by index, but lists are mutable while tuples are
immutable. Dictionaries are different from lists and tuples in that they
use key-value pairs and are unordered.
(a) Explain the use of the zip ( ) function in python?
(b) Explain packing and unpacking in python?

(a) The zip() function in Python is used to combine two or more


iterables into a single iterable. It returns an iterator that aggregates
the elements from each of the iterables. The resulting iterator stops
when the shortest input iterable is exhausted. The syntax for the
zip() function is as follows:

scssCopy code zip(iterable1, iterable2, ...)

Here's an example of how to use the zip() function to combine two


lists:

scssCopy code list1 = [1, 2, 3] list2 = ['a', 'b', 'c'] zipp


ed = zip(list1, list2) print(list(zipped)) # [(1, 'a'), (2,
'b'), (3, 'c')]

3 of 13 12-12-2023, 19:10
Unit -2 (8m): https://awaisahmed.notion.site/Unit-2-8m-07957688c19d43eea4460e4e...

(b) In Python, packing and unpacking are two concepts related to


handling multiple values. Packing refers to combining multiple values
into a single variable, whereas unpacking refers to separating a single
variable into multiple values.

Here's an example of packing:

pythonCopy code my_tuple = 1, 2, 3 # packing three values in


to a tuple print(my_tuple) # (1, 2, 3)

And here's an example of unpacking:

pythonCopy code my_tuple = (1, 2, 3) a, b, c = my_tuple # un


packing the tuple into three variables print(a) # 1 print(b)
# 2 print(c) # 3

Packing and unpacking can be used with other data structures as well,
such as lists and dictionaries.
(a) Explain the various operations that can be performed on lists in
python?
(b) Discuss the built–in functions used on dictionaries with
examples?

(a) Lists are a fundamental data structure in Python, and there are
several operations that can be performed on them. Some of the
commonly used operations include:

• Indexing: Accessing elements of a list by their position. For


example, my_list[0] returns the first element of the list.

• Slicing: Extracting a subsequence of elements from a list. For


example, my_list[1:4] returns a list containing the second, third,
and fourth elements of the original list.

• Appending: Adding an element to the end of a list. For example,


my_list.append(5) adds the value 5 to the end of the list.

• Extending: Adding multiple elements to the end of a list. For


example, my_list.extend([6, 7, 8]) adds the values 6, 7, and 8

4 of 13 12-12-2023, 19:10
Unit -2 (8m): https://awaisahmed.notion.site/Unit-2-8m-07957688c19d43eea4460e4e...

to the end of the list.

• Inserting: Adding an element at a specific position in a list. For


example, my_list.insert(2, 'new value') adds the string 'new
value' at position 2 in the list.

• Removing: Removing the first occurrence of a specific value from


a list. For example, my_list.remove('old value') removes the
first occurrence of the string 'old value' from the list.

• Sorting: Sorting the elements of a list. For example,


my_list.sort() sorts the elements of the list in ascending order.

• Reversing: Reversing the order of elements in a list. For example,


my_list.reverse() reverses the order of elements in the list.

(b) Dictionaries in Python have several built-in functions that can be


used to manipulate their content. Here are some commonly used
functions:

• keys() : returns a list of all the keys in the dictionary.

• values() : returns a list of all the values in the dictionary.

• items() : returns a list of tuples, where each tuple contains a key-


value pair from the dictionary.

• get(key) : returns the value associated with the specified key, or


None if the key does not exist in the dictionary.

• pop(key) : removes the key-value pair associated with the


specified key from the dictionary, and returns the value of the
removed item.

• update(dict2) : adds the key-value pairs from the specified


dictionary dict2 to the current dictionary.

• clear() : removes all the key-value pairs from the dictionary.

Here's an example of using some of these functions:

scssCopy code my_dict = {'a': 1, 'b': 2, 'c': 3} print(my_di


ct.keys()) # ['a', 'b', 'c'] print(my_dict.values()) # [1,
2, 3] print(my_dict.items()) # [('a', 1), ('b', 2), ('c',
3)] print(my_dict.get('d')) # None print(my_dict.pop('b')) #
2 print(my_dict) # {'a': 1, 'c': 3} my_dict2 = {'d': 4, 'e':
5} my_dict.update(my_dict2) print(my_dict) # {'a': 1, 'c':

5 of 13 12-12-2023, 19:10
Unit -2 (8m): https://awaisahmed.notion.site/Unit-2-8m-07957688c19d43eea4460e4e...

5} my_dict.update(my_dict2) print(my_dict) # {'a': 1, 'c':


3, 'd': 4, 'e': 5} my_dict.clear() print(my_dict) # {}

(a) Explain the methods associated with sets with an example?


(b) Explain the frozen set with an example?

(a) Sets in Python are used to store a collection of unique elements.


Sets are mutable i.e. we can change the elements in a set after it is
created. Here are some of the methods associated with sets:

1. add(): This method is used to add an element to a set.

Example:

s = {1, 2, 3} s.add(4) print(s) # Output: {1, 2, 3, 4}

1. remove(): This method is used to remove an element from a set.

Example:

s = {1, 2, 3, 4} s.remove(3) print(s) # Output: {1, 2, 4}

1. union(): This method is used to combine two or more sets.

Example:

s1 = {1, 2, 3} s2 = {3, 4, 5} s3 = s1.union(s2) print(s3) #


Output: {1, 2, 3, 4, 5}

1. intersection(): This method is used to find the common elements


between two or more sets.

Example:

s1 = {1, 2, 3} s2 = {3, 4, 5} s3 = s1.intersection(s2) print


(s3) # Output: {3}

(b) A frozen set is a set that is immutable i.e. we cannot change the

6 of 13 12-12-2023, 19:10
Unit -2 (8m): https://awaisahmed.notion.site/Unit-2-8m-07957688c19d43eea4460e4e...

(b) A frozen set is a set that is immutable i.e. we cannot change the
elements in a frozen set after it is created. We can create a frozen set
using the frozenset() function. Here is an example:

s1 = frozenset([1, 2, 3]) s2 = frozenset([2, 3, 4]) s3 = s1.


union(s2) print(s3) # Output: frozenset({1, 2, 3, 4})

7 of 13 12-12-2023, 19:10

You might also like