Python All Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

Name: Sejal Singh Roll No.

: 44(2121401)
Course: BCA ( G ) Student ID: 21042655

1.Write a python program to check if a value entered by a user is


Palindrome or not.

SOURCE CODE:
n=int(input("enter number:"))
temp=n
rev = 0
dig=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("the number is palindrome")
else:
print("the number is not palindrome")

OUTPUT:
Enter number:313
The number is palindrome

Process finished with exit code 0


Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

2.Write a python program to check if a value entered by a user is


Armstrong or not.

SOURCE CODE:
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

OUTPUT:
Enter a number: 163
163 is not an Armstrong number

Process finished with exit code 0


Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

3.Consider a number entered by a user. Now calculate the Factorial of this


number.

SOURCE CODE:
for i in range(2,21,2):
factorial = 1
for j in range(1,i+1):
factorial *= j
print("factorial of",i,"is", factorial)

OUTPUT:
factorial of 2 is 2
factorial of 4 is 24
factorial of 6 is 720
factorial of 8 is 40320
factorial of 10 is 3628800
factorial of 12 is 479001600
factorial of 14 is 87178291200
factorial of 16 is 20922789888000
factorial of 18 is 6402373705728000
factorial of 20 is 24329020081766

4.Write a python program to print the Fibonacci sequence upto the range
entered by a user.
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

SOURCE CODE:
nterms = int(input("how many terms?"))
n1, n2 = 0 , 1
count = 0
if nterms <= 0:
print("enter the number")
elif nterms == 1:
print("fibonacci sequence",nterms)
print(n1)
else:
print("fibonacci sequence:")
while count < nterms:
print(n1)
n = n1+n2
n1 = n2
n2 = n
count += 1

OUTPUT:
how many terms?6
fibonacci sequence:
0
1
1
2
3
5
5.Write a python program to check whether the number entered by the
user is a Perfect number or not.

SOURCE CODE:
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

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


sum = 0
for i in range(1, num):
if num % i == 0:
sum += i
if sum == num:
print(num, "is a perfect number")
else:
print(num, "is not a perfect number")

OUTPUT:
Enter a number: 6
6 is a perfect number

Process finished with exit code 0

6.Consider any long string. Now replace each space between two words
with the tab (i.e. the space created when you press the key 'Tab' from your
keyboard).

SOURCE CODE:
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

string = "Consider any long string. Now replace each space between two words
with the tab (i.e. the space created when you press the key 'Tab' from your
keyboard)."
new_string = string.replace(" ", "\t")
print(new_string)

