Practical_3: Iterable objects or Containers#
Exercise 1:#
Create a list of integers from 1 to 10.
# Exercise 1
Exercise 2:#
Create a tuple containing the elements "apple"
, "banana"
, and "cherry"
.
# Exercise 2
Exercise 3:#
Convert the list numbers = [10, 20, 30, 40]
into a tuple.
# Exercise 3
Exercise 4:#
Create a set containing the elements 1, 2, 3, 4, 5
.
# Exercise 4
Exercise 5:#
Create a dictionary student
with the keys "name"
, "age"
, and "grade"
and corresponding values "Alice"
, 20
, and "A"
.
# Exercise 5
Exercise 6:#
Access the second element of the list [10, 20, 30, 40, 50]
using indexing.
# Exercise 6
Exercise 7:#
Change the third element of the list fruits = ["apple", "banana", "cherry", "orange"]
to "mango"
.
# Exercise 7
Exercise 8:#
Add the number 6
to the set numbers = {1, 2, 3, 4, 5}
.
# Exercise 8
Exercise 9:#
Remove the key "age"
from the dictionary person = {"name": "John", "age": 25, "city": "New York"}
.
# Exercise 9
Exercise 10:#
Create a range object starting from 0 to 9.
# Exercise 10
Exercise 11:#
Use a list comprehension to create a list of squares of numbers from 1 to 10.
# Exercise 11
Exercise 12:#
Create a tuple of even numbers between 2 and 10 using a tuple comprehension.
# Exercise 12
Exercise 13:#
Use a for loop to iterate through the list fruits = ["apple", "banana", "cherry"]
and print each element.
# Exercise 13
Exercise 14:#
Check if the value 3
exists in the set numbers = {1, 2, 3, 4, 5}
.
# Exercise 14
Exercise 15:#
Use the len()
function to find the length of the tuple numbers = (10, 20, 30)
.
# Exercise 15
Exercise 16:#
Add the value 100
at the end of the list scores = [80, 85, 90, 95]
.
# Exercise 16
Exercise 17:#
Write a function that takes a list as input and returns the sum of all elements in the list.
# Exercise 17
Exercise 18:#
Create a set containing the unique elements of the list numbers = [1, 2, 2, 3, 4, 4, 5]
.
# Exercise 18
Exercise 19:#
Convert the string "hello"
into a list of characters.
# Exercise 19
Exercise 20:#
Write a Python function that reverses a tuple (1, 2, 3, 4)
.
# Exercise 20
Exercise 21:#
Merge two dictionaries dict1 = {"a": 1, "b": 2}
and dict2 = {"c": 3, "d": 4}
into a single dictionary.
# Exercise 21
Exercise 22:#
Create a dictionary where keys are numbers from 1 to 5, and values are their squares.
# Exercise 22
Exercise 23:#
Write a Python program to iterate through a dictionary and print each key-value pair.
# Exercise 23
Exercise 24:#
Create a list of the first 5 letters of the alphabet using the chr()
function.
# Exercise 24
Exercise 25:#
Use the zip()
function to combine two lists [1, 2, 3]
and ["a", "b", "c"]
into a list of tuples.
# Exercise 25
Exercise 26:#
Find the index of the value 50
in the list [10, 20, 30, 40, 50]
.
# Exercise 26
Exercise 27:#
Sort the list names = ["Alice", "Bob", "Charlie", "David"]
in alphabetical order.
# Exercise 27
Exercise 28:#
Write a Python function that takes a list of integers as input and returns the largest number.
# Exercise 28
Exercise 29:#
Use the count()
method to count how many times "apple"
appears in the list fruits = ["apple", "banana", "apple", "orange"]
.
# Exercise 29
Exercise 30:#
Create a dictionary person
where keys are "name"
, "age"
, and "city"
, and values are "Alice"
, 25
, and "New York"
respectively. Then access the value of the "city"
key.
# Exercise 30
Exercise 31:#
Write a Python function that takes a list as input and returns a new list with all duplicates removed.
# Exercise 31
Exercise 32:#
Write a Python function that takes two lists and returns their intersection.
# Exercise 32
Exercise 33:#
Create a generator that yields the squares of numbers from 1 to 10.
# Exercise 33
Exercise 34:#
Write a Python function that takes a dictionary as input and returns the sum of all its values.
# Exercise 34
Exercise 35:#
Use the sorted()
function to sort the list [3, 1, 4, 1, 5, 9]
in ascending order.
# Exercise 35
Exercise 36:#
Write a Python function that takes a list of strings as input and returns the longest string.
# Exercise 36
Exercise 37:#
Check if all the elements in the list [1, 2, 3, 4, 5]
are greater than 0 using the all()
function.
# Exercise 37
Exercise 38:#
Create a list of tuples where each tuple contains a number and its square for numbers from 1 to 5.
# Exercise 38
Exercise 39:#
Write a Python function that takes a list of tuples and returns a list of the second elements from each tuple.
# Exercise 39
Exercise 40:#
Write a Python function that flattens a list of lists into a single list.
# Exercise 40
Exercise 41:#
Use the enumerate()
function to iterate through the list ["apple", "banana", "cherry"]
and print the index and value.
# Exercise 41
Exercise 42:#
Write a Python program that checks if a tuple is empty.
# Exercise 42
Exercise 43:#
Create a list of even numbers between 1 and 20 using the range()
function.
# Exercise 43
Exercise 44:#
Write a Python function that takes a list of numbers as input and returns the list in reverse order.
# Exercise 44
Exercise 45:#
Create a dictionary comprehension that maps numbers from 1 to 5 to their cubes.
# Exercise 45
Exercise 46:#
Write a Python program that creates a new list containing only the unique elements of the list [1, 2, 2, 3, 4, 4, 5]
.
# Exercise 46
Exercise 47:#
Write a Python function that takes a list of dictionaries and returns a list of the values for a given key.
# Exercise 47
Exercise 48:#
Write a Python function that uses a generator to yield the Fibonacci sequence up to n
terms.
# Exercise 48
Exercise 49:#
Write a Python function that takes a string and returns a list of all unique characters in the string.
# Exercise 49
Exercise 50:#
Write a Python function that merges two sorted lists into a single sorted list.
# Exercise 50
Exercise 51:#
Use the
filter()
function to filter out odd numbers from the list [1, 2, 3, 4, 5, 6]
.
# Exercise 51
Exercise 52:#
Write a Python program that converts a list of tuples [(1, 2), (3, 4), (5, 6)]
into a dictionary.
# Exercise 52
Exercise 53:#
Write a Python function that takes two sets and returns their symmetric difference.
# Exercise 53
Exercise 54:#
Write a Python program that finds the common elements in three lists list1
, list2
, and list3
.
# Exercise 54
Exercise 55:#
Write a Python function that returns the factorial of a number using recursion.
# Exercise 55
Exercise 56:#
Write a Python program that removes all empty strings from the list ["apple", "", "banana", "", "cherry"]
.
# Exercise 56
Exercise 57:#
Write a Python function that takes a dictionary and returns a new dictionary with the keys and values swapped.
# Exercise 57
Exercise 58:#
Write a Python function that takes a list of integers and returns a new list with only the even numbers.
# Exercise 58
Exercise 59:#
Write a Python function that takes a list of tuples and returns a list of the first elements of each tuple.
# Exercise 59
Exercise 60:#
Write a Python program that removes duplicates from a list without changing the order of elements.
# Exercise 60
Exercise 61:#
Use the map()
function to convert a list of strings to a list of their lengths.
# Exercise 61
Exercise 62:#
Write a Python program that calculates the average of a list of numbers.
# Exercise 62
Exercise 63:#
Write a Python function that generates a list of the first n
Fibonacci numbers.
# Exercise 63
Exercise 64:#
Write a Python program that counts how many times each element appears in the list ["apple", "banana", "apple", "cherry"]
.
# Exercise 64
Exercise 65:#
Write a Python program that removes all occurrences of a specific value from a list.
# Exercise 65
Exercise 66:#
Write a Python function that takes a list of strings and returns a list of the strings that start with a vowel.
# Exercise 66
Exercise 67:#
Write a Python program that finds the intersection of two sets set1
and set2
.
# Exercise 67
Exercise 68:#
Write a Python function that flattens a nested list.
# Exercise 68
Exercise 69:#
Write a Python program that checks if all the elements in a list are unique.
# Exercise 69
Exercise 70:#
Use the reduce()
function from the functools
module to multiply all elements in a list.
# Exercise 70
Exercise 71:#
Write a Python function that takes a list of strings and returns a dictionary where keys are strings and values are their lengths.
# Exercise 71
Exercise 72:#
Write a Python program that counts the number of vowels in a string.
# Exercise 72
Exercise 73:#
Create a list of tuples where each tuple contains a number from 1 to 5 and its factorial.
# Exercise 73
Exercise 74:#
Write a Python program that converts a list of dictionaries into a single dictionary by merging them.
# Exercise 74
Exercise 75:#
Write a Python function that checks if a set is a subset of another set.
# Exercise 75
Exercise 76:#
Write a Python program that sorts a list of tuples based on the second element in each tuple.
# Exercise 76
Exercise 77:#
Write a Python function that takes a list of integers and returns a new list with only the positive integers.
# Exercise 77
Exercise 78:#
Write a Python function that takes two sets and returns their union.
# Exercise 78
Exercise 79:#
Write a Python function that takes a list of numbers and returns the product of all the numbers.
# Exercise 79
Exercise 80:#
Write a Python program that converts a list of characters into a string.
# Exercise 80
Exercise 81:#
Write a Python program that finds the second largest number in a list of integers.
# Exercise 81
Exercise 82:#
Write a Python function that takes a dictionary and returns a list of its keys in sorted order.
# Exercise 82
Exercise 83:#
Write a Python program that finds the most common element in a list.
# Exercise 83
Exercise 84:#
Write a Python function that takes a list of tuples and returns a list of the tuples sorted by the first element.
# Exercise 84
Exercise 85:#
Write a Python program that removes duplicates from a list of dictionaries.
# Exercise 85
Exercise 86:#
Write a Python function that takes a list of numbers and returns the minimum and maximum numbers in the list.
# Exercise 86
Exercise 87:#
Write a Python program that creates a list of tuples from two lists.
# Exercise 87
Exercise 88:#
Write a Python function that takes a list of strings and returns a list of the strings in reverse order.
# Exercise 88
Exercise 89:#
Write a Python function that takes a list of integers and returns the sum of all odd numbers.
# Exercise 89
Exercise 90:#
Write a Python program that checks if a string is a palindrome.
# Exercise 90
Exercise 91:#
Write a Python program that finds the median of a list of numbers.
# Exercise 91
Exercise 92:#
Write a Python function that takes a list of dictionaries and returns a dictionary with the total value for each key.
# Exercise 92
Exercise 93:#
Write a Python program that calculates the mode of a list of numbers.
# Exercise 93
Exercise 94:#
Write a Python function that takes a list of numbers and returns the sum of the squares of all the numbers.
# Exercise 94
Exercise 95:#
Write a Python program that creates a list of tuples, where each tuple contains a number and its cube for numbers from 1 to 5.
# Exercise 95
Exercise 96:#
Write a Python function that returns a list of all prime numbers up to n
.
# Exercise 96
Exercise 97:#
Write a Python program that finds the common keys between two dictionaries.
# Exercise 97
Exercise 98:#
Write a Python function that takes a list of strings and returns a list of the strings that contain more than 5 characters.
# Exercise 98
Exercise 99:#
Write a Python program that converts a list of strings into a list of tuples, where each tuple contains the string and its length.
# Exercise 99
Exercise 100:#
Write a Python function that takes a list of dictionaries and returns a new dictionary by summing the values of each key across all dictionaries.
# Exercise 100