0% found this document useful (0 votes)
52 views13 pages

PP Lab Manual Unit 1

The document outlines a Python Programming Lab course for Electrical and Electronics Engineering, detailing various programming exercises. It includes programs for finding the largest number among three inputs, displaying prime numbers in a range, swapping two numbers without a temporary variable, and demonstrating different types of operators in Python. Each section provides example code and expected outputs for clarity.

Uploaded by

royalsaaho2003
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)
52 views13 pages

PP Lab Manual Unit 1

The document outlines a Python Programming Lab course for Electrical and Electronics Engineering, detailing various programming exercises. It includes programs for finding the largest number among three inputs, displaying prime numbers in a range, swapping two numbers without a temporary variable, and demonstrating different types of operators in Python. Each section provides example code and expected outputs for clarity.

Uploaded by

royalsaaho2003
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
You are on page 1/ 13

PYTHON PROGRAMMING LAB ELECTRICAL AND ELECTRONICS ENGINEERING

SKILL ENHANCEMENT COURSE: PYTHON PROGRAMMING LAB


II YEAR II SEM Course Code: 231EE4S01
UNIT-I
1. Write a program to find the largest element among three Numbers.
# Program to find the largest number among three numbers

def find_largest(a, b, c):


if a >= b and a >= c:
return a
elif b >= a and b >= c:
return b
else:
return c

# Input from the user


num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

# Find the largest number


largest = find_largest(num1, num2, num3)

# Display the result


print(f"The largest number among {num1}, {num2}, and {num3} is {largest}.")

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.

M.V.KUMAR REDDY 1 ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY(A)


PYTHON PROGRAMMING LAB ELECTRICAL AND ELECTRONICS ENGINEERING

2. Write a Program to display all prime numbers within an interval


# Function to check if a number is prime
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True

# Function to display prime numbers within an interval


def display_primes(start, end):
print(f"Prime numbers between {start} and {end}:")
for num in range(start, end + 1):
if is_prime(num):
print(num, end=" ")

# Input interval
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))

# Display primes in the given range


display_primes(start, end)

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

M.V.KUMAR REDDY 2 ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY(A)


PYTHON PROGRAMMING LAB ELECTRICAL AND ELECTRONICS ENGINEERING

3. Write a program to swap two numbers without using a temporary variable.


# Function to swap two numbers
def swap_numbers(a, b):
print(f"Before swapping: a = {a}, b = {b}")

# Swapping without a temporary variable


a=a+b
b=a-b
a=a-b

print(f"After swapping: a = {a}, b = {b}")


return a, b

# Example usage
num1 = 5
num2 = 10
swap_numbers(num1, num2)

Output:
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5

4. Demonstrate the following Operators in Python with suitable examples.


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

i)Program on Arithmetic Operators


# Python program to demonstrate arithmetic operators
def main():
# Input two numbers
M.V.KUMAR REDDY 3 ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY(A)
PYTHON PROGRAMMING LAB ELECTRICAL AND ELECTRONICS ENGINEERING

num1 = float(input("Enter the first number: "))


num2 = float(input("Enter the second number: "))

# Perform arithmetic operations


addition = num1 + num2
subtraction = num1 - num2
multiplication = num1 * num2
division = num1 / num2 if num2 != 0 else "undefined (division by zero)"
modulus = num1 % num2 if num2 != 0 else "undefined (modulus by zero)"
exponentiation = num1 ** num2
floor_division = num1 // num2 if num2 != 0 else "undefined (floor division by zero)"

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

# Call the main function


if __name__ == "__main__":
main()
Input:
Enter the first number: 10
Enter the second number: 3
Output:
Arithmetic Operations:
Addition: 10.0 + 3.0 = 13.0
Subtraction: 10.0 - 3.0 = 7.0
Multiplication: 10.0 * 3.0 = 30.0
Division: 10.0 / 3.0 = 3.3333333333333335
Modulus: 10.0 % 3.0 = 1.0
Exponentiation: 10.0 ** 3.0 = 1000.0
Floor Division: 10.0 // 3.0 = 3.0

M.V.KUMAR REDDY 4 ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY(A)


PYTHON PROGRAMMING LAB ELECTRICAL AND ELECTRONICS ENGINEERING

ii) Relational Operators


# Relational Operators Demonstration

def relational_operators_demo(a, b):


print(f"Comparing a = {a} and b = {b}")
print("a == b :", a == b) # Equal to
print("a != b :", a != b) # Not equal to
print("a > b :", a > b) # Greater than
print("a < b :", a < b) # Less than
print("a >= b :", a >= b) # Greater than or equal to
print("a <= b :", a <= b) # Less than or equal to

# Test the function


a = 10
b = 20
relational_operators_demo(a, b)

# Change the values of a and b to test further


a = 15
b = 15
relational_operators_demo(a, b)

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

