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

Renuka Rai Python Project File

Uploaded by

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

Renuka Rai Python Project File

Uploaded by

renukarai702
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/ 3

1. Write a program in python to count the number of words in given string.

def count_words(sentence):
words = sentence.split()
return len(words)

# main
input_string = "Hello Everyone! I am Renuka Rai."
word_count = count_words(input_string)
print("Number of words:", word_count)

2. Write a program in python to find the maximum frequency character in string.

def max_frequency_char(string):
char_freq = {}

# Count frequencies
for char in string:
char_freq[char] = char_freq.get(char, 0) + 1

max_freq_char = max(char_freq, key=char_freq.get)

return max_freq_char

# main
string = "Phenomenon"
max_char = max_frequency_char(string)
print("The maximum frequency character in the string is:", max_char)

3. Write a program in python to print all even number in a list.

def print_even_numbers(list1):
for num in list1:
if num % 2 == 0:
print(num)

# main
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("Even numbers in the list:")
print_even_numbers(numbers)

4. Write a program in python to remove the last element from tuple.

def remove_last_element(input_tuple):
new_tuple = input_tuple[:-1]
return new_tuple

# main
original_tuple = (1,2,3,4)
print("Original tuple:", original_tuple)
modified_tuple = remove_last_element(original_tuple)
print("Tuple after removing the last element:", modified_tuple)

5. Write a program in python to find max and min k elements 8n tuple.

def max_min_k_elements(input_tuple, k):


#sorting tuple
sorted_tuple = sorted(input_tuple)

max_elements = sorted_tuple[-k:]

min_elements = sorted_tuple[:k]

return max_elements, min_elements

# main
input_tuple = (3, 1, 5, 2, 4,7,8)
k=3
max_elements, min_elements = max_min_k_elements(input_tuple, k)
print("The maximum 3 elements in the tuple are:", max_elements)
print("The minimum 3 elements in the tuple are:", min_elements)

6. Write a program in python to merge 2 dictionary.

def merge_dicts(dict1, dict2):


merged_dict = dict1.copy()
merged_dict.update(dict2)

return merged_dict

#main
dict1 = {'Summer': 1, 'Winter': 2}
dict2 = {'Spring': 3, 'Autumn': 4}

merged = merge_dicts(dict1, dict2)


print("Merged dictionary:", merged)
7. Write a program in python to check if given key already exist in dictionary.

def check_key_exists(dictionary, key):


if key in dictionary:
print(f"The key '{key}' exists in the dictionary.")
else:
print(f"The key '{key}' does not exist in the dictionary.")

#main
my_dict = {'a': 1, 'b': 2, 'c': 3,'d':4}

check_key_exists(my_dict, 'c')
check_key_exists(my_dict, 'j')

8. Write a program in python to swap elements in string list.

def swap_elements(string_list, index1, index2):


if 0 <= index1 < len(string_list) and 0 <= index2 < len(string_list):
string_list[index1], string_list[index2] = string_list[index2], string_list[index1]

#main
my_list = ['apple', 'banana', 'orange', 'grape']

print("Original list:", my_list)

swap_elements(my_list, 0, 3)

print("List after swapping:", my_list)

You might also like