PRACTICAL python 12

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

PRACTICAL- 1

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

Aim: Add three items to your empty set.


CODE
my_set = set()
my_set.add(1)
my_set.add(2)
my_set.add(3)
print(my_set)
OUTPUT

Aim: Create a second set which includes at least one common


element with the first set
CODE
my_set = {1, 2, 3}
second_set = {2, 4, 5}
print("First set:", my_set)
print("Second set:", second_set)
OUTPUT

Aim: Find the union, symmetric difference, and intersection of the


two sets. Print the results of
each operation
CODE
my_set = {1, 2, 3}
second_set = {2, 4, 5}
union_result = my_set.union(second_set)
symmetric_difference_result =
my_set.symmetric_difference(second_set)
intersection_result = my_set.intersection(second_set)

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

Aim: Write a program to swap the values of two variables


CODE
a = 18
b = 21
c=a
a=b
print("The value of a",b)
print("The value of b",c)

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

Aim: Write a Python program to display the Fibonacci sequence for


10 terms (use ‘while”)
CODE
n_terms = 10

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

for num in range(lower, upper + 1):


# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)

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

sum_result = num1 + num2


product_result = num1 * num2
print(f"The sum of {num1} and {num2} is {sum_result}")
print(f"The product of {num1} and {num2} is {product_result}")
OUTPUT

Aim: Write a Program to compute x n of given two integers x and n. x


and n to be taken using input command.
CODE
x = int(input("Enter the base (x): "))
n = int(input("Enter the exponent (n): "))
result = x ** n

print(f"{x} raised to the power {n} is {result}")


OUTPUT

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

Aim: Write a Program for calculating simple interest. All numerical


values required to be taken using input command.
CODE
P = float(input("Enter the principal amount (P): "))
R = float(input("Enter the rate of interest (R) per year: "))
T = float(input("Enter the time (T) in years: "))

SI = (P * R * T) / 100

print(f"The Simple Interest is: {SI}")

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

if percentage >= 90:


grade = 'A'
elif percentage >= 80:
grade = 'B'
elif percentage >= 70:
grade = 'C'
elif percentage >= 60:
grade = 'D'
else:
grade = 'F'

print(f"The grade for {percentage}% is: {grade}")


OUTPUT
Aim: Write a Program to print Fibonacci series up to certain limit
(using – while). Limit to be taken from the user.
CODE
limit = int(input("Enter the limit for Fibonacci series: "))

a, b = 0, 1

print("Fibonacci series up to", limit, ":")


while a <= limit:
print(a, end=" ")
a, b = b, a + b

OUTPUT

Aim: Write a Program to display prime numbers up to a certain limit.


Limit to be taken from the user.
CODE
limit = int(input("Enter the limit to display prime numbers: "))

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

Aim: Write a program to calculate and display Area and Perimeter of


a rectangle. Take floating input from user.
CODE
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = length * width
perimeter = 2 * (length + width)
print(f"The area of the rectangle is: {area}")
print(f"The perimeter of the rectangle is: {perimeter}")
OUTPUT

Aim: Write a Program to swap two numbers using a third variable.


CODE
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
print (f" Before swapping: num1 = {num1}, num2 = {num2}")
temp = num1
num1 = num2
num2 = temp
print (f" After swapping: num1 = {num1}, num2 = {num2}")
OUTPUT
Aim: Write a Program to swap two numbers without using a third
variable.
CODE
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
print(f"Before swapping: num1 = {num1}, num2 = {num2}")
num1 = num1 + num2
num2 = num1 - num2
num1 = num1 - num2
print(f"After swapping: num1 = {num1}, num2 = {num2}")
OUTPUT

Level-1
Aim: Write a program to print from 1 to 10 .
CODE
for i in range(1, 11):
print(i)
OUTPUT

Aim: Write a program to print from 1 to n.


CODE
n = int(input("Enter a number: "))
for i in range(1, n + 1):
print(i)
OUTPUT

Aim: Write a program to print from 10 to 1.


CODE
for i in range(10, 0, -1):
print(i)
OUTPUT

Aim: Write a program to print sum of n to 1.


CODE
n = int(input("Enter a number: "))
total_sum = sum(range(n, 0, -1))
print("Sum from", n, "to 1 is:", total_sum)
OUTPUT

