UNIT I – Introduction to Python
1. What data type is returned by input() function in Python?
2. Which of the following is a reserved keyword in Python?
a) loop b) print c) while d) function
3. What will be the output of:
print(5 + 2 * 3)
4. What is the correct way to write a comment in Python?
5. What is the result of the expression:
type(10 / 2)
UNIT II – Control Structures
1. Which loop is best when the number of iterations is known?
2. What keyword is used to exit a loop prematurely?
3. What is the output of:
x=5
if x > 2:
print("Yes")
else:
print("No")
4. How do you write a for loop to print numbers from 1 to 5?
5. What is the output of:
for i in range(3):
if i == 1:
continue
print(i)
UNIT III – Functions
1. What is the output of this code?
def add(a, b=5):
return a + b
print(add(3))
2. What is recursion? Give an example of a recursive function.
3. What are lambda functions in Python?
1
4. How do you call a function with named arguments?
5. What is the difference between a parameter and an argument?
UNIT IV – Strings and Collections
1. How do you get the length of a list in Python?
2. What is the difference between a list and a tuple?
3. How can you convert a string "123" to an integer?
4. What will be the output of:
s = "hello"
print(s[1:4])
5. How do you add a key-value pair to a dictionary?
UNIT V – File Operations
1. Which mode opens a file for appending new data?
2. Write the code to read all lines from a file named data.txt.
3. What does f.close() do after file operations?
4. How can you write the string "Hello" to a file in Python?
5. Which module would you import to work with file paths?
UNIT VI – Packages
1. How do you import the numpy package in Python?
2. What is a user-defined module?
3. Which package is commonly used for data analysis in Python?
4. How do you import only the sqrt function from the math module?
5. What command installs new packages in Python using pip?
UNIT I – Introduction to Python
1. Answer: str (string)
Explanation: input() always returns a string.
2. Answer: c) while
Explanation: while is a reserved keyword; print is a built-in function, not a keyword.
3. Answer: 11
Explanation: Multiplication has higher precedence: 2*3=6, then 5+6=11.
2
4. Answer: # This is a comment
Explanation: # starts a comment in Python.
5. Answer: <class 'float'>
Explanation: Division / returns a float, so 10 / 2 = 5.0.
UNIT II – Control Structures
1. Answer: for loop
Explanation: Use for when you know the number of iterations.
2. Answer: break
Explanation: break exits the loop immediately.
3. Answer: Yes
Explanation: Since x = 5 > 2, the if block executes.
4. Answer:
for i in range(1, 6):
print(i)
Explanation: range(1, 6) goes from 1 to 5.
5. Answer:
0
2
Explanation: When i == 1, continue skips printing.
UNIT III – Functions
1. Answer: 8
Explanation: b defaults to 5; 3 + 5 = 8.
2. Answer: Recursion is when a function calls itself.
Example: factorial function.
3. Answer: Anonymous functions created with lambda, used for short functions.
4. Answer:
5. func_name(param1=value1, param2=value2)
6. Answer: Parameters are variables in function definition; arguments are values passed
when calling.
UNIT IV – Strings and Collections
3
1. Answer: len(list_name)
2. Answer: Lists are mutable; tuples are immutable.
3. Answer: int("123") converts string to integer.
4. Answer: ell
Explanation: s[1:4] slices characters at indices 1, 2, 3.
5. Answer:
dict_name[key] = value
UNIT V – File Operations
1. Answer: "a" (append mode)
2. Answer:
with open("data.txt", "r") as f:
lines = f.readlines()
3. Answer: Closes the file, freeing resources.
4. Answer:
with open("file.txt", "w") as f:
f.write("Hello")
5. Answer: os module
UNIT VI – Packages
1. Answer:
2. import numpy as np
3. Answer: A Python file (.py) you create and import in other scripts.
4. Answer: pandas
5. Answer:
6. from math import sqrt
7. Answer:
pip install package_name