Assignment 2
Assignment 2
Ans:-
if-else Statement
Example:
age = 18
else:
2. if-elif-else Statement
Used when there are multiple conditions. The elif block checks additional
conditions if the first if is False.
Example:
marks = 75
if marks >= 90:
print("Grade: A")
print("Grade: B")
print("Grade: C")
else:
print("Grade: F")
Ans:-
a = [1, 2, 3]
b=a
Ans:-
Loop control statements are used to control the execution of loops (for
and while). Python has three loop control statements:
1. break
2. continue
The continue statement skips the current iteration and continues the loop
Example:
if num == 3:
continue
print(num)
3. pass
Example:
468
10 12 14 16 18
Ans:-
rows = [1, 3, 5]
num = 2
for _ in range(row):
num += 2
print()
6. Explain use of Pass and Else keyword with for loops in python.
Ans:-
Python provides the pass and else keywords to control the flow inside
loops.
1. pass Keyword
pass is a placeholder that does nothing.
Used when a loop must have a body but you don’t want to write
code yet.
Example:
for i in range(5):
if i == 2:
pass
print(i)
The else block runs after the loop completes normally (without
break).
Example:
for i in range(3):
print(i)
else:
print("Loop completed!")
Ans:-
Types of Arithmetic Operators
+ Addition 5+3→8
- Subtraction 10 - 4 → 6
* Multiplication 6 * 2 → 12
% Modulus (remainder) 10 % 3 → 1
** Exponentiation (power) 2 ** 3 → 8
Example Program
a = 10
b=3
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Exponentiation:", a ** b)
Ans:-
Example (a = 5, b =
Operator Name Description
3)
Example Program
a=5
b=3
print("AND:", a & b)
print("OR:", a | b)
print("XOR:", a ^ b)
print("NOT a:", ~a)
Ans:-
2. not in → Returns True if the value does not exist in the sequence.
Example 1: Using in
numbers = [1, 2, 3, 4, 5]
A for loop is used to iterate over a sequence (like a list, tuple, string, or
range) and execute code multiple times.
Syntax:
python
CopyEdit
python
CopyEdit
print(fruit)