Leela Soft 1000 MCQ Programs Madhu
Program Explanation
1. User must enter a string and store it in a variable.
2. Both the count variables are initialized to zero.
3. The for loop is used to traverse through the characters in the string.
4. The first count variable is incremented each time a digit is encountered and the second
count variable is incremented each time a character is encountered.
5. The total count of digits and characters in the string are printed.
Runtime Test Cases
Case 1:
Enter string:Hello123
The number of digits is:
3
The number of characters is:
8
Case 2:
Enter string:Abc12
The number of digits is:
2
The number of characters is:
5
Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given
String
Problem Description
The program takes a string and forms a new string made of the first 2 characters and last 2
characters from a given string.
Problem Solution
1. Take a string from the user and store it in a variable.
2. Initialize a count variable to 0.
3. Use a for loop to traverse through the characters in the string and increment the count
variable each time.
4. Form the new string using string slicing.
5. Print the newly formed string.
6. Exit.
Program/Source Code
Here is source code of the Python Program to form a new string made of the first 2 characters
and last 2 characters from a given string. The program output is also shown below.
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
string=input("Enter string:")
count=0
for i in string:
count=count+1
new=string[0:2]+string[count-2:count]
print("Newly formed string is:")
print(new)
Program Explanation
1. User must enter a string and store it in a variable.
2. The count variable is initialized to zero.
3. The for loop is used to traverse through the characters in the string.
4. The count is incremented each time a character is encountered.
5. The new string is formed by using string slicing and the first two letters and the last two
letters of the string are concatenated using the ‘+’ operator.
6. The newly formed string is printed.
Runtime Test Cases
Case 1:
Enter string:Hello world
Newly formed string is:
Held
Case 2:
Enter string:Checking
Newly formed string is:
Chng
Python Program to Count the Occurrences of Each Word in a Given String Sentence
Problem Description
The program takes a string and counts the occurrence of each word in the given sentence.
Problem Solution
1. Take a string and a word from the user and store it in separate variables.
2. Initialize a count variable to 0.
3. Split the string using space as the reference and store the words in a list.
4. Use a for loop to traverse through the words in the list and use an if statement to check
if the word in the list matches the word given by the user and increment the count.
5. Print the total count of the variable.
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
6. Exit.
Program/Source Code
Here is source code of the Python Program to calculate the length of a string without using library
functions. The program output is also shown below.
string=input("Enter string:")
word=input("Enter word:")
a=[]
count=0
a=string.split(" ")
for i in range(0,len(a)):
if(word==a[i]):
count=count+1
print("Count of the word is:")
print(count)
Program Explanation
1. User must enter a string and a word and store it in separate variables.
2. The count variable is initialized to zero.
3. The string is split into words using space as the reference and stored in a list.
4. The for loop is used to traverse through the words in the list.
5. The count is incremented each time the word in the list is equal to the word given by the
user.
6. The total count of the word is printed.
Runtime Test Cases
Case 1:
Enter string:hello world
Enter word:hello
Count of the word is:
1
Case 2:
Enter string:orange blue red orange
Enter word:orange
Count of the word is:
2
Python Program to Check if a Substring is Present in a Given String
Problem Description
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
The program takes a string and checks if a substring is present in the given string.
Problem Solution
1. Take a string and a substring from the user and store it in separate variables.
2. Check if the substring is present in the string using find() in-built function.
3. Print the final result.
4. Exit.
Program/Source Code
Here is source code of the Python Program to calculate the length of a string without using library
functions. The program output is also shown below.
string=input("Enter string:")
sub_str=input("Enter word:")
if(string.find(sub_str)==-1):
print("Substring not found in string!")
else:
print("Substring in string!")
Program Explanation
1. User must enter a string and a substring from the user and store it in separate variables.
2. It is then checked if the substring is present in the string using find() in-built function.
3. An if statement is used to make the decision and the final result is printed.
Runtime Test Cases
Case 1:
Enter string:Hello world
Enter word:world
Substring in string!
Case 2:
Enter string:Hello world
Enter word:apple
Substring not found in string!
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
5. Python Programming Examples on Dictionary
Python Program to Add a Key-Value Pair to the Dictionary
Problem Description
The program takes a key-value pair and adds it to the dictionary.
Problem Solution
1. Take a key-value pair from the user and store it in separate variables.
2. Declare a dictionary and initialize it to an empty dictionary.
3. Use the update() function to add the key-value pair to the dictionary.
4. Print the final dictionary.
5. Exit.
Program/Source Code
Here is source code of the Python Program to add a key-value pair to a dictionary. The program
output is also shown below.
key = int(input("Enter the key (int) to be added:"))
value = int(input("Enter the value for the key to be added:"))
d = {}
d.update({key:value})
print("Updated dictionary is:")
print(d)
Program Explanation
1. User must enter a key-value pair and store it in separate variables.
2. A dictionary is declared and initialized to an empty dictionary.
3. The update() function is used to add the key-value pair to the dictionary.
4. The final dictionary is printed.
Runtime Test Cases
Case 1:
Enter the key (int) to be added:12
Enter the value for the key to be added:34
Updated dictionary is:
{12: 34}
Case 2:
Enter the key (int) to be added:34
Enter the value for the key to be added:29
Updated dictionary is:
{34: 29}
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Python Program to Concatenate Two Dictionaries Into One
Problem Description
The program takes two dictionaries and concatenates them into one dictionary.
Problem Solution
1. Declare and initialize two dictionaries with some key-value pairs
2. Use the update() function to add the key-value pair from the second dictionary to the first
dictionary.
3. Print the final dictionary.
4. Exit.
Program/Source Code
Here is source code of the Python Program to add a key-value pair to a dictionary. The program
output is also shown below.
d1 = {'A':1, 'B':2}
d2 = {'C':3}
d1.update(d2)
print("Concatenated dictionary is:")
print(d1)
Program Explanation
1. User must enter declare and initialize two dictionaries with a few key-value pairs and
store it in separate variables.
2. The update() function is used to add the key-value pair from the second to the first
dictionary.
3. The final updated dictionary is printed.
Runtime Test Cases
Case 1:
Concatenated dictionary is:
{'A': 1, 'C': 3, 'B': 2}
Python Program to Check if a Given Key Exists in a Dictionary or Not
Problem Description
The program takes a dictionary and checks if a given key exists in a dictionary or not.
Problem Solution
1. Declare and initialize a dictionary to have some key-value pairs.
2. Take a key from the user and store it in a variable.
3. Using an if statement and the in operator, check if the key is present in the dictionary
using the dictionary.keys() method.
4. If it is present, print the value of the key.
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
5. If it isn’t present, display that the key isn’t present in the dictionary.
6. Exit.
Program/Source Code
Here is source code of the Python Program to check if a given key exists in a dictionary or not.
The program output is also shown below.
d = {'A':1, 'B':2, 'C':3}
key = input("Enter key to check:")
if key in d.keys():
print("Key is present and value of the key is:")
print(d[key])
else:
print("Key isn't present!")
Program Explanation
1. User must enter the key to be checked and store it in a variable.
2. An if statement and the in operator is used check if the key is present in the list containing
the keys of the dictionary.
3. If it is present, the value of the key is printed.
4. If it isn’t present, “Key isn’t present!” is printed.
5. Exit.
Runtime Test Cases
Case 1:
Enter key to check:A
Key is present and value of the key is:
1
Case 2:
Enter key to check:F
Key isn't present!
Python Program to Generate a Dictionary that Contains Numbers (between 1 and n) in the
Form (x,x*x).
Problem Description
The program takes a number from the user and generates a dictionary that contains numbers
(between 1 and n) in the form (x,x*x).
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Problem Solution
1. Take a number from the user and store it in a separate variable.
2. Declare a dictionary and using dictionary comprehension initialize it to values keeping the
number between 1 to n as the key and the square of the number as their values.
3. Print the final dictionary.
4. Exit.
Program/Source Code
Here is source code of the Python Program to generate a dictionary that contains numbers
(between 1 and n) in the form (x,x*x). The program output is also shown below.
n = int(input("Enter a number:"))
d = {x:x * x for x in range(1, n + 1)}
print(d)
Program Explanation
1. User must enter a number and store it in a variable.
2. A dictionary is declared and initialized to values using dictionary comprehension.
3. The numbers between 1 to n are kept as keys while the squares of the numbers are made
their values.
4. The final dictionary is printed.
Runtime Test Cases
Case 1:
Enter a number:5
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Case 2:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196,
15: 225, 16: 256, 17: 289, 18: 324, 19: 361}
Python Program to Sum All the Items in a Dictionary
Problem Description
The program takes a dictionary and prints the sum of all the items in the dictionary.
Problem Solution
1. Declare and initialize a dictionary to have some key-value pairs.
2. Find the sum of all the values in the dictionary.
3. Print the total sum.
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
4. Exit.
Program/Source Code
Here is source code of the Python Program to find the sum all the items in a dictionary. The
program output is also shown below.
d = {'A':100, 'B':540, 'C':239}
print("Total sum of values in the dictionary:")
print(sum(d.values()))
Program Explanation
1. The sum() function is used to find the sum of all the values in the dictionary.
2. The total sum of all the values in the dictionary is printed.
3. Exit.
Runtime Test Cases
Case 1:
Total sum of values in the dictionary:
879
Python Program to Multiply All the Items in a Dictionary
Problem Description
The program takes a dictionary and multiplies all the items in the dictionary.
Problem Solution
1. Declare and initialize a dictionary to have some key-value pairs.
2. Initialize a variable that should contain the total multiplied value to 1.
3. Use the for loop to traverse through the values of the dictionary.
4. Then multiply all the values in the dictionary against each other.
5. Print the total multiplied value.
6. Exit.
Program/Source Code
Here is source code of the Python Program to multiply all the items in a dictionary. The program
output is also shown below.
d = {'A':10, 'B':10, 'C':239}
total = 1
for i in d:
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
total = total * d[i]
print(total)
Program Explanation
1. A dictionary is declared and initialized to have some key-value pairs.
2. The total variable is initialized to 1.
3. The for loop is used to traverse through the values of the dictionary.
4. Then all the values in the dictionary are multiplied against each other.
5. The total multiplied value is printed.
Runtime Test Cases
Case 1:
23900
Python Program to Remove the Given Key from a Dictionary
Problem Description
The program takes a dictionary and removes a given key from the dictionary.
Problem Solution
1. Declare and initialize a dictionary to have some key-value pairs.
2. Take a key from the user and store it in a variable.
3. Using an if statement and the in operator, check if the key is present in the dictionary.
4. If it is present, delete the key-value pair.
5. If it isn’t present, print that the key isn’t found and exit the program.
6. Exit.
Program/Source Code
Here is source code of the Python Program to remove the given key from a dictionary. The
program output is also shown below.
d = {'a':1, 'b':2, 'c':3, 'd':4}
print("Initial dictionary")
print(d)
key = input("Enter the key to delete(a-d):")
if key in d:
del d[key]
else:
print("Key not found!")
exit(0)
print("Updated dictionary")
print(d)
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Program Explanation
1. User must enter the key to be checked and store it in a variable.
2. An if statement and the in operator is used check if the key is present in the dictionary.
3. If it is present, the key-value pair is deleted.
4. If it isn’t present, “Key not found!” is printed and the program is exited.
Runtime Test Cases
Case 1:
Initial dictionary
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
Enter the key to delete(a-d):c
Updated dictionary
{'a': 1, 'b': 2, 'd': 4}
Case 2:
Initial dictionary
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
Enter the key to delete(a-d):g
Key not found!
Python Program to Form a Dictionary from an Object of a Class
Problem Description
The program forms a dictionary from an object of a class.
Problem Solution
1. Declare a class named A.
2. Initialize the keys with their values in the __init__ method of the class and form a
dictionary using the __dict__ method.
3. Print the dictionary formed from the object of the class.
4. Exit.
Program/Source Code
Here is source code of the Python Program to form a dictionary from an object of a class. The
program output is also shown below.
class A(object):
def __init__(self):
self.A = 1
self.B = 2
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
obj = A()
print(obj.__dict__)
Program Explanation
1. A class named A is declared.
2. The keys are initialized with their values in the __init__ method of the class.
3. The dictionary is formed using the object of the class and by using the __dict__ method.
4. The dictionary formed from the object of the class is printed.
Runtime Test Cases
Case 1:
{'A': 1, 'B': 2}
Python Program to Map Two Lists into a Dictionary
Problem Description
The program takes two lists and maps two lists into a dictionary.
Problem Solution
1. Declare two empty lists and initialize them to an empty list.
2. Consider a for loop to accept values for the two lists.
3. Take the number of elements in the list and store it in a variable.
4. Accept the values into the list using another for loop and insert into the list.
5. Repeat 4 and 5 for the values list also.
6. Zip the two lists and use dict() to convert it into a dictionary.
7. Print the dictionary.
8. Exit.
Program/Source Code
Here is source code of the Python Program to map two lists into a dictionary. The program output
is also shown below.
keys = []
values = []
n = int(input("Enter number of elements for dictionary:"))
print("For keys:")
for x in range(0, n):
element = int(input("Enter element" + str(x + 1) + ":"))
keys.append(element)
print("For values:")
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
for x in range(0, n):
element = int(input("Enter element" + str(x + 1) + ":"))
values.append(element)
d = dict(zip(keys, values))
print("The dictionary is:")
print(d)
Program Explanation
1. User must enter the number of elements in the list and store it in a variable.
2. User must enter the values to the same number of elements into the list.
3. The append function obtains each element from the user and adds the same to the end
of the list as many times as the number of elements taken.
4. The same of 2 and 3 is done for the second values list also.
5. The two lists are merged together using the zip() function.
6. The zipped lists are then merged to form a dictionary using dict().
7. The dictionary formed from the two lists is then printed.
Runtime Test Cases
Case 1:
Enter number of elements for dictionary:3
For keys:
Enter element1:1
Enter element2:2
Enter element3:3
For values:
Enter element1:1
Enter element2:4
Enter element3:9
The dictionary is:
{1: 1, 2: 4, 3: 9}
Case 2:
Enter number of elements for dictionary:2
For keys:
Enter element1:23
Enter element2:46
For values:
Enter element1:69
Enter element2:138
The dictionary is:
{46: 138, 23: 69}
www.leelasoft.com Cell: 78 42 66 47 66