OUTPUT:
Consider any long string.Now replace each space between
two words with the tab (i.e. the space created when
you press the key 'Tab' from your keyboard).
```[^1^][1]

7.Take any list and print it in the following manner:


a) Print only last 3 elements
b) Print all values except first and last value
c) Print only first 3 elements

SOURCE CODE:
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

my_list =[1,2,3,2,3,4,3,2,1]

print(my_list[-3:])
print(my_list[1:-1])
print(my_list[3:])

OUTPUT:

[3, 2, 1]
[2, 3, 2, 3, 4, 3, 2]
[2, 3, 4, 3, 2, 1]

Process finished with exit code 0

8.Python program to remove duplicate values from a list.

SOURCE CODE:
my_list=[1,2,3,4,2,3,5,3,2,1]
new_list=[]
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

for element in my_list:


if element not in new_list:
new_list.append(element)
print(new_list)

OUTPUT:
[1, 2, 3, 4, 5]

Process finished with exit code 0

9. Consider a tuple having values integer, decimal and string. Write a


python program to delete all decimal values and add 3 character values in
it.

SOURCE CODE:
my_tuple = (1, 2.0, "three", 4.0, "five")
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

new_tuple = tuple([i for i in my_tuple if not isinstance(i, float)] + ["six",


"seven", "eight"])
print(new_tuple)

OUTPUT:
(1, 'three', 'five', 'six', 'seven', 'eight')

10.Make a dictionary in which keys are in numbers and values are cube of
that key.
a) Print this dictionary
b) Print only its value
c) Now first empty that dictionary than again print it.

SOURCE CODE:
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

my_dict = {i: i**3 for i in range(1, 11)}


print(my_dict)
print(list(my_dict.values()))
my_dict.clear()
print(my_dict)

OUTPUT:
{1: 1, 2: 8, 3: 27, 4: 64, 5: 125, 6: 216, 7: 343, 8: 512, 9: 729, 10: 1000}
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
{}

11.Write a python program having a user defined function which will


calculate the total number of vowels used in string entered by a user.

SOURCE CODE:
def count_vowels(string):
vowels = "aeiouAEIOU"
count = 0
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

for char in string:


if char in vowels:
count += 1
return count

string = input("Enter a string: ")


print("Total number of vowels used in the string:", count_vowels(string))

OUTPUT:
Enter a string: aieiccc
Total number of vowels used in the string: 4

Process finished with exit code

12.A shop will give discount of 10% if the cost of purchased quantity is
more than 1000 rupees. Now write a python program having a user defined
function which will first calculate whether a user purchased quantities
more than 1000 rupees or not and then accordingly it will print the total
cost for user.

SOURCE CODE:
def calculate_cost(quantity, price_per_item):
cost = quantity * price_per_item
if cost > 1000:
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

cost *= 0.9
return cost

quantity = int(input("Enter the quantity purchased: "))


price_per_item = float(input("Enter the price per item: "))
total_cost = calculate_cost(quantity, price_per_item)
print("Total cost for user:", total_cost)

OUTPUT:
Enter the quantity purchased: 5
Enter the price per item: 450
Total cost for user: 2025.0

Process finished with exit code 0


13.Suppose a company decided to give bonus of 5% to their employee if
his/her year of service in the company is more than 5 years. Now write a
python program having a user defined function which will print the net
bonus amount. Ask user to input the salary and the year of service.

SOURCE CODE:
def calculate_bonus(salary, years_of_service):
if years_of_service > 5:
bonus = 0.05 * salary
return bonus
else:
return 0
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

salary = int(input("Enter your salary: "))


years_of_service = int(input("Enter your years of service: "))

bonus = calculate_bonus(salary, years_of_service)

print("Your net bonus amount is:", bonus)

OUTPUT:
Enter your salary: 46541
Enter your years of service: 6
Your net bonus amount is: 2327.05

Process finished with exit code 0


14.Write a lambda function in python which will multiplies two values (i.e.
x*y). Now using this
function write a python program to print the table of a number (from 1 to
10) entered by a user.

SOURCE CODE:
multiply = lambda x, y: x * y
number = int(input("Enter a number: "))
for i in range(1, 11):
print(number, "x", i, "=", multiply(number, i))
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

OUTPUT:
Enter a number: 4
4x1=4
4x2=8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
Process finished with exit code 0
15.Write a Python program to square and cube every number in a given
list of integers using Lambda.

SOURCE CODE:
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x**2, numbers))
cubed_numbers = list(map(lambda x: x**3, numbers))
print("Original numbers:", numbers)
print("Squared numbers:", squared_numbers)
print("Cubed numbers:", cubed_numbers)
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

OUTPUT:
Original numbers: [1, 2, 3, 4, 5]
Squared numbers: [1, 4, 9, 16, 25]
Cubed numbers: [1, 8, 27, 64, 125]

Process finished with exit code 0

16.Write a python class which can take values of length and breadth of a
rectangle from user and check if it is square or not. Take inputs of length
and breadth from user.

SOURCE CODE:
class Rectangle:
def __init__(self, length, breadth):
self.length = length
self.breadth = breadth

def is_square(self):
if self.length == self.breadth:
return True
else:
return False
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

length = int(input("Enter length of the rectangle: "))


breadth = int(input("Enter breadth of the rectangle: "))

rectangle = Rectangle(length, breadth)

if rectangle.is_square():
print("The rectangle is a square")
else:
print("The rectangle is not a square")

OUTPUT:
Enter length of the rectangle: 9
Enter breadth of the rectangle: 5
The rectangle is not a square

17.Write a python program which can take the 3 sides of a triangle – x, y


and z from a user. Now create a class which can determine whether the
triangle is equilateral or not.

SOURCE CODE:
class Triangle:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z

def is_equilateral(self):
if self.x == self.y == self.z:
return True
else:
return False
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

x = int(input("Enter first side of the triangle: "))


y = int(input("Enter second side of the triangle: "))
z = int(input("Enter third side of the triangle: "))

triangle = Triangle(x, y, z)

if triangle.is_equilateral():
print("The triangle is equilateral")
else:
print("The triangle is not equilateral")

OUTPUT:
Enter first side of the triangle: 6
Enter second side of the triangle: 7
Enter third side of the triangle: 3
The triangle is not equilateral
18.Write 4 python class to which can calculate addition, multiplication,
subtraction and division of two numbers. Take inputs from user.

SOURCE CODE:
class Calculator:
def __init__(self):
pass

def add(self, a, b):


return a + b

def subtract(self, a, b):


return a - b

def multiply(self, a, b):


return a * b
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

def divide(self, a, b):


if b == 0:
return "Cannot divide by zero"
else:
return a / b

calculator = Calculator()

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


b = int(input("Enter second number: "))

print("Addition:", calculator.add(a, b))


print("Subtraction:", calculator.subtract(a, b))
print("Multiplication:", calculator.multiply(a, b))
print("Division:", calculator.divide(a, b))

OUTPUT:
Enter first number: 65
Enter second number: 89
Addition: 154
Subtraction: -24
Multiplication: 5785
Division: 0.7303370786516854
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

19.Create a python class which have a function which will print all even
numbers from 1 to 100 and another function which will print all odd
numbers from 1 to 100.

SOURCE CODE:
class Numbers:
def print_even_numbers(self):
for i in range(2, 101, 2):
print(i)

def print_odd_numbers(self):
for i in range(1, 101, 2):
print(i)
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

numbers = Numbers()

print("Even numbers from 1 to 100:")


numbers.print_even_numbers()

print("Odd numbers from 1 to 100:")


numbers.print_odd_numbers()

OUTPUT :
Even numbers from 1 to 100:
2
4
6
8
10
:
98
100
Odd numbers from 1 to 100:
1
3
5
7
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

9
11
:
97
99

20.Make a function which will reverse a string and integer. Now ask user to
enter any value (either string or non-decimal number) and check whether
it is a palindrome value or not.

SOURCE CODE:
def reverse(value):
if isinstance(value, int):
value = str(value)
reversed_value = value[::-1]
return reversed_value
def is_palindrome(value):
reversed_value = reverse(value)
if str(value) == reversed_value:
return True
else:
return False
value = input("Enter a value: ")
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

if is_palindrome(value):
print("The value is a palindrome")
else:
print("The value is not a palindrome")

OUTPUT:
Enter a value: 66
The value is a palindrome

21.Write a python program to make a list of 5 subject marks obtained by


two student. Make a function to find which list of marks have highest sum
of values.

SOURCE CODE:
def find_highest_sum(marks1, marks2):
sum1 = sum(marks1)
sum2 = sum(marks2)

if sum1 > sum2:


return "Student 1 has the highest sum of marks"
elif sum2 > sum1:
return "Student 2 has the highest sum of marks"
else:
return "Both students have the same sum of marks"

marks1 = []
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

marks2 = []

print("Enter marks for Student 1:")

for i in range(5):
mark = int(input("Enter mark for subject {}: ".format(i+1)))
marks1.append(mark)

print("Enter marks for Student 2:")

for i in range(5):
mark = int(input("Enter mark for subject {}: ".format(i+1)))
marks2.append(mark)

result = find_highest_sum(marks1, marks2)

print(result)

OUTPUT:
Enter marks for Student 1:
Enter mark for subject 1: 56
Enter mark for subject 2: 78
Enter mark for subject 3: 45
Enter mark for subject 4: 12
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

Enter mark for subject 5: 45


Enter marks for Student 2:
Enter mark for subject 1: 98
Enter mark for subject 2: 88
Enter mark for subject 3: 21
Enter mark for subject 4: 56
Enter mark for subject 5: 23
Student 2 has the highest sum of marks
22.Make a python function to count the total occurrence of each character
in a string in the form of a dictionary. Now ask user to enter any string and
print the occurrence in the following manner.
Input String: good morning
Output may be like {'g': 2, 'o': 3, 'd': 1, ' ': 1, 'm': 1, 'r': 1, 'n': 2, 'i': 1}

SOURCE CODE:
def count_characters(string):
char_count = {}

for char in string:


if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1

return char_count

string = input("Enter a string: ")


Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

char_count = count_characters(string)
print(char_count)

OUTPUT:
Enter a string: good morning
{'g': 2, 'o': 3, 'd': 1, ' ': 1, 'm': 1, 'r': 1, 'n': 2, 'i': 1}
23.Write a python program to create a list with some values (integer, float,
and string). Now make a function which will remove all string values, and
another function to find the sum of remaining numerical values.

SOURCE CODE:
def remove_strings(values):
return [value for value in values if not isinstance(value, str)]
def sum_numerical_values(values):
total = 0
for value in values:
if isinstance(value, (int, float)):
total += value
return total

values = [1, 2.5, "hello", 3, "world", 4.2]


print("Original values:", values)
values = remove_strings(values)
print("Values with strings removed:", values)
total = sum_numerical_values(values)
print("Sum of numerical values:", total)
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

OUTPUT:
Original values: [1, 2.5, 'hello', 3, 'world', 4.2]
Values with strings removed: [1, 2.5, 3, 4.2]
Sum of numerical values: 10.7
24 .Make a function which will return minimum and maximum value
among 10 integer values. Write a python program to ask user to enter any
10 integer values and print the minimum and maximum value present in it.

SOURCE CODE:
def find_min_max(values):
min_value = values[0]
max_value = values[0]
for value in values:
if value < min_value:
min_value = value
elif value > max_value:
max_value = value
return (min_value, max_value)

values = []
print("Enter 10 integer values:")
for i in range(10):
value = int(input("Enter value {}: ".format(i+1)))
values.append(value)
min_value, max_value = find_min_max(values)
print("Minimum value:", min_value)
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

print("Maximum value:", max_value)

OUTPUT:
Enter 10 integer values:
Enter value 1: 45
Enter value 2: 8
Enter value 3: 5
Enter value 4: 4
Enter value 5: 7
Enter value 6: 7
Enter value 7: 6
Enter value 8: 78
Enter value 9: 98
Enter value 10: 47
Minimum value: 4
Maximum value: 98
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

25 .Make a function to add values in a dictionary having student record –


name, roll number, and last 3 semester’s percentage. Also make a function
to calculate the average percentage secured by a student till current
semester. Now print the calculated average percentage of the student.

SOURCE CODE:
def add_student_record(records, name, roll_number, percentage1, percentage2,
percentage3):
records[roll_number] = {"name": name, "percentage1": percentage1,
"percentage2": percentage2, "percentage3": percentage3}
def calculate_average_percentage(records, roll_number):
record = records[roll_number]
total_percentage = record["percentage1"] + record["percentage2"] +
record["percentage3"]
average_percentage = total_percentage / 3
return average_percentage

records = {}
add_student_record(records, "John", 1, 80, 85, 90)
add_student_record(records, "Jane", 2, 75, 80, 85)
roll_number = int(input("Enter roll number of student: "))
average_percentage = calculate_average_percentage(records, roll_number)
print("Average percentage of student with roll number {} is
{}".format(roll_number, average_percentage))
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

OUTPUT:
Enter roll number of student: 1
Average percentage of student with roll number 1 is 85.0
26 .Write a python program to create a list of 5 subject marks obtained by
a student. Now make a function which will calculate percentage of the
marks present in the list.

SOURCE CODE:
def calculate_percentage(marks):
total_marks = sum(marks)
percentage = total_marks / len(marks)
return percentage

marks = []
print("Enter marks for 5 subjects:")
for i in range(5):
mark = int(input("Enter mark for subject {}: ".format(i+1)))
marks.append(mark)
percentage = calculate_percentage(marks)
print("Percentage of marks:", percentage)

OUTPUT:
Enter marks for 5 subjects:
Enter mark for subject 1: 78
Enter mark for subject 2: 48
Enter mark for subject 3: 62
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655

Enter mark for subject 4: 35


Enter mark for subject 5: 98
Percentage of marks: 64.2

You might also like