python programming question bank unit wise by Rupesh ✓
python programming question bank unit wise by Rupesh ✓
11. Explain the difference between set and frozenset data types in
Python.
Set: A set is a mutable collection that contains unique elements. It
allows modifications like adding or removing elements.
Frozenset: A frozenset is an immutable version of a set. Once
created, elements cannot be added or removed from it.
10. What is the difference between "in" and "not in" membership
operators in Python?
in: Returns True if the value is found in the specified iterable (like a
list, tuple, string). Example:
"a" in "apple" # Returns True
not in: Returns True if the value is not found in the specified
iterable. Example:
"z" not in "apple" # Returns True
13. How can you convert a string to integer in Python? You can use
the int() function to convert a string to an integer. Example:
s = "123"
n = int(s) # n will be 123 (integer)
16. Explain the different methods to accept input from the user in
Python:
The input() function is used to accept user input. It returns the
input as a string. To get other data typ
es, you can convert the input using functions like int(), float(), etc.
Example:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
23. What is the difference between for loop and while loop?
for loop: Iterates over a sequence (such as a list, tuple, or range)
and executes the block of code for each item in the sequence.
Example:
for i in range(5):
print(i)
while loop: Continues to execute the block of code as long as a
condition remains true. Example:
i=0
while i < 5:
print(i)
i += 1
25. Explain the need for functions. Functions in Python are needed
for:
Code reusability: You can define a function once and reuse it
multiple times.
Modularization: Functions break down large programs into
smaller, manageable parts.
Organization: Functions improve code organization, making it
more readable and maintainable.
Avoiding code repetition: Functions allow you to write reusable
code blocks, reducing duplication.
27. Explain max(), min(), len(), abs(), round(), pow(), sum(), eval(),
and exec() functions with an example:
max(): Returns the largest item in an iterable or the largest of two
or more arguments. Example: max(3, 5, 2) returns 5.
min(): Returns the smallest item in an iterable or the smallest of
two or more arguments. Example: min(3, 5, 2) returns 2.
len(): Returns the length of an object (e.g., list, string). Example:
len("Hello") returns 5.
abs(): Returns the absolute value of a number. Example: abs(-7)
returns 7.
round(): Rounds a floating-point number to the nearest integer or
to the specified number of decimal places. Example: round(3.14159,
2) returns 3.14.
pow(): Returns the power of a number. Example: pow(2, 3) returns
8.
sum(): Returns the sum of all items in an iterable. Example: sum([1,
2, 3]) returns 6.
eval(): Executes a string as a Python expression and returns the
result. Example: eval("3 + 5") returns 8.
exec(): Executes a dynamically created Python program (multi-line
strings). Example:
exec("x = 5\nprint(x)")