SBL
NAME: AMIL GAURI
MOODLE ID: 21106004
CSE(AIML)
ROLL NO: 20
EXPERIMENT 5
1)Create 4 functions add,sub,mul,div that performs
addition ,subtraction,multiplication and division of two
numbers and name this file as calc.py. Import this
module to use all operations of calc.
PROGRAM
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))
print("Enter which operation would you like to perform?")
ch = input("Enter any of these char for specific operation +,-,*,/: ")
result = 0
if ch == '+':
result = num1 + num2
elif ch == '-':
result = num1 - num2
elif ch == '*':
result = num1 * num2
elif ch == '/':
result = num1 / num2
else:
print("Input character is not recognized!")
print(num1, ch , num2, ":", result)
OUTPUT
2)Implement the above program using packages.
PROGRAM
def add(P, Q):
# This function is used for adding two numbers
return P + Q
def subtract(P, Q):
# This function is used for subtracting two numbers
return P - Q
def multiply(P, Q):
# This function is used for multiplying two numbers
return P * Q
def divide(P, Q):
# This function is used for dividing two numbers
return P / Q
# Now we will take inputs from the user
print ("Please select the operation.")
print ("a. Add")
print ("b. Subtract")
print ("c. Multiply")
print ("d. Divide")
choice = input("Please enter choice (a/ b/ c/ d): ")
num_1 = int (input ("Please enter the first number: "))
num_2 = int (input ("Please enter the second number: "))
if choice == 'a':
print (num_1, " + ", num_2, " = ", add(num_1, num_2))
elif choice == 'b':
print (num_1, " - ", num_2, " = ", subtract(num_1, num_2))
elif choice == 'c':
print (num1, " * ", num2, " = ", multiply(num1, num2))
elif choice == 'd':
print (num_1, " / ", num_2, " = ", divide(num_1, num_2))
else:
print ("This is an invalid input")
OUTPUT
3) Verify a user's email using Regular expressions
PROGRAM
import re
# Make a regular expression
# for validating an Email
regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b'
# Define a function for
# for validating an Email
def check(email):
# pass the regular expression
# and the string into the fullmatch() method
if(re.fullmatch(regex, email)):
print("Valid Email")
else:
print("Invalid Email")
# Driver Code
if __name__ == '__main__':
# Enter the email
email = "ankitrai326@gmail.com"
# calling run function
check(email)
email = "my.ownsite@our-earth.org"
check(email)
email = "ankitrai326.com"
check(email)
OUTPUT