100% found this document useful (1 vote)
373 views10 pages

AI Lab 1

The document describes a lab assignment on Python programming. The objectives are to install Python, learn the IDLE interface, and write basic Python code. The tasks involve taking user input, conditional statements, defining functions, creating classes with inheritance, and importing modules. Programs are written to demonstrate integer input validation, unit conversions with functions, and calculator classes with additional mathematical methods.

Uploaded by

Fatima Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
373 views10 pages

AI Lab 1

The document describes a lab assignment on Python programming. The objectives are to install Python, learn the IDLE interface, and write basic Python code. The tasks involve taking user input, conditional statements, defining functions, creating classes with inheritance, and importing modules. Programs are written to demonstrate integer input validation, unit conversions with functions, and calculator classes with additional mathematical methods.

Uploaded by

Fatima Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Artificial Intelligence

CSL 411

Lab Journal

Fatima Saeed Khan


01-134182-015
BSCS 6A

Department of Computer Science


BAHRIA UNIVERSITY, ISLAMABAD
Lab # 1: Introduction to Python

Objectives:

To be able to install python. Introduction to python IDLE and learning to code in python

Tools Used:

IDLE (Python 3.4 GUI Python)

Submission Date: 3/4/21

Evaluation: Signatures of Lab Engineer:


Task # 1:

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

print('Negative changed to zero')

elif x == 0:

print('Zero')

elif x == 1:

print('Single')

else:

print('More')

Procedure/Program:

x = int(input("Please enter an integer: ")) #typecasting


if x < 0:
x=0
print('Negative changed to zero')
elif x == 0:
print('Zero')
elif x == 1:
print('Single')
else:
print('More')
Result/Output:

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:

1. Write a simple unit calculator program. Follow the steps below:


a. Declare and define a function named Menu which displays a list of choices for user such
as meter to kilometer, kilometer to meter, centimeter to meter, & centimeter to
millimeter. It takes the choice from user as an input and return.
b. Define and declare a separate function for each choice.
c. In the main body of the program call respective function depending on user’s choice.
d. Program should not terminate till user chooses option to “Quit”.

Procedure/Program:

def Menu ():


print("Choose Type of Conversion:\n1. Meters to Kilometers\n2. Kilometers to Meteres\n3.
Centimeters to Meters\n4. Centimeters to Milimeters\n5. Quit")
choice = str(input())
return choice;

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:

1. Create a class name basic_calc with following attributes and methods;


Two integers (values are passed with instance creation)
Different methods such as addition, subtraction, division, multiplication
Create another class inherited from basic_calc named s_calc which should have the following
additional methods;
Factorial, x_power_y,log, ln etc

2. Modify the classes created in the above task under as follows:


Create a module name basic.py having the class name basic_calc with all the attributes and
methods defined before.
Now import the basic.py module in your program and do the inheritance step defined before i.e.
Create another class inherited from basic_calc named s_calc which should have the following
additional methods;
Factorial, x_power_y, log, ln etc

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.

You might also like