0% found this document useful (0 votes)
7 views6 pages

Python LabTW 3

The document demonstrates various Python operators, including arithmetic, relational, assignment, logical, bitwise, ternary, membership, and identity operators, with examples and expected outputs. It also illustrates conditional statements such as if, if-else, and if-elif-else, along with their outputs. Lastly, it showcases iterative statements using while and for loops with corresponding examples and outputs.

Uploaded by

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

Python LabTW 3

The document demonstrates various Python operators, including arithmetic, relational, assignment, logical, bitwise, ternary, membership, and identity operators, with examples and expected outputs. It also illustrates conditional statements such as if, if-else, and if-elif-else, along with their outputs. Lastly, it showcases iterative statements using while and for loops with corresponding examples and outputs.

Uploaded by

lavizamakandar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TERM WORK-3

a) Demonstrate the following operators in python with suitable examples.


i) Arithmetic operators
ii) Relational operators
iii) Assignment operator
iv) Logical operator
v) Bitwise operator
vi) Ternary operator
vii) Membership operator
viii) Identity operator

# Arithmetic operators
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) # This rounds down the number to the nearest whole integer.
print("Modulus:", a % b) # returns the remainder after division
print("Exponential:", a ** b) # 10^3 = 10 × 10 × 10 = 1000.

Output
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Floor Division: 3
Modulus: 1
Exponential: 1000

# Relational operators
x=5
y = 10
print("Equal to:", x == y)
print("Not equal to:", x != y)
print("Greater than:", x > y)
print("Less than:", x < y)
print("Greater than or equal to:", x >= y)
print("Less than or equal to:", x <= y)
Output
Equal to: False
Not equal to: True
Greater than: False
Less than: True
Greater than or equal to: False
Less than or equal to: True

# Assignment operators
x = 10 # Basic assignment
print("Initial value of x:", x)
Output
Initial value of x: 10

# Addition assignment
x += 5 # x = x + 5
print("After x += 5:", x)
Output
After x += 5: 15

# Subtraction assignment
x -= 3 # x = x - 3
print("After x -= 3:", x)
Output
After x -= 3: 12

# Multiplication assignment
x *= 2 # x = x * 2
print("After x *= 2:", x)
Output
After x *= 2: 24

# Division assignment
x /= 4 # x = x / 4
print("After x /= 4:", x)
Output
After x /= 4: 6.0

# Logical operators
x=5
y = 10
print("x > 3 and y < 15:", x > 3 and y < 15) # True and True → True
print("x > 3 or y < 20:", x > 3 or y < 20) # True or True → True
print("not (x > 3):", not (x > 3)) # not True → False
Output
x > 3 and y < 15: True
x > 3 or y < 20: True
not (x > 3): False

# Bitwise operators
x = 10 # binary: 1010
y = 4 # binary: 0100

# Bitwise AND
print("Bitwise AND:", x & y) # 1010 & 0100 → 0000 (0)

# Bitwise OR
print("Bitwise OR:", x | y) # 1010 | 0100 → 1110 (14)

# Bitwise XOR
print("Bitwise XOR:", x ^ y) # 1010 ^ 0100 → 1110 (14)

# Bitwise NOT
print("Bitwise NOT:", ~x) # ~1010 → -(1010 + 1) = -11

# Left shift
print("Left shift:", x << 1) # 1010 << 1 → 10100 (20)

# Right shift
print("Right shift:", x >> 1) # 1010 >> 1 → 0101 (5)

Output
Bitwise AND: 0
Bitwise OR: 14
Bitwise XOR: 14
Bitwise NOT: -11
Left shift: 20
Right shift: 5

#Ternary operator
a=5
b = 10
result = "a is greater" if a > b else "b is greater"
print("Ternary operator result:", result)

Output
Ternary operator result: b is greater
# Membership operators
my_list = [1, 2, 3, 4, 5] # List of numbers

# Using 'in' operator


print("Is 3 in the list?", 3 in my_list) # True

# Using 'not in' operator


print("Is 6 not in the list?", 6 not in my_list) # True

Output
Is 3 in the list? True
Is 6 not in the list? True

# Identity operator
x = [1, 2, 3]
y = [1, 2, 3]
z = x # z refers to the same object as x
# Identity operator 'is'
print("x is y:", x is y) # False (different objects in memory)
print("x is z:", x is z) # True (same object in memory)

Output
x is y: False
x is z: True

b) Demonstrate the following conditional statements in python with suitable examples.


i) if statement
ii) if else statement
iii) if-elif-else statement

# if statement
age = 18
if age >= 18:
print("You are eligible to vote.")

Output
You are eligible to vote.
# if-else statement
age = 16
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")

Output
You are not eligible to vote.

# if-elif-else statement
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
else:
print("Grade: F")

Output
Grade: B

c) Demonstrate the following iterative statements in python in with suitable examples.


i) while loop
ii) for loop

# while loop
count = 0
while count < 5:
print("Count is:", count)
count += 1

Output
Count is: 0
Count is: 1
Count is: 2
Count is: 3
Count is: 4
# for loop
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print("Fruit:", fruit)

Output
Fruit: apple
Fruit: banana
Fruit: cherry

You might also like