M.V.KUMAR REDDY 5 ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY(A)


PYTHON PROGRAMMING LAB ELECTRICAL AND ELECTRONICS ENGINEERING

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

# Add and assign


x += 5
print(f"After x += 5: {x}")

# Subtract and assign


x -= 3
print(f"After x -= 3: {x}")

# Multiply and assign


x *= 2
print(f"After x *= 2: {x}")

# Divide and assign


x /= 4
print(f"After x /= 4: {x}")

# Modulus and assign


x %= 3
print(f"After x %= 3: {x}")

# Exponentiation and assign


x **= 2
print(f"After x **= 2: {x}")

# Floor division and assign


x //= 2
print(f"After x //= 2: {x}")

M.V.KUMAR REDDY 6 ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY(A)


PYTHON PROGRAMMING LAB ELECTRICAL AND ELECTRONICS ENGINEERING

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

M.V.KUMAR REDDY 7 ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY(A)


PYTHON PROGRAMMING LAB ELECTRICAL AND ELECTRONICS ENGINEERING

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

Input and Output:


Enter first number: 5
Enter second number: -3

Logical AND Operator:


At least one number is not positive.

Logical OR Operator:
At least one number is positive.

Logical NOT Operator:


First number (5) 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)})")

M.V.KUMAR REDDY 8 ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY(A)


PYTHON PROGRAMMING LAB ELECTRICAL AND ELECTRONICS ENGINEERING

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

# Left shift operator


left_shift_result = a << 2 # Shift bits of a left by 2 positions
print(f"a << 2 = {left_shift_result} (Binary: {bin(left_shift_result)})")

# Right shift operator


right_shift_result = a >> 2 # Shift bits of a right by 2 positions
print(f"a >> 2 = {right_shift_result} (Binary: {bin(right_shift_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)

vi) Ternary Operator


The syntax of the ternary operator is:
value_if_true if condition else value_if_false

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

M.V.KUMAR REDDY 9 ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY(A)


PYTHON PROGRAMMING LAB ELECTRICAL AND ELECTRONICS ENGINEERING

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)

# Using 'in' operator


print(3 in my_list) # True because 3 is in the list
print('H' in my_string) # True because 'H' is in the string
print(20 in my_tuple) # True because 20 is in the tuple

# Using 'not in' operator


print(6 in my_list) # False because 6 is not in the list
print('z' in my_string) # False because 'z' is not in the string
print(50 not in my_tuple) # True because 50 is not in the tuple

# Combining membership operators with conditions


if 4 in my_list:
print("4 is in the list")

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

M.V.KUMAR REDDY 10 ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY(A)


PYTHON PROGRAMMING LAB ELECTRICAL AND ELECTRONICS ENGINEERING

viii)Identity Operators
# Identity operators example in Python
# Using 'is' and 'is not' with numbers
x = 10
y = 10
z = 20

# Checking if x and y refer to the same object


print("x is y:", x is y) # Should return True, because of small integer caching
print("x is z:", x is z) # Should return False, as x and z are different objects

# Using 'is' and 'is not' with lists


list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1

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

# Using identity operators with None


a = None
b = None

print("a is b:", a is b) # True, both a and b point to None

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

M.V.KUMAR REDDY 11 ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY(A)


PYTHON PROGRAMMING LAB ELECTRICAL AND ELECTRONICS ENGINEERING

5. Write a program to add and multiply complex numbers


class ComplexNumber:
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary

def __str__(self):
return f"{self.real} + {self.imaginary}i"

def add(self, other):


real = self.real + other.real
imaginary = self.imaginary + other.imaginary
return ComplexNumber(real, imaginary)

def multiply(self, other):


real = self.real * other.real - self.imaginary * other.imaginary
imaginary = self.real * other.imaginary + self.imaginary * other.real
return ComplexNumber(real, imaginary)
# Example usage
complex1 = ComplexNumber(2, 3) # 2 + 3i
complex2 = ComplexNumber(1, 4) # 1 + 4i

# Add the two complex numbers


sum_result = complex1.add(complex2)
print(f"Sum: {sum_result}")

# Multiply the two complex numbers


product_result = complex1.multiply(complex2)
print(f"Product: {product_result}")

Output:
Sum: 3 + 7i
Product: -10 + 11i

M.V.KUMAR REDDY 12 ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY(A)


PYTHON PROGRAMMING LAB ELECTRICAL AND ELECTRONICS ENGINEERING

6. Write a program to print multiplication table of a given number


# Function to print multiplication table
def print_multiplication_table(number, up_to=10):
for i in range(1, up_to + 1):
print(f"{number} x {i} = {number * i}")

# Input: user provides the number


number = int(input("Enter a number: "))

# Call the function to print the table


print_multiplication_table(number)

M.V.KUMAR REDDY 13 ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY(A)

You might also like