Python All Programs
Python All Programs
Python All Programs
: 44(2121401)
Course: BCA ( G ) Student ID: 21042655
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
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
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
OUTPUT:
Enter a number: 6
6 is a perfect number
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]
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]
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
OUTPUT:
[1, 2, 3, 4, 5]
SOURCE CODE:
my_tuple = (1, 2.0, "three", 4.0, "five")
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655
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
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]
{}
SOURCE CODE:
def count_vowels(string):
vowels = "aeiouAEIOU"
count = 0
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655
OUTPUT:
Enter a string: aieiccc
Total number of vowels used in the string: 4
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
OUTPUT:
Enter the quantity purchased: 5
Enter the price per item: 450
Total cost for user: 2025.0
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
OUTPUT:
Enter your salary: 46541
Enter your years of service: 6
Your net bonus amount is: 2327.05
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]
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
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
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
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
calculator = Calculator()
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()
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
SOURCE CODE:
def find_highest_sum(marks1, marks2):
sum1 = sum(marks1)
sum2 = sum(marks2)
marks1 = []
Name: Sejal Singh Roll No. : 44(2121401)
Course: BCA ( G ) Student ID: 21042655
marks2 = []
for i in range(5):
mark = int(input("Enter mark for subject {}: ".format(i+1)))
marks1.append(mark)
for i in range(5):
mark = int(input("Enter mark for subject {}: ".format(i+1)))
marks2.append(mark)
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
SOURCE CODE:
def count_characters(string):
char_count = {}
return char_count
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
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
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
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