1 .
Write a program to generate the pattern
rows = 5
cols = 5
for i in range(rows):
for j in range(cols):
if i == 0 or i == rows - 1 or j == 0 or j == cols - 1:
print("*", end="")
else:
print(" ", end="")
print()
size = 5
# Upper half of the rhombus
for i in range(size):
for j in range(size - i - 1):
print(" ", end="")
for j in range(2 * i + 1):
if j == 0 or j == 2 * i: # Print '*' only at the edges
print("*", end="")
else:
print(" ", end="")
print()
# Lower half of the rhombus
for i in range(size - 2, -1, -1):
for j in range(size - i - 1):
print(" ", end="")
for j in range(2 * i + 1):
if j == 0 or j == 2 * i:
print("*", end="")
else:
print(" ", end="")
print()
3 Write a program to take a list as input from the user and find all the numbers
● Greater than the average
● Lesser than the average
● Create a list of the above two set
3 Write a program to take a list as input from the user and find all the numbers
● Greater than the average
● Lesser than the average
● Create a list of the above two set
# Get the list of numbers from the user
num_list = [int(x) for x in input("Enter a list of numbers separated by spaces: ").split()]
# Calculate the average
avg = sum(num_list) / len(num_list)
# Find numbers greater than and less than the average
greater_than_avg = [num for num in num_list if num > avg]
less_than_avg = [num for num in num_list if num < avg]
# Create a list containing both sets of numbers
combined_list = greater_than_avg + less_than_avg
# Print the results
print("Numbers greater than average:", greater_than_avg)
print("Numbers less than average:", less_than_avg)
print("Combined list:", combined_list)
4 Write a program to implement bubble sort on a given range of numbers in a list
num = [10,2,30,4,21,12]
for i in range(0,len(num)-1):
for j in range(0,len(num)-i-1):
if num[j] > num[j + 1]:
temp = num[j]
num[j] = num[j + 1]
num[j +1] = temp
for x in num:
print(x)
5 Write a program to implement linear search on a given range of numbers in a list
# Get the list of numbers from the user
num_list = [int(x) for x in input("Enter a list of numbers separated by spaces: ").split()]
# Get the target value from the user
target_value = int(input("Enter the target value: "))
# Perform linear search
found = False
for i in range(len(num_list)):
if num_list[i] == target_value:
found = True
break
# Print the result
if found:
print("Target value found")
else:
print("Target value not found")
6 Write a program to create a list of strings and make a list which will have how many characters
are there in each string .
# Get the list of strings from the user
words = input("Enter a list of strings separated by spaces: ").split()
# Create a list to store the lengths of the strings
word_lengths = []
# Calculate the length of each string and append it to the list
for word in words:
word_lengths.append(len(word))
# Print the list of word lengths
print("List of word lengths:", word_lengths)
7 Write a program to create a list of strings and Count the number of lower case letters Count the number of upper case
letters Count the number of digits
# Get the list of strings from the user
strings = input("Enter a list of strings separated by spaces: ").split()
# Initialize counters
lower_count = 0
upper_count = 0
digit_count = 0
# Iterate through each string and count the characters
for string in strings:
for char in string:
if char.islower():
lower_count += 1
elif char.isupper():
upper_count += 1
elif char.isdigit():
digit_count += 1
# Print the results
print("Lowercase letters:", lower_count)
print("Uppercase letters:", upper_count)
print("Digits:", digit_count)
8 . Write a program to create a list of 10 years. Print all the years which are prime.
# Create an empty list to store the years
years = []
# Get 10 years from the user
for i in range(10):
year = int(input("Enter a year: "))
years.append(year)
# Print prime years
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
print("Prime years:")
for year in years:
if is_prime(year):
print(year)
Write a program to create a list of 10 numbers and print all the numbers which are
a. Even
b. Prime
# Create an empty list to store the numbers
numbers = []
# Get 10 numbers from the user
for i in range(10):
number = int(input("Enter a number: "))
numbers.append(number)
# Print even numbers
print("Even numbers:")
for number in numbers:
if number % 2 == 0:
print(number)
# Print prime numbers
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
print("Prime numbers:")
for number in numbers:
if is_prime(number):
print(number)
10 . Write a program to create a list of 10 octal numbers. Ask the user which of the numbers has to
be converted to binary and which has to be converted hexa and print the results
# Create a list of 10 octal numbers
octal_numbers = [123, 456, 701, 234, 567, 100, 345, 670, 127, 453]
# Ask the user which number to convert to binary
binary_index = int(input("Enter the index of the number to convert to binary (0-9): "))
# Ask the user which number to convert to hexadecimal
hexa_index = int(input("Enter the index of the number to convert to hexadecimal (0-9): "))
# Convert to binary
binary_number = bin(octal_numbers[binary_index])
# Convert to hexadecimal
hexa_number = hex(octal_numbers[hexa_index])
# Print the results
print("Binary:", binary_number)
print("Hexadecimal:", hexa_number)
11 1. Create a list of strings and then create a list which will be the length of the strings. Convert the
above two lists into a dictionary with key as the string and value as the string length
# Create empty lists for strings and lengths
strings = []
lengths = []
# Get strings from the user
num_strings = int(input("Enter the number of strings: "))
for i in range(num_strings):
string = input("Enter a string: ")
strings.append(string)
lengths.append(len(string))
# Create a dictionary from the two lists
string_lengths = dict(zip(strings, lengths))
# Print the dictionary
print(string_lengths)
12 Create a list of 3 digit numbers,create another list which will be the sum of the digits. Convert
the above two lists into a dictionary.
# Create empty lists for numbers and digit sums
numbers = []
digit_sums = []
# Get 3-digit numbers from the user
num_numbers = int(input("Enter the number of 3-digit numbers: "))
for i in range(num_numbers):
while True:
number = int(input("Enter a 3-digit number: "))
if 100 <= number <= 999:
break
else:
print("Invalid input. Please enter a 3-digit number.")
numbers.append(number)
# Calculate the sum of digits
sum = 0
temp = number
while temp > 0:
digit = temp % 10
sum += digit
temp //= 10
digit_sums.append(sum)
# Create a dictionary from the two lists
number_sums = dict(zip(numbers, digit_sums))
# Print the dictionary
print(number_sums)
13 Create a list of strings and then create a list which will be the length of the strings, sort the second list
in increasing order using any sorting algorithm. (Do not use the built in function).
# Create empty lists for strings and lengths
strings = []
lengths = []
# Get strings from the user
num_strings = int(input("Enter the number of strings: "))
for i in range(num_strings):
string = input("Enter a string: ")
strings.append(string)
lengths.append(len(string))
# Bubble sort to sort the lengths list in increasing order
for i in range(len(lengths) - 1):
for j in range(len(lengths) - i - 1):
if lengths[j] > lengths[j + 1]:
lengths[j], lengths[j + 1] = lengths[j + 1], lengths[j]
# Print the sorted list of lengths
print("Sorted lengths:", lengths)
14 Create a list of strings and then create a list which will be the length of the strings, find the
maximum length of the string in the second list using any search algorithm. (Do not use the
built in function)
# Create empty lists for strings and lengths
strings = []
lengths = []
# Get strings from the user
num_strings = int(input("Enter the number of strings: "))
for i in range(num_strings):
string = input("Enter a string: ")
strings.append(string)
lengths.append(len(string))
# Find the maximum length using linear search
max_length = lengths[0] # Initialize max_length with the first element
for length in lengths:
if length > max_length:
max_length = length
# Print the maximum length
print("Maximum length:", max_length)
15. Create a list of strings and identify all the strings which are palindromes and create a list of all
the palindrome strings.
# Create an empty list for strings
strings = []
# Get strings from the user
num_strings = int(input("Enter the number of strings: "))
for i in range(num_strings):
string = input("Enter a string: ")
strings.append(string)
# Identify palindromes and create a list
palindromes = []
for string in strings:
if string == string[::-1]:
palindromes.append(string)
# Print the list of palindromes
print("Palindromes:", palindromes)
16. Create a list of phone numbers. Write a program that identifies the phone numbers beginning
with a number given by the user .
# Create an empty list for phone numbers
phone_numbers = []
# Get phone numbers from the user
num_numbers = int(input("Enter the number of phone numbers: "))
for i in range(num_numbers):
number = input("Enter a phone number: ")
phone_numbers.append(number)
# Get the starting number from the user
start_number = input("Enter the starting number: ")
# Identify phone numbers beginning with the given number
matching_numbers = [number for number in phone_numbers if number.startswith(start_number)]
# Print the matching numbers
print("Matching numbers:", matching_numbers)
17. Write a program to create a list of 10 hexa numbers. Ask the user which of the numbers has
to be converted to binary and which has to be converted octal and print the results
# Create a list of 10 hexadecimal numbers
hexa_numbers = ["0x1A", "0x2B", "0x3C", "0x4D", "0x5E", "0x6F", "0x70", "0x81", "0x92", "0xA3"]
# Ask the user which number to convert to binary
binary_index = int(input("Enter the index of the number to convert to binary (0-9): "))
# Ask the user which number to convert to octal
octal_index = int(input("Enter the index of the number to convert to octal (0-9): "))
# Convert to binary
decimal_num = int(hexa_numbers[binary_index], 16)
binary_number = bin(decimal_num)
# Convert to octal
decimal_num = int(hexa_numbers[octal_index], 16)
octal_number = oct(decimal_num)
# Print the results
print("Binary:", binary_number)
print("Octal:", octal_number)
18. Write a program to create a list of strings. Create a list of all the strings that have vowel in it
# Create an empty list for strings
strings = []
# Get strings from the user
num_strings = int(input("Enter the number of strings: "))
for i in range(num_strings):
string = input("Enter a string: ")
strings.append(string)
# Create a list of strings with vowels
vowels = "aeiou"
strings_with_vowels = [string for string in strings if any(vowel in string for vowel in vowels)]
# Print the list of strings with vowels
print("Strings with vowels:", strings_with_vowels)
19. Write a program to create a list of strings, create a list of all the strings whose length are even.
# Create an empty list for strings
strings = []
# Get strings from the user
num_strings = int(input("Enter the number of strings: "))
for i in range(num_strings):
string = input("Enter a string: ")
strings.append(string)
# Create a list of strings with even lengths
even_length_strings = [string for string in strings if len(string) % 2 == 0]
# Print the list of strings with even lengths
print("Strings with even lengths:", even_length_strings)
20. Write a program to create a list of numbers.
a. Find the sum of all the numbers
b. Find the average of all numbers
c. Find the sum of numbers in a range given by the user.
d. Find the average of numbers in a range given by the user
# Create an empty list for numbers
numbers = []
# Get numbers from the user
num_numbers = int(input("Enter the number of numbers: "))
for i in range(num_numbers):
number = int(input("Enter a number: "))
numbers.append(number)
# a. Find the sum of all the numbers
total_sum = sum(numbers) # Use the built-in sum function
print("Sum of all numbers:", total_sum)
# b. Find the average of all numbers
average_of_numbers = total_sum / len(numbers)
print("Average of all numbers:", average_of_numbers)
# c. Find the sum of numbers in a range given by the user
start_index = int(input("Enter the starting index: "))
end_index = int(input("Enter the ending index: "))
range_sum = sum(numbers[start_index:end_index + 1]) # Use the built-in sum function
print("Sum of numbers in range:", range_sum)
# d. Find the average of numbers in a range given by the user
average_in_range = range_sum / (end_index - start_index + 1)
print("Average of numbers in range:", average_in_range)