Write a short note on data types in Python.
● Python has several built-in data types:
○ Numeric: Includes int, float, and complex.
○ Sequence: Includes list, tuple, and range.
○ Text: str (string).
○ Mapping: dict (dictionary).
○ Set: set and frozenset.
○ Boolean: bool.
○ Binary: bytes, bytearray, and memoryview.
Write a short note on exception handling.
● Exception handling in Python is done using the try, except, else, and
finally blocks. It allows programmers to handle errors gracefully and
execute code under specific conditions.
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
else:
print("Division successful.")
finally:
print("Execution finished.")
What is a module? What is a package? Explain with examples.
● A module is a single file containing Python code (functions, variables,
classes). For example, [Link] is a module with mathematical functions.
● A package is a collection of modules in a directory. It must contain an
__init__.py file. For example, a package named mypackage might contain
[Link], [Link], etc.
# Example of importing a module
import math
print([Link](16)) # Output: 4.0
Write a recursive function in Python to display the addition of digits in a
single digit.
def add_digits(n):
if n < 10:
return n
else:
return add_digits(sum(int(digit) for digit in str(n)))
result = add_digits(9875)
print(result) # Output: 2 (9+8+7+5 = 29 -> 2+9 = 11 -> 1+1 = 2)
Write a program in Python to accept 'n' integers in a list, compute & display
the addition of all squares of these integers
n = int(input("Enter the number of integers: "))
integers = [int(input(f"Enter integer {i+1}: ")) for i in range(n)]
sum_of_squares = sum(i**2 for i in integers)
print(f"The sum of squares is: {sum_of_squares}")
Write a Python program to count all occurrences of "India" and "Country" in
a text file "[Link]".
def count_occurrences(filename, word):
with open(filename, 'r') as file:
content = [Link]()
return [Link]().count([Link]())
india_count = count_occurrences('[Link]', 'India')
country_count = count_occurrences('[Link]', 'Country')
print(f"Occurrences of 'India': {india_count}, Occurrences of 'Country':
{country_count}")
Write a program to get a single string from two given strings, separated by
space, and swap the first two characters of each string.
def swap_strings(str1, str2):
swapped_str1 = str2[:2] + str1[2:] # Swap first two characters
swapped_str2 = str1[:2] + str2[2:]
return swapped_str1 + ' ' + swapped_str2
result = swap_strings('abc', 'pqr')
print(result) # Output: pqc abr
Write a program to display the power of 2 up to a given number using an
anonymous function.
power_of_two = lambda n: [2**i for i in range(n + 1)]
n=2
powers = power_of_two(n)
for i in range(len(powers)):
print(f"2 raised to {i} is {powers[i]}")
# Output:
# 2 raised to 0 is 1
# 2 raised to 1 is 2
# 2 raised to 2 is 4
Write a program to read an entire text file.
with open('[Link]', 'r') as file: # Use the correct filename
content = [Link]()
print(content) # Prints the entire content of the file
Write a Python program to count vowels and consonants in a string.
def count_vowels_consonants(s):
vowels = "aeiouAEIOU"
count_v = sum(1 for char in s if char in vowels)
count_c = sum(1 for char in s if [Link]() and char not in vowels)
return count_v, count_c
string = "Hello World"
vowels_count, consonants_count = count_vowels_consonants(string)
print(f"Vowels: {vowels_count}, Consonants: {consonants_count}")
Write a Python script to print a dictionary where the keys are numbers
between 1 and 15 (both included) and the values are the squares of the keys.
squares_dict = {i: i**2 for i in range(1, 16)}
print(squares_dict)
# Output: {1: 1, 2: 4, 3: 9, ..., 15: 225}
Write a Python function that accepts a string and counts the number of upper
and lower case letters.
def count_case(s):
upper_count = sum(1 for char in s if [Link]())
lower_count = sum(1 for char in s if [Link]())
return upper_count, lower_count
string = "Hello World"
upper_count, lower_count = count_case(string)
print(f"Upper case: {upper_count}, Lower case: {lower_count}")