PP Lab Manual Unit 1
PP Lab Manual Unit 1
Example Output :
Enter the first number: 10
Enter the second number: 25
Enter the third number: 15
The largest number among 10.0, 25.0, and 15.0 is 25.0.
# Input interval
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))
Output:
Enter the starting number: 10
Enter the ending number: 50
Prime numbers between 10 and 50:
11 13 17 19 23 29 31 37 41 43 47
# Example usage
num1 = 5
num2 = 10
swap_numbers(num1, num2)
Output:
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
# Display results
print("\nArithmetic Operations:")
print(f"Addition: {num1} + {num2} = {addition}")
print(f"Subtraction: {num1} - {num2} = {subtraction}")
print(f"Multiplication: {num1} * {num2} = {multiplication}")
print(f"Division: {num1} / {num2} = {division}")
print(f"Modulus: {num1} % {num2} = {modulus}")
print(f"Exponentiation: {num1} ** {num2} = {exponentiation}")
print(f"Floor Division: {num1} // {num2} = {floor_division}")
Output:
Comparing a = 10 and b = 20
a == b : False
a != b : True
a > b : False
a < b : True
a >= b : False
a <= b : True
Comparing a = 15 and b = 15
a == b : True
a != b : False
a > b : False
a < b : False
a >= b : True
a <= b : True
iii)Assignment Operators
# Python Program to demonstrate Assignment Operators
# Basic assignment
x = 10
print(f"Initial value of x: {x}")
Output:
Initial value of x: 10
After x += 5: 15
After x -= 3: 12
After x *= 2: 24
After x /= 4: 6.0
After x %= 3: 0.0
After x **= 2: 0.0
After x //= 2: 0.0
iv) Logical Operators
# Logical Operators in Python
# User input for demonstration
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# AND operator
print("\nLogical AND Operator:")
if num1 > 0 and num2 > 0:
print("Both numbers are positive.")
else:
print("At least one number is not positive.")
# OR operator
print("\nLogical OR Operator:")
if num1 > 0 or num2 > 0:
print("At least one number is positive.")
else:
print("Neither number is positive.")
# NOT operator
print("\nLogical NOT Operator:")
if not (num1 > 0):
print(f"First number ({num1}) is not positive.")
else:
print(f"First number ({num1}) is positive.")
# Combined example
print("\nCombined Example:")
if num1 > 0 and not (num2 > 0):
print("First number is positive and second number is not positive.")
else:
print("Either both are positive or the second number is positive.")
Logical OR Operator:
At least one number is positive.
Combined Example:
First number is positive and second number is not positive.
v) Bitwise Operators
# Bitwise operators in Python
a = 10 # In binary: 1010
b = 4 # In binary: 0100
# AND operator
and_result = a & b # Binary: 1010 & 0100 = 0000
print(f"a & b = {and_result} (Binary: {bin(and_result)})")
# OR operator
or_result = a | b # Binary: 1010 | 0100 = 1110
print(f"a | b = {or_result} (Binary: {bin(or_result)})")
# XOR operator
xor_result = a ^ b # Binary: 1010 ^ 0100 = 1110
print(f"a ^ b = {xor_result} (Binary: {bin(xor_result)})")
# NOT operator
not_result = ~a # Inverts all the bits of a
print(f"~a = {not_result} (Binary: {bin(not_result)})")
Output:
a & b = 0 (Binary: 0b0)
a | b = 14 (Binary: 0b1110)
a ^ b = 14 (Binary: 0b1110)
~a = -11 (Binary: -0b1011)
a << 2 = 40 (Binary: 0b101000)
a >> 2 = 2 (Binary: 0b10)
Here's an example of a Python program that demonstrates the use of the ternary operator:
# Program to check if a number is even or odd using the ternary operator
number = int(input("Enter a number: "))
# Using the ternary operator
result = "Even" if number % 2 == 0 else "Odd"
print(f"The number {number} is {result}.")
vii)Membership Operators
# Membership Operators in Python
# Defining a list, string, and tuple
my_list = [1, 2, 3, 4, 5]
my_string = "Hello, Python!"
my_tuple = (10, 20, 30, 40)
if 'Python' in my_string:
print("'Python' is in the string")
Output:
True
True
True
False
False
True
4 is in the list
'Python' is in the string
viii)Identity Operators
# Identity operators example in Python
# Using 'is' and 'is not' with numbers
x = 10
y = 10
z = 20
print("list1 is list2:", list1 is list2) # False, because list1 and list2 are different objects
print("list1 is list3:", list1 is list3) # True, because list1 and list3 refer to the same object
print("list1 is not list2:", list1 is not list2) # True
Output:
x is y: True
x is z: False
list1 is list2: False
list1 is list3: True
list1 is not list2: True
a is b: True
def __str__(self):
return f"{self.real} + {self.imaginary}i"
Output:
Sum: 3 + 7i
Product: -10 + 11i