Python Practical programs
Python Practical programs
def atm_money_distribution(amount):
# Available denominations in most ATMs
denominations = [2000, 500, 200, 100, 50, 20, 10]
# Go through each denomination and calculate how many of each are needed
for denom in denominations:
if amount >= denom:
count = amount // denom
distribution[denom] = count
amount = amount % denom
return distribution
# Example usage:
amount = int(input("Enter the amount to withdraw: "))
distribution = atm_money_distribution(amount)
if distribution:
print("Money Distribution:")
for denom, count in distribution.items():
print(f"{denom} INR: {count} notes")
Write a basic python program for Sequential Removal
def sequential_removal(lst, remove_count):
print("Original List:", lst)
for i in range(remove_count):
if len(lst) == 0:
print("List is already empty.")
break
# Remove the first element (or any element sequentially)
removed_element = lst.pop(0)
print(f"Removed: {removed_element}, Remaining List: {lst}")
# Example usage:
my_list = [10, 20, 30, 40, 50, 60]
remove_count = int(input("Enter the number of elements to remove sequentially: "))
sequential_removal(my_list, remove_count)
Write a basic python program for String validation
def string_validation(s):
print(f"Validating string: '{s}'")
# Example usage:
user_string = input("Enter a string to validate: ")
string_validation(user_string)
Write a basic python program for Number Operations
def number_operations(num1, num2):
print(f"Numbers: {num1}, {num2}")
# Addition
addition = num1 + num2
print(f"Addition: {num1} + {num2} = {addition}")
# Subtraction
subtraction = num1 - num2
print(f"Subtraction: {num1} - {num2} = {subtraction}")
# Multiplication
multiplication = num1 * num2
print(f"Multiplication: {num1} * {num2} = {multiplication}")
# Division
if num2 != 0:
division = num1 / num2
print(f"Division: {num1} / {num2} = {division}")
else:
print("Division by zero is not allowed.")
# Modulus (remainder)
if num2 != 0:
modulus = num1 % num2
print(f"Modulus: {num1} % {num2} = {modulus}")
# Floor Division
if num2 != 0:
floor_division = num1 // num2
print(f"Floor Division: {num1} // {num2} = {floor_division}")
# Example usage:
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
number_operations(num1, num2)
Write a basic python program for Duplicate Number
def find_duplicates(nums):
duplicates = []
seen = set() # A set to keep track of seen numbers
return duplicates
# Example usage:
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
duplicates = find_duplicates(numbers)
if duplicates:
print("Duplicate numbers:", duplicates)
else:
print("No duplicates found.")
Write a basic python program for Intersection of two arrays
def find_intersection(arr1, arr2):
# Convert both lists to sets to find common elements
set1 = set(arr1)
set2 = set(arr2)
# Example usage:
array1 = list(map(int, input("Enter the first array (numbers separated by spaces): ").split()))
array2 = list(map(int, input("Enter the second array (numbers separated by spaces): ").split()))
if intersection:
print("Intersection of the two arrays:", intersection)
else:
print("No common elements.")
write a basic python program for finding First unique character
def first_unique_character(s):
# Create a dictionary to count the frequency of each character
char_count = {}
# Iterate over the string again to find the first unique character
for char in s:
if char_count[char] == 1:
return char
# Example usage:
user_input = input("Enter a string: ")
result = first_unique_character(user_input)
if result:
print(f"The first unique character is: '{result}'")
else:
print("No unique character found.")
Output:
Enter a string: swiss
The first unique character is: 'w'
Write a basic python program to Reverse vowels in a given string
def reverse_vowels(s):
vowels = "aeiouAEIOU" # Define vowels (both uppercase and lowercase)
s_list = list(s) # Convert string to a list for easier manipulation
left, right = 0, len(s) - 1
# Example usage:
user_input = input("Enter a string: ")
result = reverse_vowels(user_input)
print("String after reversing vowels:", result)
Write a basic python program to find Twin Primes
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
def find_twin_primes(limit):
twin_primes = []
# Loop through numbers and check for twin primes
for num in range(2, limit):
if is_prime(num) and is_prime(num + 2):
twin_primes.append((num, num + 2))
return twin_primes
# Example usage:
limit = int(input("Enter the limit to find twin primes: "))
twin_primes = find_twin_primes(limit)
if twin_primes:
print("Twin primes up to", limit, ":")
for pair in twin_primes:
print(pair)
else:
print("No twin primes found.")
Output:
Enter the limit to find twin primes: 20
Twin primes up to 20 :
(3, 5)
(5, 7)
(11, 13)
(17, 19)
Write an object oriented python program to calculate Electricity bill
# Define the Customer class
class Customer:
def __init__(self, name, customer_id):
self.name = name
self.customer_id = customer_id
# Example usage
customer1 = Customer("Alice", 101)
units_consumed = float(input("Enter the number of units consumed: "))
Output:
Enter your first name: Gomathi
Enter your last name: Nayagam
Generated Username: gomathi.nayagam
Generated Password: v{9dYz\4;"s{
Write a object oriented python program to implement Student attendance record
# Define the Student class
class Student:
def __init__(self, name, roll_number):
self.name = name
self.roll_number = roll_number
self.attendance_record = [] # List to store attendance ('Present' or 'Absent')