20IT425 Python
20IT425 Python
def are(len):
return len*len
def parameter(len):
return 4*len
Output:
20IT425 1
4IT01: Python Programming
Output:
1.3 WAP to find out absolute value of an input number take input from user.
def find_absolute_value(number):
if number < 0:
number = -number
return number
absolute_value = find_absolute_value(num)
Output:
Output:
20IT425 2
4IT01: Python Programming
Output:
def convert_bits(bits):
# Constants for conversion
gigabyte = bits / (8 * 1024 ** 3)
megabyte = bits / (8 * 1024 ** 2)
terabyte = bits / (8 * 1024 ** 4)
return gigabyte, megabyte, terabyte
bits = float(input("Enter the number of bits: "))
gigabyte, megabyte, terabyte = convert_bits(bits)
Output:
20IT425 3
4IT01: Python Programming
20IT425 4
4IT01: Python Programming
def odd_even(no):
if(no%2==0):
print("Even")
else:
print("Odd")
odd_even(number)
Output:
def is_leap_year(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
if is_leap_year(year):
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
20IT425 5
4IT01: Python Programming
Output:
Output:
2.4 WAP a program that takes the mark of 5 subject and displat the the grade.
def calculate_grade(average_mark):
if average_mark >= 90:
return "A+"
elif average_mark >= 80:
return "A"
elif average_mark >= 70:
return "B"
elif average_mark >= 60:
return "C"
elif average_mark >= 50:
return "D"
else:
20IT425 6
4IT01: Python Programming
return "F"
marks = []
for i in range(5):
subject_mark = float(input("Enter the mark for subject {}: ".format(i+1)))
marks.append(subject_mark)
grade = calculate_grade(average)
Output:
20IT425 7
4IT01: Python Programming
20IT425 8
4IT01: Python Programming
Practical 3 – Loops
3.1 WAP to print all even number between 1 to 100 using while loop.
number = 1
Output:
def fact(num):
f=1
while(num>0):
f=f*num
num-=1
return f
num = eval(input("Enter any number to find factorial for the same : "))
fact(num)
Output:
20IT425 9
4IT01: Python Programming
3.3 WAP to takes number and find sum of digits from that number.
Output:
def print_bottom_to_top_pyramid(rows):
for i in range(rows, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1))
print_bottom_to_top_pyramid(num_rows)
Output:
20IT425 10
4IT01: Python Programming
def find_min_max_from_tuples(lst):
if not lst:
return None, None
Output:
def find_repeated_items_in_tuple(tup):
repeated_items = []
seen_items = set()
20IT425 11
4IT01: Python Programming
return repeated_items
my_tuple = (1, 2, 3, 2, 4, 5, 4, 6, 7, 8, 5)
repeated_items = find_repeated_items_in_tuple(my_tuple)
print("Repeated items:", repeated_items)
Output:
def sum_list_items(lst):
total_sum = 0
return total_sum
my_list = [1, 2, 3, 4, 5]
result = sum_list_items(my_list)
print("Sum of list items:", result)
Output:
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
result_union = find_union_of_lists(list1, list2)
print("Union of lists:", result_union)
20IT425 12
4IT01: Python Programming
Output:
4.5 WAP to Print Smallest Even and Smallest Odd Number in a List.
def find_smallest_even_odd(lst):
smallest_even = None
smallest_odd = None
my_list = [5, 3, 2, 7, 8, 4, 9, 1, 6]
smallest_even, smallest_odd = find_smallest_even_odd(my_list)
Output:
20IT425 13
4IT01: Python Programming
def find_common(lst1,lst2):
dct={}
lst3=[]
for i in lst1:
dct[i] = 1
for i in lst2:
if i in dct and dct[i]==1:
lst3.append(i)
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
find_common(list1,list2)
Output:
return combined_dict
20IT425 14
4IT01: Python Programming
Output:
def multiply_all_items(dictionary):
result = 1
return result
Output:
def sort_dict_by_value(dictionary):
sorted_dict = dict(sorted(dictionary.items(), key=lambda item: item[1]))
return sorted_dict
Output:
20IT425 15
4IT01: Python Programming
def find_highest_3_values(dictionary):
sorted_items = sorted(dictionary.items(), key=lambda item: item[1], reverse=True)
highest_3_values = sorted_items[:3]
return highest_3_values
my_dict = {'a': 10, 'b': 30, 'c': 20, 'd': 50, 'e': 40}
highest_3_values = find_highest_3_values(my_dict)
print("Highest 3 values in the dictionary:", highest_3_values)
Output:
20IT425 16
4IT01: Python Programming
Practical 5 – String
5.1 WAP to remove left and right-hand side space form the string.
def remove_right_space(str):
st = str.rstrip()
return st
Output:
def remove_duplicates(input_str):
seen_chars = set()
result_str = ''
return result_str
Output:
20IT425 17
4IT01: Python Programming
5.3 WAP to check string starts and end with specific value.
Output:
def reverse_string(str):
st = str[::-1]
return st
Output:
20IT425 18
4IT01: Python Programming
def count_space(str):
c=0
for i in str:
if i!=' ':
c+=1
return c
Output:
20IT425 19
4IT01: Python Programming
20IT425 20
4IT01: Python Programming
Practical 6 – Functions
6.1 WAP to create a function that takes a number as a parameter and check the number is
prime or not.
def is_prime(number):
if number <= 1:
return False
for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
return False
return True
num = int(input("Enter number for finding prime or not : "))
if is_prime(num):
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
Output:
20IT425 21
4IT01: Python Programming
Output:
6.3 WAP to calculate total upper case and lower case letter of a given string.
def count_upper_lower(str):
upper_c=0
lower_c=0
for i in str:
if i.isupper():
upper_c+=1
if i.islower():
lower_c+=1
return upper_c,lower_c
st = eval(input("Enter any string for finding upper and lower from it : "))
print("No of upper and lower char respectively ",count_upper_lower(st))
20IT425 22
4IT01: Python Programming
Output:
6.4 WAP to arrange given number in descending orders import module of ascending order.
def arrange_in_descending(numbers_list):
sorted_numbers_descending = sorted(numbers_list, reverse=True)
return sorted_numbers_descending
numbers = [10, 5, 8, 3, 7, 2]
sorted_descending = arrange_in_descending(numbers)
print("Numbers in descending order:", sorted_descending)
Output:
20IT425 23
4IT01: Python Programming
20IT425 24
4IT01: Python Programming
Practical 7 – OOP
7.1 WAP to add complex number using class and object.
class ComplexNumber:
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary
def add(self, other):
new_real = self.real + other.real
new_imaginary = self.imaginary + other.imaginary
return ComplexNumber(new_real, new_imaginary)
def __str__(self):
if self.imaginary >= 0:
return f"{self.real} + {self.imaginary}i"
else:
return f"{self.real} - {abs(self.imaginary)}i"
if __name__ == "__main__":
complex1 = ComplexNumber(3, 2)
complex2 = ComplexNumber(1, -4)
result = complex1.add(complex2)
print("Complex Number 1:", complex1)
print("Complex Number 2:", complex2)
print("Sum:", result)
Output:
20IT425 25
4IT01: Python Programming
Output:
20IT425 26
4IT01: Python Programming
class InputHandler:
def get_positive_numbers(self):
num_list = []
while True:
try:
num = int(input("Enter a positive number (or 0 to stop): "))
if num < 0:
print("Please enter a positive number.")
elif num == 0:
break
else:
num_list.append(num)
except ValueError:
print("Invalid input. Please enter a valid number.")
return num_list
20IT425 27
4IT01: Python Programming
if num_list:
result = self.multiply_positive_numbers(num_list)
print("Result:", result)
else:
print("No positive numbers entered.")
if __name__ == "__main__":
app = PositiveNumberMultiplierApp()
app.run()
Output:
20IT425 28
4IT01: Python Programming
7.4 WAP to create employee salary system for PF, TDS and 80C.
class EmployeeSalarySystem:
def __init__(self, name, basic_salary, hra, medical_allowance, other_allowance,
investments_80c):
self.name = name
self.basic_salary = basic_salary
self.hra = hra
self.medical_allowance = medical_allowance
self.other_allowance = other_allowance
self.investments_80c = investments_80c
def calculate_pf(self):
pf_rate = 0.12 # Assuming 12% of basic salary for PF deduction
pf_deduction = self.basic_salary * pf_rate
return pf_deduction
def calculate_tds(self):
tds_rate = 0.10 # Assuming 10% of total taxable income for TDS deduction
taxable_income = self.basic_salary + self.hra + self.medical_allowance +
self.other_allowance - self.investments_80c
tds_deduction = taxable_income * tds_rate
return tds_deduction
def calculate_net_salary(self):
pf_deduction = self.calculate_pf()
tds_deduction = self.calculate_tds()
total_deductions = pf_deduction + tds_deduction
net_salary = self.basic_salary + self.hra + self.medical_allowance + self.other_allowance -
total_deductions
20IT425 29
4IT01: Python Programming
return net_salary
if __name__ == "__main__":
name = input("Enter employee name: ")
basic_salary = float(input("Enter basic salary: "))
hra = float(input("Enter HRA: "))
medical_allowance = float(input("Enter medical allowance: "))
other_allowance = float(input("Enter other allowance: "))
investments_80c = float(input("Enter investments under 80C: "))
net_salary = employee.calculate_net_salary()
print(f"Net Salary for {employee.name}: {net_salary:.2f}")
Output:
20IT425 30