9) a. Modules Examples




Wilfred, the magician, claims to make you younger/older by the number of years you want. Please create a module that helps Wilfred do both the operations:

1
2
3
4
5
6
7
8
Program:
# abracadabra.py

def make_older(age, number_of_years):
    return age + number_of_years
   
def make_younger(age, number_of_years):
    return age - number_of_years




Gilbert is a big businessman with multiple streams of income and wants to calculate his income tax for the last fiscal year. He has written the code for the same. For the sake of calculation lets assume that the income tax is income*10%. Please fill in the blanks to calculate the tax that needs to be paid by Gilbert.

1
2
3
4
5
Program:
# tax_calculator.py

def income_tax(income: float):
    return 0.1 * income
1
2
3
4
5
6
7
8
9
10
11
#main.py

from tax_calculator import __________

income_from_securities = 1200.45
income_from_salary = 10000.00
income_from_rent = 20000.00

total_income_tax = income_tax(income_from_securities) + income_tax(income_from_salary) + income_tax(income_from_rent)

print('The total tax is ', total_income_tax)




Mighel has a task that requires a set of string operations and wants multiple developers to be working on it. The tasks range from finding the length of string to finding if a substring is present or not. She has given the task of finding if a string is palindrom or not to you. As this task will be divided among various developers, you only want to create your own module for it. Write a program to do the same.

1
2
3
4
5
Program:
# string_palindrom.py

def palin_check(check_str):
    return check_str==check_str[::-1]




Professor Ben has a list of student names which he acquired from Taylor. Taylor instead acquired the list from Barnabas who has a habit of toggling the alternate alphabets in upper and lower case. Ben wants this list to be standardized by capitalizing the first alphabet of first name and the first alphabet of last name. Ben needs to give this list to his dean as quickly as possible. Help Ben create a module with a method capitalize_name() to make the names in a standard format. For simplicity, lets assume that all names are first name and last name separated by a space.

1
2
3
4
5
Program:
# capitalize.py

def capitalize_name(name):
    return name.split(' ')[0].capitalize() + " " + name.split(' ')