0% found this document useful (0 votes)
4 views9 pages

Assignment 2

This document covers Python operators and control flow statements, including membership, identity, assignment, arithmetic, and bitwise operators. It explains decision-making statements such as if-else and if-elif-else, as well as loop control statements like break, continue, and pass. Additionally, it provides examples of using for loops and demonstrates the use of the pass and else keywords in loops.

Uploaded by

tajanenikhil271
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Assignment 2

This document covers Python operators and control flow statements, including membership, identity, assignment, arithmetic, and bitwise operators. It explains decision-making statements such as if-else and if-elif-else, as well as loop control statements like break, continue, and pass. Additionally, it provides examples of using for loops and demonstrates the use of the pass and else keywords in loops.

Uploaded by

tajanenikhil271
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIT II [10 Marks]

Python Operators and Control Flow Statements

1. List membership operators in Python.

Ans:- 1.in, 2.not in

2. Explain decision making statements if-else, if-elif-else with


example.

Ans:-

if-else Statement

The if statement checks a condition. If True, it executes a block of code; otherwise,


the else block runs.

Example:

age = 18

if age >= 18:

print("You are an adult.")

else:

print("You are a minor.")

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")

elif marks >= 75:

print("Grade: B")

elif marks >= 50:

print("Grade: C")

else:

print("Grade: F")

3. Explain identity and assignment operators with example

Ans:-

. Identity Operators (is, is not)

These check if two variables refer to the same object in memory.

 is → Returns True if two variables refer to the same object.

a = [1, 2, 3]

b=a

print(a is b) # Output: True

 is not → Returns True if two variables refer to different objects.

x = [10, 20, 30]

y = [10, 20, 30] # Different object with the same values

print(x is not y) # Output: True


4. Explain loop control statement in python.

Ans:-

Loop control statements are used to control the execution of loops (for
and while). Python has three loop control statements:

1. break

The break statement exits the loop when a condition is met.


for num in range(1, 6):
if num == 3:
break # Stops the loop when num is 3
print(num)

2. continue

The continue statement skips the current iteration and continues the loop

Example:

for num in range(1, 6):

if num == 3:

continue

print(num)

3. pass

The pass statement does nothing and is used as a placeholder.

Example:

for num in range(1, 6):


if num == 3:
pass # Placeholder, does nothing
print(num)
5. Write python program to display output like.

468

10 12 14 16 18

Ans:-

rows = [1, 3, 5]

num = 2

for row in rows:

for _ in range(row):

print(num, end=" ")

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)

2. else Keyword with for Loop

 The else block runs after the loop completes normally (without
break).

 If the loop is stopped using break, else does not run.

Example:

for i in range(3):

print(i)

else:

print("Loop completed!")

7. Explain arithmetic operators in python with an example.

Ans:-
Types of Arithmetic Operators

Operator Description Example

+ Addition 5+3→8

- Subtraction 10 - 4 → 6

* Multiplication 6 * 2 → 12

/ Division (returns float) 10 / 3 → 3.33

Floor Division (removes


// 10 // 3 → 3
decimal)

% 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)

8. Describe bitwise operators in python with an example.

Ans:-

Example (a = 5, b =
Operator Name Description
3)

& AND 1 if both bits are 1 5&3→1

1 if at least one bit is


` ` OR
1

^ XOR 1 if bits are different 5^3→6

~ NOT Flips all bits ~5 → -6

Shifts bits left (multiply by


<< Left Shift 5 << 1 → 10
2)

Right Shifts bits right (divide by


>> 5 >> 1 → 2
Shift 2)

Example Program

a=5

b=3

print("AND:", a & b)

print("OR:", a | b)

print("XOR:", a ^ b)
print("NOT a:", ~a)

print("Left Shift:", a << 1)

print("Right Shift:", a >> 1)

9. Explain membership operator operators in python with examples.

Ans:-

Types of Membership Operators

1. in → Returns True if the value exists in the sequence.

2. not in → Returns True if the value does not exist in the sequence.

Example 1: Using in

fruits = ["apple", "banana", "cherry"]

print("apple" in fruits) # True (apple is in the list)

print("mango" in fruits) # False (mango is not in the list)

Example 2: Using not in

numbers = [1, 2, 3, 4, 5]

print(6 not in numbers) # True (6 is not in the list)

print(3 not in numbers) # False (3 is in the list)

10. Explain for-loop in python with example.


Ans:-

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

for variable in sequence:

# Code block to execute

Example 1: Using for Loop with a List

python

CopyEdit

fruits = ["apple", "banana", "cherry"]

for fruit in fruits:

print(fruit)

You might also like