QUESTION BANK
1. What is the need for role of precedence? Illustrate the rules of precedence in with example.
In Python, operator precedence determines the order in which operations are performed in an
expression. Without precedence rules, Python wouldn't know which operation to perform first, which
could lead to incorrect results or unexpected behavior.
()-Parathesis
**-Exponential
+x,-x,-unary plus,unary minus
*,/,//,%-multiplication,division,floor division,modulo
+,- addition,subtraction
EX:10 + (5 * 2) = 20
2. List and explain Comparison and Boolean operators used in Python with example.
Comparison Operators
Comparison operators: are used to compare two values. The result is always a Boolean value: True or
False.
== - equal to
!= - not equal to
> - greater than
< - less than
EX:
a = 10
b = 20
print(a == b)
print(a < b)
Boolean (Logical) Operators
These operators are used to combine multiple conditional expressions.
and - true if both conditions are true
or - true if atleast one is true
not - reverses the result
EX:
x=7
y = 12
print(x > 5 and y < 15) # True (
print(x > 10 or y < 15) # True
print(not(x > 10)) #True
3. With an example explain the following built-in function
I) len()- Returns the number of items in an object (like a string, list, tuple, etc.).
name = "Python"
print(len(name)) # Output: 6
II) str()- Converts a value into a string data type.
num = 100
print(str(num)) # Output: '100'
II) float()-Converts a number or numeric string into a floating-point number.
a=5
b = float(a)
print(b) # Output: 5.0
IV) range()- Returns a sequence of numbers, often used in loops.
for i in range(1, 6):
print(i) # Output: 1 2 3 4 5
V) int()- Converts a float, string, or other number to an integer (removes decimals if present).
x = 9.8
print(int(x)) # Output: 9
4. Explain the use of following functions/methods with code snippets
i) input()-Takes user input as a string from the console.
name = input("Enter your name: ")
print("Hello", name)
ii) type()-Returns the data type of a value or variable.
x = 42
print(type(x)) # Output: <class 'int'>
iii) format()-Formats strings by inserting values into placeholders
name = "John"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
Output:
My name is John and I am 25 years old.
iv) remove()-Removes the first occurrence of a specified value from a list.
fruits = ['apple', 'banana', 'cherry', 'banana']
[Link]('banana')
print(fruits) # Output: ['apple', 'cherry', 'banana']
5. Explain string concatenation and replication with example.
String Concatenation:
Concatenation means joining two or more strings together using the + operator.
a = "Hello"
b = "World" #Output: HelloWorld
print(a + b)
String Replication:
Replication means repeating a string multiple times using the * operator.
word = "Hi! "
print(word * 3) # Output: Hi! Hi! Hi!
7. Describe about Control statements (if, else, elif) with example
1. if Statement
Executes a block only if the condition is True.
age = 18
if age >= 18:
print("You are eligible to vote.")
O/p You are eligible to vote.
2. if...else Statement
Executes one block if the condition is True, and another block if it's False.
age = 16
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
O/p You are not eligible to vote.
3. if...elif...else Statement
Used for multiple conditions.
marks = 75
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 50:
print("Grade: C")
else:
print("Fail")
O/pGrade: B
8. Differentiate while loop and for loop with example code snippets
I) while Loop-Repeats a block of code as long as a condition is True.
count = 1while count <= 5:
print("Count is:", count)
count += 1
O/p
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Ii) for Loop-Repeats a block of code for each item in a sequence (like a list or range()).
for i in range(1, 6):
print("Count is:", i)
O/p
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
[Link] the use of break and continue statement with example
I)break Statement-Exits the loop entirely, even if the loop
for i in range(1, 6):
if i == 3:
break
print(i)
O/p
1
2
Ii)continue Statement-Skips the current iteration and moves to the next one.
for i in range(1, 6):
if i == 3:
continue
print(i)
O/p
1
2
4
5