1. Write a Python program to find the second smallest number in a list. input: second_smallest([1, 2, -8, -2, 0]) output : -2
list = [1, 2, -8, -2, 0]
print("The List of Elements are", list)
smallest = min(list)
list.remove(smallest)
second_smallest = min(list)
print("The Second Smallest Element in the List is",second_smallest)
print("The Removed(the first smallest number) element is",smallest)
The List of Elements are [1, 2, -8, -2, 0]
The Second Smallest Element in the List is -2
The Removed(the first smallest number) element is -8
  1. Write a Python program to change a given string to a new string where the first and last chars have been exchanged
string = input("Enter the string")
print("Before character exchange :",string)
result = string[-1:] + string[1:-1] + string[:1]
print("After character exchange :",result)
Enter the stringPythonLanguage
Before character exchange : PythonLanguage
After character exchange : eythonLanguagP
  1. Write a Python function that takes a list of words and returns the length of the longest one
def longest_word(words):
    longest_length = 0
    longest_word = 0
    for word in words:
        if len(word) > longest_length:
            longest_length = len(word)
            longest_word = word
    return longest_length, longest_word
word_list = ["kiwi", "banana", "cherry", "watermelon"]
result_length, result_word = longest_word(word_list)
print("Longest word length:", result_length)
print("Longest word:", result_word)
Longest word length: 10
Longest word: watermelon
  1. Write a Python program to remove the nth index character from a nonempty string
string=input("Enter the string:")
n=int(input("Enter the index of the character to remove:"))
removed_character=string[n]
modified_string = string[:n] + string[n+1:]
print("Modified string after removing a character:",modified_string)
print("Removed character is",removed_character)
Enter the string:Python Programming
Enter the index of the character to remove:9
Modified string after removing a character: Python Prgramming
Removed character is o
  1. Check if a given key already exists in a dictionary input: d = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60} is_key_present(5) is_key_present(9)

output:

Key is present in the dictionary
Key is not present in the dictionary

# Answer 5
def is_key_present(dictionary,key):
  if key in dictionary:
    return ("Key is present in the dictionary")
  else:
    return ("Key is not present in the dictionary")

d={1:10,2:20,3:30,4:40,5:50,6:60}
key1 = 5
key2 = 9
result1 = is_key_present(d,key1)
result2 = is_key_present(d,key2)
print(result1)
print(result2)
Key is present in the dictionary
Key is not present in the dictionary