AI Lab 1
AI Lab 1
CSL 411
Lab Journal
Objectives:
To be able to install python. Introduction to python IDLE and learning to code in python
Tools Used:
Open IDLE and run the following program. Try different integer values for separate runs of the
program. Play around with the indentation of the program lines of code and run it again. See what
happens. Make a note of what changes you made and how it made the program behave. Also note any
errors, as well as the changes you need to make to remove the errors.
x = input("Please enter an integer: ")
if x < 0:
x = 0
elif x == 0:
print('Zero')
elif x == 1:
print('Single')
else:
print('More')
Procedure/Program:
Analysis/Conclusion:
Because Python takes input as a string, we cannot use comparison operators on it. Hence, we need to
typecast the input.
Task # 2:
Procedure/Program:
def mtokm(v):
return float(v)/1000
def kmtom(v):
return float(v)*1000
def cmtom(v):
return float(v)/100
def cmtomm(v):
return float(v)*10
while 1:
x=Menu()
if x == '1':
val = int(input("Enter Value: "))
print (mtokm(val), "KM")
elif x == '2':
val = int(input("Enter Value: "))
print (kmtom(val), "Meters")
elif x == '3':
val = int(input("Enter Value: "))
print (cmtom(val), "Meters")
elif x == '4':
val = int(input("Enter Value: "))
print (cmtomm(val), "Millimeters")
elif x == '5':
break
Result/Output:
Main Menu:
Sample Output:
Analysis/Conclusion:
A separate function is created using def just for displaying the main menu to the user and taking their
choice from it. Then, different functions for each type of conversions are defined with their respective
formulas. Finally, in the main function, we used a while loop to infinitely display the main menu after
each conversion until the user decides to quit. Since the input is taken as a string by default, it should be
ensured that typecasting to string is done for each value entered by the user to achieve the correct
output.
Task # 3:
Procedure/Program:
1.
import math
class basic_calc:
def __innit__(self, int1, int2) :
self.x = int1
self.y = int2
def add(self) :
return self.x + self.y
def subtract(self) :
return self.x - self.y
def multiply(self) :
return self.x * self.y
def divide(self) :
return self.x / self.y
class s_calc(basic_calc):
def fact(self):
return (math.factorial(x), math.factorial(y))
def power(self):
return pow(self.x, self.y)
def log(self):
return (math.log(self.x), math.log(self.y))
def ln(self):
return (math.log(self.x), math.log(self.y))
2.
- Creating basic.py
- Importing Module
import math
import basic
class s_calc(basic.basic_calc):
def fact(self):
return (math.factorial(self.x))
def power(self):
return pow(self.x, self.y)
def log(self):
return (math.log(self.x), math.log(self.y))
def ln(self):
return (math.log(self.x), math.log(self.y))
def Menu():
print("ENTER CHOICE\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\5. Finding
Factorial\n6. Power\n7. Logarithm\n8. Natural Log (ln)\n8. Exit")
choice = str(input())
return choice
while 1:
ch=Menu()
if ch == '1':
x = int(input("First Value"))
y = int(input("Second Value"))
obj = s_calc(x,y)
print("Sum = ",obj.add(),"\n")
elif ch == '2':
x = int(input("First Value"))
y = int(input("Second Value"))
obj = s_calc(x,y)
print("Difference = ",obj.subtract(),"\n")
elif ch == '3':
x = int(input("First Value"))
y = int(input("Second Value"))
obj = s_calc(x,y)
print("Product = ",obj.multiply(),"\n")
elif option == '4':
x = int(input("First Value"))
y = int(input("Second Value"))
obj = s_calc(x,y)
print("Quotient = ",obj.divide(),"\n")
elif option == '5':
x = int(input("Value"))
y=int(0)
obj = s_calc(x,y)
print("Factorial = ",obj.fact(),"\n")
elif option == '6':
x = int(input("First Value"))
y = int(input("Second Value"))
obj = s_calc(x,y)
print("Power = ",obj.power(),"\n")
elif option == '7':
x = int(input("First Value"))
y = int(input("Second Value"))
obj = s_calc(x,y)
print("Log = ",obj.log(),"\n")
elif option == '8':
x = int(input("First Value"))
y = int(input("Second Value"))
obj = s_calc(x,y)
print("Ln = ",obj.ln(),"\n")
elif option == '9':
break
Result/Output:
Analysis/Conclusion:
To create a new module we have simply created a new python file, named it basic.py and saved it. Then
we entered our basic operations code into it. In order to import it into our main file we used import
keyword. We defined the other types of calculation into the main file by using inheritance from the
basic class.