Python Question Bank
Python Question Bank
example
Numeric Data Types in Python
In Python, numeric data types represent different types of numbers and are mainly
classified into three categories:
1. Integer (int)
2. Floating-Point (float)
3. Complex Numbers (complex)
1. Integer (int)
Represents whole numbers (both positive and negative) without decimals.
Can be of arbitrary length in Python.
Example:
x = 10 # Positive integer
y = -25 # Negative integer
z = 1000000000 # Large integer
2. Floating-Point (float)
Represents real numbers with decimal points.
Can also represent scientific notation.
Example:
a = 10.5 # Floating point number
b = -2.75 # Negative float
c = 1.2e3 # Scientific notation (1.2 × 10³ = 1200.0)
print(a + b) # Output: 19
print(a - b) # Output: 11
print(a * b) # Output: 60
print(a / b) # Output: 3.75
print(a // b) # Output: 3 (floor division)
print(a % b) # Output: 3 (modulus)
Conclusion
Python provides three main numeric data types: int, float, and complex. These types
support mathematical operations and can be converted into one another as needed.
Understanding these data types is crucial for performing numerical computations efficiently
in Python.
1. Conditional Statements
Conditional statements allow decision-making based on conditions using if, if-else, and if-
elif-else.
1.1 if Statement
Executes a block of code only if a condition is True.
Example:
age = 18
if age >= 18:
print("You are eligible to vote.")
Output:
You are eligible to vote.
Conclusion
Control flow statements (if, loops, break, continue, pass) help in decision-making and
looping, making programs more efficient. Mastering these statements is crucial for writing
structured Python programs.
pi_value = 3.14159
Step 2: Import and use it in another Python script
import my_module
Conclusion
Python provides different ways to import modules, allowing code reuse and better
organization. Choosing the right import method depends on the situation.
Conclusion
Python provides multiple ways to add, update, and delete elements in a list efficiently.
Mastering these methods helps in handling lists effectively in programming.
explain pass, break, continue and else
statements write answer
Pass, Break, Continue, and Else Statements in Python
Python provides control flow statements to manage loops and conditional execution
efficiently. These include pass, break, continue, and else (with loops). Each serves a distinct
purpose in controlling program execution.
1. pass Statement
The pass statement is a placeholder that does nothing and is used when a block of code is
required syntactically but has no execution.
Example: Using pass in a Loop
for i in range(5):
if i == 2:
pass # Placeholder for future code
print(i)
Output:
0
1
2
3
4
📌 Here, pass does nothing, allowing the program to continue execution normally.
2. break Statement
The break statement terminates the loop prematurely when a condition is met.
Example: Using break in a Loop
for i in range(5):
if i == 3:
break # Loop stops when i == 3
print(i)
Output:
0
1
2
📌 The loop stops execution when i == 3.
3. continue Statement
The continue statement skips the current iteration and moves to the next iteration of the
loop.
Example: Using continue in a Loop
for i in range(5):
if i == 2:
continue # Skips when i == 2
print(i)
Output:
0
1
3
4
📌 When i == 2, continue skips that iteration, and the loop moves to i == 3.
Conclusion
pass: Placeholder that does nothing.
break: Exits the loop immediately.
continue: Skips the current iteration and moves to the next.
else (with loops): Executes only if the loop completes without break.
1. Set (set)
A set is an unordered collection of unique elements. It does not allow duplicate values and
is defined using curly braces {} or the set() function.
Key Features of a Set:
✔️Unordered – No specific sequence of elements.
✔️Mutable – Can add/remove elements, but elements must be immutable.
✔️No Duplicates – Duplicate values are automatically removed.
Example: Creating and Using a Set
# Creating a set
fruits = {"apple", "banana", "cherry", "apple"}
print(fruits) # Output: {'banana', 'cherry', 'apple'} (duplicates removed)
# Adding an element
fruits.add("orange")
print(fruits) # Output: {'banana', 'cherry', 'apple', 'orange'}
# Removing an element
fruits.remove("banana")
print(fruits) # Output: {'cherry', 'apple', 'orange'}
Set Operations
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print(A | B) # Union: {1, 2, 3, 4, 5, 6}
print(A & B) # Intersection: {3, 4}
print(A - B) # Difference: {1, 2}
2. Tuple (tuple)
A tuple is an ordered, immutable sequence of elements. It is defined using parentheses ().
Key Features of a Tuple:
✔️Ordered – Maintains insertion order.
✔️Immutable – Cannot be modified after creation.
✔️Can Contain Duplicates – Unlike sets, tuples allow repeated values.
Example: Creating and Using a Tuple
# Creating a tuple
numbers = (10, 20, 30, 40)
print(numbers) # Output: (10, 20, 30, 40)
# Accessing elements
print(numbers[1]) # Output: 20
# Slicing a tuple
print(numbers[1:3]) # Output: (20, 30)
# Nested tuple
nested_tuple = (1, 2, (3, 4), 5)
print(nested_tuple[2]) # Output: (3, 4)
Tuple Packing and Unpacking
# Tuple Packing
person = ("Alice", 25, "Engineer")
# Tuple Unpacking
name, age, profession = person
print(name) # Output: Alice
print(age) # Output: 25
print(profession) # Output: Engineer
3. Dictionary (dict)
A dictionary is a collection of key-value pairs, where each key is unique, and values can be
accessed using keys instead of indexes.
Key Features of a Dictionary:
✔️Unordered – Elements have no fixed order (before Python 3.7).
✔️Mutable – Can modify, add, or delete key-value pairs.
✔️Unique Keys – Duplicate keys are not allowed.
Example: Creating and Using a Dictionary
# Creating a dictionary
student = {
"name": "John",
"age": 21,
"course": "Computer Science"
}
# Accessing values
print(student["name"]) # Output: John
# Updating a value
student["age"] = 22
print(student["age"]) # Output: 22
# Removing a key-value pair
del student["course"]
print(student) # Output: {'name': 'John', 'age': 22, 'grade': 'A'}
Dictionary Methods
print(student.keys()) # Output: dict_keys(['name', 'age', 'grade'])
print(student.values()) # Output: dict_values(['John', 22, 'A'])
print(student.items()) # Output: dict_items([('name', 'John'), ('age', 22), ('grade', 'A')])
Conclusion
Data Type Ordered? Mutable? Duplicates Allowed? Syntax
Set ❌ No ✅ Yes ❌ No {} or set()
Tuple ✅ Yes ❌ No ✅ Yes ()
Dict ✅ Yes ✅ Yes ❌ No (Keys) {key: value}
Understanding sets, tuples, and dictionaries is essential for handling data efficiently in
Python.
Syntax of range()
The range() function has three different forms:
1. range(stop) → Generates numbers from 0 to stop - 1.
2. range(start, stop) → Generates numbers from start to stop - 1.
3. range(start, stop, step) → Generates numbers from start to stop - 1 with a given step
size.
General Syntax
range(start, stop, step)
start (optional) → The starting value (default is 0).
stop (required) → The ending value (exclusive).
step (optional) → The difference between consecutive values (default is 1).
1. Using range(stop)
Generates numbers from 0 to stop - 1.
Example:
for i in range(5):
print(i)
Output:
0
1
2
3
4
📌 Since start is not given, it defaults to 0. The sequence stops at 4 (5 - 1).
Output:
2
3
4
5
6
📌 The sequence starts at 2 and stops at 6 (7 - 1).
Output:
10
8
6
4
2
📌 The sequence starts at 10, decreases by 2, and stops before 0.
Conclusion
range() generates a sequence of numbers.
It can be used in loops for iteration.
Supports three parameters: start, stop, and step.
Can generate sequences in both forward and reverse order.