0% found this document useful (0 votes)
16 views

ASSIGNMENT PHYTTON

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)
16 views

ASSIGNMENT PHYTTON

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/ 11

ASSIGNMENT-3

Q.1 Compare lists, tuples, sets, and dictionaries in Python based on mutability, usage, and
performance. Write examples to illustrate the differences between these data structures.
Feature List Tuple Set Dictionary
Mutability Mutable (can change) Immutable Mutable (can Mutable (can
(cannot change) change, but change, but keys
elements must be must be unique)
unique)
Usage Ordered collection, Ordered Unordered Unordered
allows duplicates. collection, collection of collection of key-
Ideal for storing and allows unique items. value pairs.
modifying sequential duplicates. Best Great for checking Perfect for fast
data. for fixed data, if something exists lookups, updates,
like coordinates or removing and storing
or function duplicates. related data.
return values.
Speed of Fast for accessing Faster than lists Very fast for Extremely fast for
Operations elements by index or for accessing checking if an item finding, adding,
adding at the end, data but cannot exists, adding, or or removing
slower for inserting or be modified. removing items. items by key.
removing in the
middle.
Example my_list = [1, 2, 3] my_tuple = (1, 2, my_set = {1, 2, 3} my_dict = {"a":
my_list.append(4) → 3) my_set.add(4) → 1, "b": 2}
[1, 2, 3, 4] # Immutable: {1, 2, 3, 4} my_dict["c"] = 3
my_tuple[1] = 4 my_set.add(3) → → {"a": 1, "b": 2,
→ Error {1, 2, 3, 4} (no "c": 3}
duplicates) my_dict["b"] → 2

Q.2 Write a Python program to perform the following operations on a given string:
1. Convert the string to lowercase and uppercase.
2. Replace all spaces with underscores.
3. Split the string into words and join them back with a hyphen.
4. Explain the string methods used in your program.
Q.3 Discuss the immutable nature of tuples in Python. Write a Python program to create a
tuple of numbers, unpack its elements into variables, and find the sum of the elements. Explain
tuple packing and unpacking with examples.
Tuples in Python are immutable, meaning their elements cannot be modified, added, or removed
after the tuple is created. This immutability makes tuples:
• Hashable: They can be used as keys in dictionaries or elements of sets (provided all elements
are also hashable).
• Memory Efficient: They are faster and use less memory compared to lists.
Tuple Packing and Unpacking
1. Tuple Packing:
o Packing means assigning multiple values to a single tuple.

2. Tuple Unpacking:
• Unpacking means extracting elements from a tuple into separate variables.

Q.4 Discuss how strings are created and manipulated in Python. Write a Python program that
takes a sentence as input, counts the number of vowels in it, and prints the result.
Q.5 Compare lists and tuples in Python. Write a Python program to illustrate the immutable
nature of tuples with an example.
Feature List Tuple
Mutability Mutable: Elements can be modified. Immutable: Elements cannot be
modified.
Syntax Created with square brackets [ ]. Created with parentheses ( ).
Usage Used for dynamic data that requires Used for fixed data that should not
changes. change.
Performance Slower than tuples due to extra Faster than lists as they are
overhead. immutable.
Memory Uses more memory due to mutability. More memory-efficient than lists.
Efficiency
Hashability Not hashable (cannot be keys in Hashable if all elements are
dictionaries). hashable.
ASSIGNMENT-4
Q.1 What is tuple unpacking? Provide an example Python program where you unpack a tuple
containing three elements into separate variables.
Tuple unpacking refers to the process of assigning the elements of a tuple to multiple variables in a
single statement. Python allows you to "unpack" a tuple into individual variables based on the
number of elements in the tuple.

Q.2 Write a Python program using a while loop to reverse a given number. Include comments
to explain each step and discuss the working of the while loop in detail.

Q.3 Create a dictionary in Python with three key-value pairs: name, age, and city. Write code
to update the age and remove the city. Print the updated dictionary.
Q.4 Write a Python program to count the occurrences of each word in a given string and store
the result in a dictionary.
Q.5 Find all the unique elements from a list. Explain and demonstrate the use of a frozen set
with an example.

Frozen Set in Python


A frozen set is an immutable version of a set. Like sets, frozen sets are unordered collections of
unique elements, but unlike regular sets, you cannot add or remove elements from a frozen set after it
has been created.
• Mutable sets can be modified by adding or removing elements.
• Frozen sets are immutable, which makes them hashable and can be used as keys in
dictionaries or elements in other sets.
ASSIGNMENT-5
Q.1 Given a string, write a program to find which vowels are present in it using a set.

Q.2 Write a program to count the number of unique words in a paragraph using a set.

Q.3 Write a program to create a set of unique characters from a given string.

Q.4 Given a sentence, check if all words in the sentence are unique using sets.
Q.5 Write a program to create a set of unique characters from a given string.

Q.6 Can you retrieve elements from a set? Why or why not? Explain with an example.
In Python, sets are unordered collections of unique elements, and while you can retrieve elements
from a set, you cannot access them by index (like you would with lists or tuples), because sets do
not maintain any specific order.
Why can't you retrieve elements by index?
• Sets are unordered collections, meaning the elements are stored in no particular order.
• There is no concept of an "index" in a set like there is in a list. Thus, you can't directly access
elements using an index (e.g., set[0]).
However, you can:
1. Iterate over the set to retrieve elements.
2. Convert the set to a list or another ordered collection if you need to access elements by
index.

You might also like