Python Practice Sheet – Week 2 to Week 4
Week 2 - Q1: Triangle Pattern
Code:
for i in range(5, 0, -1):
for j in range(5 - i + 1):
print(i, end=' ')
print()
Example Output:
5
4 4
3 3 3
2 2 2 2
1 1 1 1 1
Week 2 - Q2: Character Type Checker
Code:
char = input("Enter a character: ")
if [Link]():
print("It is a digit.")
elif [Link]():
print("It is a lowercase letter.")
elif [Link]():
print("It is an uppercase letter.")
else:
print("It is a special character.")
Week 2 - Q3: Fibonacci Sequence
Code:
n = int(input("Enter number of terms: "))
a, b = 0, 1
count = 0
print("Fibonacci sequence:")
while count < n:
print(a, end=' ')
a, b = b, a + b
count += 1
Week 2 - Q4: Prime Numbers in Interval
Code:
start = int(input("Enter start of interval: "))
end = int(input("Enter end of interval: "))
print("Prime numbers:")
for num in range(start, end + 1):
if num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
print(num, end=' ')
Week 3 - Q1i: Convert List and Tuple to Array
Code:
import array
lst = [1, 2, 3, 4]
arr_from_list = [Link]('i', lst)
print("Array from list:", arr_from_list)
tup = (5, 6, 7, 8)
arr_from_tuple = [Link]('i', tup)
print("Array from tuple:", arr_from_tuple)
Week 3 - Q1ii: Common Values in Two Arrays
Code:
list1 = list(map(int, input("Enter elements of first array:
").split()))
list2 = list(map(int, input("Enter elements of second array:
").split()))
common = list(set(list1) & set(list2))
print("Common values:", common)
Week 3 - Q2: GCD Function
Code:
def gcd(a, b):
while b:
a, b = b, a % b
return a
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
print("GCD is:", gcd(x, y))
Week 3 - Q3: Palindrome Checker
Code:
def palindrome(s):
return s == s[::-1]
word = input("Enter a word: ")
print("Is palindrome?", palindrome(word))
Week 4 - Q1: is_sorted()
Code:
def is_sorted(lst):
return lst == sorted(lst)
lst = list(map(int, input("Enter list elements: ").split()))
print("Is sorted?", is_sorted(lst))
Week 4 - Q2: has_duplicates()
Code:
def has_duplicates(lst):
return len(lst) != len(set(lst))
lst = input("Enter elements separated by space: ").split()
print("Has duplicates?", has_duplicates(lst))
Week 4 - Q3i: remove_duplicates()
Code:
def remove_duplicates(lst):
return list(set(lst))
lst = input("Enter list elements: ").split()
print("List without duplicates:", remove_duplicates(lst))
Week 4 - Q3ii: Remove Single Letter Words
Code:
words = input("Enter text: ").split()
filtered = [word for word in words if len(word) > 1 and
[Link]() not in ['a', 'i']]
print("Without single-letter words:", filtered)
Week 4 - Q3iii: Invert Dictionary
Code:
def invert_dict(d):
return {v: k for k, v in [Link]()}
n = int(input("How many key-value pairs? "))
d = {}
for _ in range(n):
key = input("Key: ")
value = input("Value: ")
d[key] = value
print("Original dictionary:", d)
print("Inverted dictionary:", invert_dict(d))
Week 4 - Q4i: Add Commas to Word
Code:
word = input("Enter a word: ")
print("Comma separated:", ','.join(word))
Week 4 - Q4ii: Remove Word From String
Code:
text = input("Enter a string: ")
remove_word = input("Enter word to remove: ")
print("Updated string:", [Link](remove_word, ""))
Week 4 - Q4iii: Capitalize Words Manually
Code:
def custom_title(sentence):
result = ""
for word in [Link]():
if len(word) > 0:
first = word[0].upper()
rest = word[1:].lower()
result += first + rest + " "
return [Link]()
sentence = input("Enter a sentence: ")
print("Custom Capitalized:", custom_title(sentence))
Week 4 - Q5: Generate Binary Strings
Code:
def generate_binary(n, prefix=""):
if n == 0:
print(prefix)
else:
generate_binary(n - 1, prefix + "0")
generate_binary(n - 1, prefix + "1")
n = int(input("Enter the value of n: "))
print(f"Binary strings of length {n}:")
generate_binary(n)