PRACTICAL python 12
PRACTICAL python 12
PRACTICAL python 12
Day 1 program
Aim: Create an empty set and assign it to a variable.
CODE
my_set = set()
my_set.add(10)
my_set.add(20)
print(my_set)
OUTPUT
print("Union:", union_result)
OUTPUT
Aim: Derive the first element of the sublist in [('a', 1), ('b', 2)]
CODE
data = [('a', 1), ('b', 2)]
first_element = data[0][0]
print("First element of the first sublist:", first_element)
OUTPUT
Aim: Concatenate the string "I am " and the integer 19 to produce a
string which reads "I am 29”
CODE
text = "I am "
age = 29
result = text + str(age)
print(result)
OUTPUT
Day 2 program
Aim: Write a Program to find the square root of a number
CODE
a = int(input("Enter the number : "))
root=a**0.5
print(root)
OUTPUT
Aim: Write a program to find the area of a Rectangle
CODE
a = int(input("Enter the length : "))
b = int(input("Enter the breadth : "))
ar = a*b
print(a,"*",b,"=",ar)
OUTPUT
OUTPUT
Aim: Write a program to find whether a number is even or odd
CODE
a = int(input("Enter the number : "))
if(a%2)==0:
print("even")
else:
print("odd")
OUTPUT
Aim: Write a program to check the largest among the given three
numbers
CODE
a = int(input("Enter the value of a : "))
b = int(input("Enter the value of b : "))
c = int(input("Enter the value of c : "))
if b<a>c:
print("a is greatest")
elif a<b>c:
print("b is greatest")
else:
print("c is greatest")
OUTPUT
a, b = 0, 1
count = 0
print("Fibonacci sequence:")
while count < n_terms:
print(a, end=" ")
a, b = b, a + b
count += 1
OUTPUT
Aim: Write a Python program to check if the input year is a leap year
or not.
CODE
year = int(input("Enter a year: "))
if (year % 4 == 0):
if (year % 100 == 0):
if (year % 400 == 0):
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
else:
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
OUTPUT
Aim: Write a program to check the largest among the given four
numbers
CODE
num1 = int(input("Enter the num1 : "))
num2 = int(input("Enter the num2 : "))
num3 = int(input("Enter the num3 : "))
num4 = int(input("Enter the num4 : "))
if (num1>num2) and (num1>num3) and (num1>num4):
largest = num1
elif (num2>num1) and (num2>num3) and (num2>num4):
largest = num2
elif (num3>num2) and (num3>num1) and (num3>num4):
largest = num3
else:
largest = num4
print ("The largest number is",largest)
OUTPUT
Aim: Write a Python program to print the prime numbers for a user
provided range.
CODE
lower = 0
upper = 100
print("Prime numbers between", lower, "and", upper, "are:")
OUTPUT
Review questions
Aim: Write a Program to input two numbers and find their sum and
product
CODE
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
Aim: Write a Program to convert the time taken from the user in
minutes into hours and remaining minutes
CODE
total_minutes = int(input("Enter time in minutes: "))
hours = total_minutes // 60
remaining_minutes = total_minutes % 60
print(f"{total_minutes} minutes is equal to {hours} hours and
{remaining_minutes} minutes.")
OUTPUT
SI = (P * R * T) / 100
OUTPUT
Aim: Write a Program to accept a number from the user and display
whether it is an even number or odd number.
CODE
number = int(input("Enter a number: "))
if number % 2 == 0:
print(f"{number} is an even number.")
else:
print(f"{number} is an odd number.")
OUTPUT
Aim: Write a Program to accept percentage of a student and display
its grade accordingly (using if , elif , else).
CODE
percentage = float(input("Enter the percentage of the student: "))
a, b = 0, 1
OUTPUT
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
print(f"Prime numbers up to {limit}:")
for num in range(2, limit + 1):
if is_prime(num):
print(num, end=" ")
OUTPUT
Level-1
Aim: Write a program to print from 1 to 10 .
CODE
for i in range(1, 11):
print(i)
OUTPUT
OUTPUT
sum of evens = 0
for i in range(1, n + 1):
if i % 2 == 0:
sum of evens += i
sum_of_evens = 0
current_even = 2
for i in range(n):
sum_of_evens += current_even
current_even += 2
OUTPUT
Level-2
Aim: write a program to find sum of digits of a given number.
CODE
def sum_of_digits(number):
number = abs(number)
digit_sum = 0
return digit_sum
if __name__ == "__main__":
num = int(input("Enter a number: "))
print(f"The sum of the digits of {num} is {sum_of_digits(num)}.")
OUTPUT
Aim: write a program to find product of digits of a given number.
CODE
number = int(input("Enter a number: "))
product = 1
temp_number = abs(number)
temp_number = abs(number)
if digit % 2 == 0:
sum_even += digit
else:
product_odd *= digit
temp_number = temp_number // 10
digits = str(abs(number))
try:
num = int(input("Enter a number: "))
result = sum_of_squares_of_digits(num)
print(f"The sum of the squares of the digits of {num} is: {result}")
except ValueError:
print("Please enter a valid integer.")
OUTPUT
Aim: Write a program to find sum of cube of digits of a given number.
CODE
def sum_of_cubes_of_digits(number):
digits = str(abs(number))
sum_cubes = sum(int(digit) ** 3 for digit in digits)
return sum_cubes
try:
num = int(input("Enter a number: "))
result = sum_of_cubes_of_digits(num)
try:
num = int(input("Enter a number: "))
result = reverse number(num)
print(f"The reverse of {num} is: {result}")
except ValueError:
print("Please enter a valid integer.")
OUTPUT
try:
num = int(input("Enter a number: "))
if is palindrome(num):
print(f"{num} is a palindrome number.")
else:
print(f"{num} is not a palindrome number.")
except ValueError:
print("Please enter a valid integer.")
OUTPUT