Aim: Write a program to print sum from 1 to n.


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

total_sum = sum(range(1, n + 1))

print("Sum from 1 to", n, "is:", total_sum)

OUTPUT

Aim: Write a program to print sum of SQUARE from 1 to n.


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

sum_of_squares = sum(i**2 for i in range(1, n + 1))

print("Sum of squares from 1 to", n, "is:", sum_of_squares)


OUTPUT
Aim: Write a program to print sum of CUBE from 1 to n.
CODE
n = int(input("Enter a number: "))

sum_of_cubes = sum(i**3 for i in range(1, n + 1))

print("Sum of cubes from 1 to", n, "is:", sum_of_cubes)


OUTPUT

Aim: Write a program to print only even number between 1 to n.


CODE
n = int(input("Enter a number: "))
for i in range(1, n + 1):
if i % 2 == 0:
print(i)
OUTPUT

Aim: Write a program to find sum of even numbers from 1 to n.


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

sum of evens = 0
for i in range(1, n + 1):
if i % 2 == 0:
sum of evens += i

print("Sum of even numbers from 1 to", n, "is:", sum of evens)


OUTPUT
Aim: Write a program to find sum of first n even numbers.
CODE
n = int(input("Enter a positive integer (n): "))

sum_of_evens = 0
current_even = 2

for i in range(n):
sum_of_evens += current_even
current_even += 2

print(f"The sum of the first {n} even numbers is: {sum_of_evens}")

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

while number > 0:


digit_sum += number % 10
number //= 10

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)

while temp_number > 0:


digit = temp_number % 10
product = product * digit
temp_number = temp_number // 10

print("The product of the digits is:", product)


OUTPUT
Aim: write a program to find sum of even digits and product of odd
digits of a given number
CODE
sum_even = 0
Product_odd = 1

temp_number = abs(number)

while temp_number > 0:


digit = temp_number % 10

if digit % 2 == 0:
sum_even += digit
else:
product_odd *= digit

temp_number = temp_number // 10

print("The sum of even digits is:", sum even)


print("The product of odd digits is:", product odd)
number = int(input("Enter a number: "))
OUTPUT
Aim: Write a program to find sum of square of digits of a given
number.
CODE
def sum_of_squares_of_digits(number):

digits = str(abs(number))

sum_squares = sum(int(digit) ** 2 for digit in digits)


return sum_squares

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)

print(f"The sum of the cubes of the digits of {num} is: {result}")


except ValueError:
print("Please enter a valid integer.")
OUTPUT
Aim: Write a program to check whether a given number is armstrong
or not.
CODE
def is armstrong_number(number):
digits = str(abs(number))
num_digits = len(digits)
armstrong_sum = sum(int(digit) ** num digits for digit in digits)
return armstrong_sum == number
try:
num = int(input("Enter a number: "))
if is armstrong_number(num):
print(f"{num} is an armstrong_number.")
else:
print(f"{num} is not an armstrong_number.")
except ValueError:
print("Please enter a valid integer.")
OUTPUT
Aim: Write a program to reverse a given number.
CODE
def reverse number(number):
is negative = number < 0
reversed num = int(str(abs(number))[::-1])
return -reversed num if is negative else reversed 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

Aim: Write a program to check whether a given number is palindrom


number or not.
CODE
def is palindrome(number):
num str = str(abs(number))
return num str == num str[-1]:

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

Aim: Write a program to print factors of given number.


CODE
def print factors(number):
abs number = abs(number)
print(f"Factors of {number} are:")
for i in range(1, abs number + 1):
if abs number % i == 0:
print(i)
try:
num = int(input("Enter a number: "))
print factors(num)
except ValueError:
print("Please enter a valid integer.")
OUTPUT

Aim: Write a program to count total number of factors of a given


number.
CODE
def count factors(number):
abs_number = abs(number)
count = 0
for i in range(1, abs_number + 1):
if abs_number % i == 0:
count += 1
return count
try:
num = int(input("Enter a number: "))
total_factors = count_factors(num)
print(f"The total number of factors of {num} is: {total_factors}")
except ValueError:
print("Please enter a valid integer.")
OUTPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy