1/22/25, 2:46 PM module 2 - Colab
Write a Python program to get a list of colours and display the first and last colour from the list.
# Get a list of colors from the user
l=[]
no=int(input("enter number of elements"))
for i in range(no):
color=input("enter color")
l.append(color)
print(l)
# Display the first and last colors
print("First color:", l[0])
print("Last color:", l[-1])
enter number of elements3
enter colorred
enter colorblue
enter colorgreen
['red', 'blue', 'green']
First color: red
Last color: green
Write a Python program to search a given element in a list.
# Get the list of elements from the user
l=[]
no=int(input("enter number of elements"))
for i in range(no):
number=input("enter number")
l.append(number)
print(l)
# Get the element to search for
target = input("Enter the element to search: ")
# Check if the element exists in the list
if target in l:
print(target,"is found in the list.")
else:
print(target, "is not found in the list.")
enter number of elements4
enter number34
enter number23
enter number3
enter number2
['34', '23', '3', '2']
Enter the element to search: 23
23 is found in the list.
Write a Python program to create a list of squares for the numbers from 0 to 9 using list comprehension
# Create a list of squares using list comprehension
squares = [x**2 for x in range(10)]
# Print the list of squares
print("List of squares:", squares)
List of squares: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Write a Python script to print a dictionary where the keys are the numbers between 10 and 15 (both included) and the values are the square of
the keys.
# Create an empty dictionary
squares_dict = {}
# Loop through numbers from 10 to 15 (both included)
for x in range(10, 16):
squares_dict[x] = x**2 # Assign the square of the number as the value
# Print the dictionary
print("Dictionary of squares:", squares_dict)
https://colab.research.google.com/drive/1_-OZu7iQAbGIAnCmDAi7rnt50gYsXdGr#scrollTo=yUuiApGRS--W&printMode=true 1/3
1/22/25, 2:46 PM module 2 - Colab
Dictionary of squares: {10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}
Write a Python program to find the frequency of each character in a given string and store the count in a dictionary with key as the character.
# Get input string from the user
string = input("Enter a string: ")
# Initialize an empty dictionary to store the frequency
frequency = {}
# Loop through each character in the string
for char in string:
if char in frequency:
frequency[char] += 1 # Increment count if character is already in dictionary
else:
frequency[char] = 1 # Initialize count if character is not in dictionary
# Print the dictionary with character frequencies
print("Character frequencies:", frequency)
Enter a string: hello
Character frequencies: {'h': 1, 'e': 1, 'l': 2, 'o': 1}
Create a dictionary of names and birthdays. Write a Python program that asks the user enters a name, and the program displays the birthday of
that person.
# Create a dictionary of names and birthdays
birthdays = {
"Alice": "January 15",
"Bob": "February 20",
"Charlie": "March 5",
"Diana": "April 10"
}
# Ask the user to enter a name
name = input("Enter a name to find their birthday: ")
# Check if the name exists in the dictionary
if name in birthdays:
print(f"{name}'s birthday is on {birthdays[name]}.")
else:
print(f"Sorry, we don't have the birthday information for {name}.")
Enter a name to find their birthday: Alice
Alice's birthday is on January 15.
Write a Python script to merge two Python dictionaries.
# Define two dictionaries
dict1 = {"a": 1, "b": 2}
dict2 = {"c": 3, "d": 4}
# Merge the dictionaries using the update() method
dict1.update(dict2)
# Print the merged dictionary
print("Merged Dictionary:", dict1)
Merged Dictionary: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Write a Python program to find maximum, minimum and sum of n numbers in a tuple.
n=int(input("Enter how many numbers..... "))
print("Enter numbers")
t=tuple()
for i in range(n):
x=int(input())
t=t+(x,)
print("minimum=",min(t))
print("maximum=",max(t))
print("sum=",sum(t))
https://colab.research.google.com/drive/1_-OZu7iQAbGIAnCmDAi7rnt50gYsXdGr#scrollTo=yUuiApGRS--W&printMode=true 2/3
1/22/25, 2:46 PM module 2 - Colab
Enter how many numbers..... 3
Enter numbers
23
24
25
minimum= 23
maximum= 25
sum= 72
https://colab.research.google.com/drive/1_-OZu7iQAbGIAnCmDAi7rnt50gYsXdGr#scrollTo=yUuiApGRS--W&printMode=true 3/3