Python Day 9
Python Day 9
Sequences: Packing and unpacking, Mutable vs. immutable sequences, Strings: Advanced
methods, Lists and list comprehensions, Tuples , Problems on above concepts.
Sequences in Python
Sequences are ordered collections of items. Python provides several built-in sequence
types, including:
● Strings
● Lists
● Tuples
All sequences share common operations, such as indexing, slicing, and iterating.
Packing
# Packing example
packed = 1, 2, 3, 4
Unpacking
Unpacking allows you to assign elements of a sequence to multiple variables.
# Unpacking example
x, y, z = 10, 20, 30
print(x, y, z) # Output: 10 20 30
*a, b = [1, 2, 3, 4]
Key Notes:
● Number of variables on the left must match the elements unless * is used.
● Useful in returning multiple values from a function.
def return_multiple():
return 5, 10, 15
Mutable Sequences
Mutable sequences can be modified after creation. You can change its value.
● Example: Lists
my_list = [1, 2, 3]
Immutable Sequences
# Strings
text = "hello"
# Tuples
my_tuple = (1, 2, 3)
Modifiable? Yes No
Use Case When frequent updates are For read-only or fixed data
needed
Strings in Python are immutable sequences of characters. Here are some advanced
methods:
split(delimiter) Splits the string into a list "a,b,c".split(",") → ["a", "b", "c"]
using delimiter.
String Formatting
● f-strings:
name = "Alice"
age = 25
Lists
Lists are mutable sequences that can hold mixed data types.
List Methods:
List Comprehensions
Syntax:
Examples:
Tuples
Tuples are immutable sequences, often used for fixed collections of items.
Tuple Methods
Examples:
print(my_tuple.count(10)) # Output: 2
print(my_tuple.index(20)) # Output: 1
packed = 1, 2, 3
# Unpacking
x, y, z = packed
print(x, y, z) # Output: 1 2 3
Write a function that returns the sum and product of two numbers. Use unpacking to
assign the results.
return a + b, a * b
Create a list of squares for all odd numbers between 1 and 10.
odd_squares = [x**2 for x in range(1, 11) if x % 2 != 0]
print(my_tuple.count(20)) # Output: 1
print(my_tuple.index(30)) # Output: 2
print(len(vowels)) # Output: 3
What happens if you try to modify a tuple? Test this with code.
my_tuple = (1, 2, 3)
_____________________________________________
1. Write a program to demonstrate tuple packing and unpacking with multiple
variables.
2. Write a program to swap two variables using tuple unpacking.
3. Create a function that returns multiple values (e.g., sum and product of two
numbers) and unpack them in the caller.
4. Write a program to unpack a nested list or tuple and access its inner elements.
5. Demonstrate how to unpack sequences with * (e.g., head, *middle, tail).
6. Write a program to show the difference between mutable (list) and immutable
(tuple) sequences by modifying elements.
7. Create a program to compare memory addresses of mutable vs immutable
sequences when their values are changed.
8. Write a program to simulate a shopping cart using a list (mutable) and a tuple
(immutable).
9. Demonstrate how slicing works differently in mutable and immutable sequences.
10.Show how concatenation affects both mutable and immutable sequences.
16.Write a program to generate a list of squares for numbers from 1 to 10 using list
comprehensions.
17.Create a list comprehension to filter out vowels from a given string.
18.Write a program to flatten a 2D list (list of lists) into a single list using list
comprehensions.
19.Generate a list of all prime numbers between 1 and 50 using list
comprehensions.
20.Write a program to create a dictionary from two lists using list comprehensions.
5. Tuples
21.Write a program to create a tuple, access its elements, and demonstrate its
immutability.
22.Develop a program to sort a list of tuples based on the second element of each
tuple.
23.Write a program to find the maximum and minimum elements in a tuple of
numbers.
24.Create a program to demonstrate the use of tuples as keys in a dictionary.
25.Write a program to find the index of a given element in a tuple.
26.Write a program to combine two lists into a list of tuples using zip(), and then
unpack the tuples.
27.Develop a function that takes a list of integers and returns a tuple with the even
numbers in one list and odd numbers in another.
28.Write a program to demonstrate the difference between shallow and deep copies
of a list containing tuples.
29.Create a function that takes a string of numbers separated by commas, converts
it to a list of integers, and returns their sum.
30.Develop a program to convert a list of strings to uppercase and remove
duplicates using list comprehensions.