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

Python Arithmetic and String Operations

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 views7 pages

Python Arithmetic and String Operations

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

Practical 1

Write a program to enter 2 integers, 2 oating numbers and then perform the
arithmetic operation on them code in python.

int1=int(input("Enter rst integer: "))


int2=int(input("Enter second integer: "))
oat1= oat(input("Enter rst oating-point number: "))
oat2= oat(input("Enter second oating-point number: "))
print("\n--- Arithmetic Operations ---")
print(f"Sum of integers: {int1+int2}")
print(f"Difference of integers: {int1-int2}")
print(f"Product of integers: {int1*int2}")
if int2!=0:
print(f"Division of integers: {int1/int2}")
else:
print("Division of integers: Not possible (division by zero)")
print(f"\nSum of oats: { oat1+ oat2}")
print(f"Difference of oats: { oat1- oat2}")
print(f"Product of oats: { oat1* oat2}")
if oat2!=0:
print(f"Division of oats: { oat1/ oat2}")
else:
print("Division of oats: Not possible (division by zero)”)

Output:
fl
fl
fl
fl
fl
fl
fl
fl
fl
fi
fl
fl
fi
fl
fl
fl
fl
fl
fl
fl
fl
fl
fl
Practical 2

Write a program to check whether a number is a armstrong number or not.

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


s=0
n=num
p=len(str(num)) # number of digits
while n>0:
d = n%10
s+=d**p
n//=10
if s==num:
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number”)

Output:
Practical 3

Write a program to print the sum of all the primes between two ranges.

def is_prime(n):
if n<2:
return False
for i in range(2, int(n**0.5)+1):
if n%i==0:
return False
return True
low=int(input("Enter lower range: "))
high=int(input("Enter upper range: "))
prime_sum=0
for num in range(low, high+1):
if is_prime(num):
prime_sum+=num
print(f"Sum of primes between {low} and {high} is: {prime_sum}”)

Output:
Practical 4

Write a program to swap two strings.

s1=input("Enter rst string: ")


s2=input("Enter second string: ")
print("\nBefore Swapping:")
print("String 1:", s1)
print("String 2:", s2)
s1,s2=s2,s1
print("\nAfter Swapping:")
print("String 1:",s1)
print("String 2:”,s2)

Output:
fi
Practical 5

Write a menu driven program to accept two strings from the user and perform the
various function using user de ned functions.

def concatenate(s1,s2):
return s1+s2
def compare(s1,s2):
if s1==s2:
return "Both strings are equal"
elif s1>s2:
return f'"{s1}" is greater than "{s2}" (lexicographically)'
else:
return f'"{s2}" is greater than "{s1}" (lexicographically)'
def reverse(s):
return s[::-1]
def length(s):
return len(s)
def substring(s1,s2):
return "Yes" if s2 in s1 else "No"
s1=input("Enter rst string: ")
s2=input("Enter second string: ")
while True:
print("\n--- String Operations Menu ---")
print("1. Concatenate Strings")
print("2. Compare Strings")
print("3. Reverse Strings")
print("4. Length of Strings")
print("5. Check if Second String is Substring of First")
print("6. Exit")
choice = int(input("Enter your choice: "))
if choice==1:
print("Concatenation:", concatenate(s1, s2))
elif choice==2:
print(compare(s1, s2))
elif choice==3:
print("Reverse of String 1:", reverse(s1))
print("Reverse of String 2:", reverse(s2))
elif choice==4:
print("Length of String 1:", length(s1))
print("Length of String 2:", length(s2))
elif choice==5:
print("Is String 2 a substring of String 1? ->", substring(s1, s2))
elif choice==6:
print("Exiting program. Goodbye!")
break
else:
print("Invalid choice! Please try again.”)
fi
fi
Output:
Practical 6

Write a program to perform List Operation like access list item, change list item,
replace value in a list n python, append items to a list, insert item to a list, extend item
to a list, remove item from a list, clear entire list.

my_list=[10,20,30,40,50]
print("Initial List:",my_list)
print("Access 1st item:",my_list[0])
print("Access last item:",my_list[-1])
my_list[1]=25
print("After changing 2nd item:",my_list)
my_list[2:4]=[35, 45]
print("After replacing values at index 2 & 3:",my_list)
my_list.append(60)
print("After appending 60:",my_list)
my_list.insert(2,15)
print("After inserting 15 at index 2:",my_list)
extra_list=[70, 80, 90]
my_list.extend(extra_list)
print("After extending another list:",my_list)
my_list.remove(25)
print("After removing 25:",my_list)
my_list.clear()
print("After clearing list:”,my_list)

Output

You might also like