Q1(a) Explain the concept of type conversion in Python.
Differentiate
between implicit and explicit conversion with examples.
Type Conversion in Python
Type conversion is the process of converting one data type into another
data type. Python supports two types of conversion:
1. Implicit Type Conversion
2. Explicit Type Conversion
1. Implicit Type Conversion
In implicit conversion, Python automatically converts one data type into
another without user intervention.
Example
x = 10
y = 2.5
z = x + y
print(z)
print(type(z))
Output
12.5
<class 'float'>
Explanation
Python automatically converts integer x into float before addition.
2. Explicit Type Conversion
In explicit conversion, the programmer manually converts one data type
into another using functions such as:
int()
float()
str()
Example
x = "25"
y = int(x)
print(y)
print(type(y))
Output
25
<class 'int'>
Difference Between Implicit and Explicit Conversion
Implicit Conversion Explicit Conversion
Done automatically by Done manually by
Python programmer
No conversion function Conversion functions
required required
Safer and automatic Programmer controls
conversion
Example: int to float Example: string to int
Q2(b) Develop a program that prints all
numbers from 1 to 100 that are divisible by
3 or 5 but not both.
for i in range(1, 101):
if (i % 3 == 0 or i % 5 == 0) and not (i % 3 == 0 and i % 5 ==
0):
print(i)
10
12
18
20
21
24
25
27
33
35
36
39
40
42
48
50
51
54
55
57
63
65
66
69
70
72
78
80
81
84
85
87
93
95
96
99
100
Q4(b) Develop a Python program to check if
a string is a palindrome using slicing.
text = input("Enter a string: ")
if text == text[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
Enter a string: maam
Palindrome
Enter a string: apple
Not Palindrome
Q4(c) Develop a program that takes a list of
numbers and returns a new list containing
only the even numbers.
nums = [1, 2, 3, 4, 5, 6, 7, 8]
even_nums = []
for n in nums:
if n % 2 == 0:
even_nums.append(n)
print(even_nums)
[2, 4, 6, 8]
Q5(a) Develop a Python program that
counts the frequency of words in a
paragraph using a dictionary and displays
the top three most frequent words.
text = input("Enter paragraph: ")
words = [Link]().split()
freq = {}
for word in words:
freq[word] = [Link](word, 0) + 1
sorted_words = sorted([Link](), key=lambda x: x[1],
reverse=True)
print("Top 3 frequent words:")
for word, count in sorted_words[:3]:
print(word, count)
Enter paragraph: my name is raju. i am five years old.i am studying
in LKG
Top 3 frequent words:
am 2
my 1
Difference Between Dictionary and List
Dictionary List
Uses key-value Uses index
pairs positions
Access using Access using
keys indexes
Unordered Ordered
Q6(c) Explain how binary files differ from
text files.
Text File Binary File
Stores Stores bytes
Text File Binary File
characters
Human Not human
readable readable
Uses .txt Uses .dat/.bin
Text File Example
with open("[Link]", "w") as f:
[Link]("Hello")
Binary File Example
with open("[Link]", "wb") as f:
[Link](b"Hello")
Q8(b) Develop a custom module for factorial
and calculate binomial coefficient.
factorial_module.py
def factorial(n):
fact = 1
for i in range(1, n + 1):
fact *= i
return fact
[Link]
from factorial_module import factorial
n = int(input("Enter n: "))
r = int(input("Enter r: "))
result = factorial(n) // (factorial(r) * factorial(n-r))
print("Binomial Coefficient:", result)
Q10(b) Difference between pure functions
and modifiers.
Pure Function Modifier
Does not modify Modifies object
object
Returns new value Changes original
object
Program
class BankAccount:
def __init__(self, balance):
[Link] = balance
def pure_deposit(self, amount):
return [Link] + amount
def modify_deposit(self, amount):
[Link] += amount
acc = BankAccount(1000)
print(acc.pure_deposit(500))
print([Link])
acc.modify_deposit(500)
print([Link])
Q10(c) Explain the role of finally clause.
finally Clause
The finally block always executes whether exception occurs or not.
Example
try:
x = 10 / 0
except ZeroDivisionError:
print("Error occurred")
finally:
print("Finally block executed")
Output
Error occurred
Finally block executed