Assignment 2
Assignment 2
# Q1. MCQs:
1. (B) range()
2. (A) in
3. (A) Nested Loop
4. (B) line 2 - because the range(10,1,-1) will never reach condition
# 2. Pattern printing:
def print_star_pattern():
for i in range(5, 0, -1):
print("*" * i)
# Assignment 3: Strings
# Q1. MCQs:
1. (C) string
2. (A) 0
3. (B) subscript
4. (D) False
# Q2. Brief answers:
# 4. String functions:
def string_function_examples():
# islower()
print("hello".islower()) # True
# title()
print("hello world".title()) # Hello World
# split()
print("hello world python".split()) # ['hello', 'world', 'python']
# Q3. Programs:
# Q1. MCQs:
"""
1. (D) 'o'
2. (D) None of these
3. (C) insert()
4. (C) pop()
"""
# 4. List functions:
def list_function_examples():
numbers = [1, 2, 3, 4, 5]
# pop()
numbers.pop() # removes last element
# remove()
numbers.remove(2) # removes first occurrence of 2
# del
del numbers[0] # removes element at index 0
# 5. Creating List & Tuple:
def create_collections():
# List creation
list1 = [] # empty list
list2 = [1, 2, 3] # list with elements
# Tuple creation
tuple1 = () # empty tuple
tuple2 = (1, 2, 3) # tuple with elements
# Q3. Programs:
# 8. Reverse list
def reverse_list(lst):
return lst[::-1]