0% found this document useful (0 votes)
4 views

Assignment 2

The document contains assignments focused on loops, strings, and lists/tuples in Python. It includes multiple-choice questions, brief explanations, and various programming examples for each topic. Key concepts covered include for loops, nested loops, string manipulation, list operations, and functions for handling collections.

Uploaded by

therealaditi20
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment 2

The document contains assignments focused on loops, strings, and lists/tuples in Python. It includes multiple-choice questions, brief explanations, and various programming examples for each topic. Key concepts covered include for loops, nested loops, string manipulation, list operations, and functions for handling collections.

Uploaded by

therealaditi20
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

# Assignment 2: Loops

# 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

# Q2. Brief answers:

# 1. For loop explanation:


Syntax:
for variable in sequence:
statements
Example:"""
def multiplication_table(n):
for i in range(1, 11):
print(f"{n} x {i} = {n*i}")

# 2. Nested loop explanation:


A nested loop is a loop inside another loop.
Example:
def multiplication_tables(start, end):
for i in range(start, end+1):
for j in range(1, 11):
print(f"{i} x {j} = {i*j}")
print()
# Q3. Programs:

# 1. Fibonacci series using while loop:


def fibonacci_series():
terms = int(input("Enter number of terms: "))
a, b = 0, 1
count = 0
while count < terms:
print(a, end=" ")
c=a+b
a=b
b=c
count += 1

# 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:

# 1. Python string explanation:


"""
A string is a sequence of characters enclosed in single ('') or double ("") quotes
"""

# 2. String index explanation:


"""
Each character in a string has a position (index) starting from 0
Example: In "Hello", H is at index 0, e at 1, etc.
"""

# 3. + and * operators in strings:


"""
+ concatenates strings: "Hello" + "World" = "HelloWorld"
* repeats strings: "Hi" * 3 = "HiHiHi"
"""

# 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']

# 5. String slicing examples:


def string_slicing_examples():
text = "Python"
print(text[0:2]) # Py
print(text[2:]) # thon
print(text[:3]) # Pyt
print(text[-2:]) # on

# Q3. Programs:

# 1. Reverse string without loop:


def reverse_string(s):
return s[::-1]

# 2. Count characters in string:


def count_characters(s):
return len(s)

# Assignment 4: Lists & Tuples

# Q1. MCQs:
"""
1. (D) 'o'
2. (D) None of these
3. (C) insert()
4. (C) pop()
"""

# Q2. Brief answers:

# 1. List and Tuple explanation:


"""
List: Ordered, mutable collection of items
Tuple: Ordered, immutable collection of items
"""

# 2. Difference between List and Tuple:


"""
- Lists are mutable, tuples are immutable
- Lists use [], tuples use ()
- Lists have more built-in methods
"""

# 3. Difference between append() and extend():


"""
append() adds one element at end: list.append(5)
extend() adds multiple elements: list.extend([5,6,7])
"""

# 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:

# 1. Find first and last item


def first_last_items(lst):
return lst[0], lst[-1]

# 2. Display list contents with separator


def display_with_separator(lst, sep):
print(sep.join(map(str, lst)))

# 3. Create list of 5 names


def create_name_list():
names = []
for i in range(5):
names.append(input(f"Enter name {i+1}: "))
return names

# 4. Find largest and smallest number


def find_minmax(numbers):
return min(numbers), max(numbers)

# 5. Insert number at index


def insert_at_index(lst, num, index):
lst.insert(index, num)
return lst

# 6. Add element at end


def add_at_end():
lst = []
# Using insert
lst.insert(len(lst), int(input("Enter number: ")))
# Using append
lst.append(int(input("Enter number: ")))
return lst

# 7. Interchange first and last


def interchange_ends(lst):
lst[0], lst[-1] = lst[-1], lst[0]
return lst

# 8. Reverse list
def reverse_list(lst):
return lst[::-1]

# 9. Check element presence


def check_element(lst, element):
return element in lst
# 10. Sum of numbers
def sum_of_numbers(lst):
return sum(lst)

# 11. Merge two lists


def merge_lists(lst1, lst2):
return lst1 + lst2

# 12. Count elements without function


def count_elements(lst):
count = 0
for _ in lst:
count += 1
return count

# 13. Fibonacci series in list


def fibonacci_list(n):
fib = [0, 1]
for i in range(2, n):
fib.append(fib[i-1] + fib[i-2])
return fib

# 14. Remove specific item


def remove_item(lst, item):
lst.remove(item)
return lst

# 15. Split list into odd/even indices


def split_odd_even_indices(lst):
return lst[::2], lst[1::2]

You might also like