Extra Questions
Extra Questions
Tuple
7. Count the occurrences of an element
in a tuple t1 = (1, 2, 3, 4, 5)
t2 = (4, 5, 6, 7)
t = (1, 2, 3, 4, 2, 5, 2) difference = tuple(set(t1) - set(t2))
print("Count of 2:", t.count(2)) print("Difference:", difference)
print("Programmed by Muskan Kateja, Roll print("Programmed by Muskan Kateja, Roll
No. 17, CO6IA") No. 17, CO6IA")
Dictionary
13. Create a dictionary with default
values using the dict.fromkeys() method
String
s = "python programming is fun"
19. Python program to sort words in words = s.split()
alphabetical order
words.sort()
print("Sorted words:", " ".join(words)) def is_palindrome(s):
print("Programmed by Muskan Kateja, Roll return s == s[::-1]
No. 17, CO6IA") s = "madam"
print("Palindrome" if is_palindrome(s) else
"Not a palindrome")
print("Programmed by Muskan Kateja, Roll
20. Python program to remove No. 17, CO6IA")
punctuation from a string
import string
s = "Hello, World! How's it going?"
clean = "".join(c for c in s if c not in 23. Python program to reverse words
string.punctuation) in a string while keeping character
print("Without punctuation:", clean) order
print("Programmed by Muskan Kateja, Roll s = "hello world"
No. 17, CO6IA") reversed_words = " ".join(word[::-1] for
word in s.split())
print("Reversed words:", reversed_words)
print("Programmed by Muskan Kateja, Roll
21. Python program to generate a No. 17, CO6IA")
random string
import random, string
length = 8
rand_str = 24. Python program to find all
''.join(random.choices(string.ascii_letters + occurrences of a substring and return
string.digits, k=length)) starting indices
print("Random string:", rand_str)
s = "ababcabcab"
print("Programmed by Muskan Kateja, Roll
sub = "abc"
No. 17, CO6IA")
indices = [i for i in range(len(s)) if
s.startswith(sub, i)]
print("Occurrences at indices:", indices)
print("Programmed by Muskan Kateja, Roll
22. Python function to check whether No. 17, CO6IA")
a given string is a palindrome or not
List
list1 = [1, 2, 3, 4]
25. Python program to compare two list2 = [1, 2, 3, 4]
lists
print("Lists are equal" if list1 == list2 else list1 = [1, 2, 2, 3, 4, 4, 5]
"Lists are not equal") s = set(list1)
print("Programmed by Muskan Kateja, Roll print("Set:", s)
No. 17, CO6IA") print("Programmed by Muskan Kateja, Roll
No. 17, CO6IA")
IMP PROGRAMS
print(num, end=" ")
37. Python program to display the num += 2
pattern using loops print()
rows = [1, 3, 5] print("Programmed by Muskan Kateja, Roll
num = 2 No. 17, CO6IA")
for r in rows:
for col in range(r):
40. Output of the following Python
code
indices = ['zero', 'one', 'two', 'three', 'four',
'five']
38. Python program to print the print(indices[:4])
triangle pattern using loops print(indices[-2:])
for i in range(1, 5): print("Programmed by Muskan Kateja, Roll
for j in range(1, i + 1): No. 17, CO6IA")
print(j, end=" ")
print()
print("Programmed by Muskan Kateja, Roll
No. 17, CO6IA") 41. Python program with parent and
child class using method overriding
class Animals:
def feed(self):
print("Animals eat food")
39. Output of the following code class Herbivorous(Animals):
def feed(self):
i)a = [2, 5, 1, 3, 6, 9, 7] print("Herbivorous eat plants")
a[2:6] = [2, 4, 9, 0] h = Herbivorous()
print(a) h.feed()
print("Programmed by Muskan Kateja, Roll print("Programmed by Muskan Kateja, Roll
No. 17, CO6IA") No. 17, CO6IA")
def display(self):
print(self.name, self.roll, self.dept,
self.mobile) print("Programmed by Muskan Kateja, Roll
No. 17, CO6IA")
s = Student("Muskan", 17, "CO",
"9876543210")
s.display()