Python Programming – Module 2 Important
Questions (8 Marks Answers)
Q1. Explain Python string operations like slicing, concatenation, repetition, and comparison with
examples.
Answer:
Python provides several operations on strings such as slicing, concatenation, repetition, and
comparison. These operations help in manipulating and analyzing text data effectively.
Slicing: Slicing is used to extract a part of a string using index positions. The starting index is
included and the ending index is excluded.
s = "Python"
print(s[0:4])
Output: Pyth
Concatenation: Concatenation means joining two or more strings using the '+' operator.
a = "Hello"
b = "World"
print(a + " " + b)
Output: Hello World
Repetition: Repetition is used to repeat a string multiple times using the '*' operator.
print("Hi " * 3)
Output: Hi Hi Hi
Comparison: Strings can be compared using comparison operators such as ==, <, and >. The
comparison is based on ASCII/Unicode values.
print("apple" < "banana")
Output: True
Q2. Write a Python program to count the number of words in a given string.
Answer:
The number of words in a string can be counted by splitting the string into words using the split()
function and then finding the length of the resulting list.
s = input("Enter a string: ")
words = [Link]()
print("Number of words:", len(words))
Example Output:
Enter a string: Python is simple
Number of words: 3
Q3. Describe mutability of lists. Show how list modification differs from list copying.
Answer:
Lists in Python are mutable, which means their elements can be changed after creation. Mutability
allows modification of existing elements without creating a new list.
List Modification: Directly changing an element modifies the original list.
a = [1, 2, 3]
a[0] = 10
print(a)
Output: [10, 2, 3]
List Copying: Copying creates a separate list so changes do not affect the original.
b = [Link]()
b[1] = 20
print(a)
print(b)
Output:
[10, 2, 3]
[10, 20, 3]
Q4. Write a Python program to check if a string is a palindrome using slicing.
Answer:
A palindrome is a string that reads the same forward and backward. Using slicing, a string can be
reversed and compared with the original string.
s = input("Enter string: ")
if s == s[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
Example Output:
Enter string: madam
Palindrome
Q5. Define Python lists and demonstrate how to insert, delete and search elements in a list.
Answer:
A Python list is an ordered and mutable collection of elements. Lists allow insertion, deletion, and
searching of elements easily.
lst = [10, 20, 30]
# Insert
[Link](40)
# Delete
[Link](20)
# Search
print(30 in lst)
print(lst)
Output:
True
[10, 30, 40]
Q6. Write a program to take a list of numbers and return a new list containing only even numbers.
Answer:
This program checks each number in the list and adds only even numbers to a new list using a loop
and conditional statement.
lst = [1, 2, 3, 4, 5, 6]
even = []
for i in lst:
if i % 2 == 0:
[Link](i)
print("Even numbers:", even)
Output:
Even numbers: [2, 4, 6]
Q7. Explain the differences between lists and tuples with an example Python code.
Answer:
Lists and tuples are both sequence data types in Python, but lists are mutable while tuples are
immutable. Lists use square brackets whereas tuples use parentheses.
lst = [1, 2, 3]
tup = (1, 2, 3)
lst[0] = 10
print(lst)
print(tup)
Output:
[10, 2, 3]
(1, 2, 3)
Q8. Using Python, write a program to remove duplicates from a list without using set().
Answer:
Duplicates can be removed by checking each element before adding it to a new list. If the element
is not already present, it is added.
lst = [1, 2, 2, 3, 4, 4]
new = []
for i in lst:
if i not in new:
[Link](i)
print("List without duplicates:", new)
Output:
List without duplicates: [1, 2, 3, 4]