0% found this document useful (0 votes)
151 views13 pages

AI Lab Manual Solution

The document provides a lab manual for an artificial intelligence course. The summary covers the key topics: 1) The manual covers installation of Python and various AI libraries like NumPy, Pandas etc. using Windows PowerShell. 2) Lab sessions include introductions to Python input/output, if-else statements, logical operators, and for loops. Examples provided calculate math problems, grades, electricity bills and more. 3) Exercises prompt users for input and use conditionals and loops to display outputs like name/address, circle measurements, swapped numbers, time conversions and mathematical operations.

Uploaded by

hassaanaftab6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views13 pages

AI Lab Manual Solution

The document provides a lab manual for an artificial intelligence course. The summary covers the key topics: 1) The manual covers installation of Python and various AI libraries like NumPy, Pandas etc. using Windows PowerShell. 2) Lab sessions include introductions to Python input/output, if-else statements, logical operators, and for loops. Examples provided calculate math problems, grades, electricity bills and more. 3) Exercises prompt users for input and use conditionals and loops to display outputs like name/address, circle measurements, swapped numbers, time conversions and mathematical operations.

Uploaded by

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

LAB MANUAL

Artificial Intelligence

The Institute of Management Sciences


Powered by: Hussain Sajid

Faculty of Computer Science


Artificial Intelligence
Lab#1 Installations
Sr# Programs
1 Installation of Python.

2 Introduction to windows PowerShell.


3 Installation of jupyter notebook using windows PowerShell
4 Installation of Numpy Library/Packing using windows PowerShell
5 Installation of Pandas Library/Packing using windows PowerShell
6 Installation of Matplotlib Library/Packing using windows PowerShell
7 Installation of Seaborn Library/Packing using windows PowerShell
8 Installation of Scipy Library/Packing using windows PowerShell

Lab 2
Introduction to python / Input / Output
1. Introduction to python IDLE
2. Writing first program in python.
3. Write a program that inputs name, age and address from the user and then displays these
values on the screen.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
address = input("Enter your address: ")
print("Name:", name)
print("Age:", age)
print("Address:", address)
4. Write a program that input two numbers and display their sum on screen.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
sum = num1 + num2
print("Sum= ", sum)
5. Write a program that inputs radius from the user and calculate area and circumference of
circle using formula and.
radius = float(input("Enter the radius of circle: "))
area = 3.14 * radius * radius
circ = 2 * 3.14 * radius

area = round(area,2)
circ = round(circ,2)
print("Area of circle is ",area)
print("Circumference of a circle is ",circ)
6. Write a program that inputs two numbers, swap their values without using third variable
and display them.
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))

print("Numbers before swapping")


print("First Number: ",x)
print("Second Number: ",y)

x = x + y
y = x - y
x = x - y
print("Numbers after swapping")
print("First Number: ",x)
print("Second Number: ",y)
7. Write a program that inputs time in seconds and convert it into HH:MM:SS format.
sec = int(input("Enter time in seconds: "))
hour = sec//3600
sec = sec%3600
minute = sec//60
sec = sec%60

print("Hours: ",hour)
print("Minutes: ",minute)
print("Seconds: ",sec)
8. Write a program that inputs two numbers and apply mathematical operation like addition,
subtraction, multiplication, division and remainder. And display the result on screen.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
sum = num1 + num2
sub = num1 - num2
multi = num1 * num2
divide = num1 / num2
remainder = num1 % num2

print("Sum= ",sum)
print("Subtract= ",sub)
print("Multiply= ",multi)
print("Divide= ",divide)
print("Remainder= ",remainder)
9. Write a program that input a character and display its ASCII code.
c = input("Enter a character: ")
asci = ord(c)
print("ASCII code of",c, "is" ,asci)
10. Write a program that will prompt the user to enter number of hours. It computes and
displays the number of weeks, days and hours within the input number of hours.
hours = int(input("Enter the number of hours: "))
week = hours//168
hours = hours%168
days = hours//24
hours = hours%24
print("Weeks: ",week)
print("Days: ",days)
print("Hours: ",hours)
11. Write a program that will prompt the user to enter the current rates of electricity, gas and
petrol per unit. Give each item’s rate an increment of 10%. Computer and display the
new prices per unit of electricity, gas and petrol.
electricity = int(input("Enter the current rate of
electricity: "))
gas = int(input("Enter the current rate of gas: "))
petrol = int(input("Enter the current rate of petrol: "))

electricity = electricity + (electricity*10/100)


gas = gas + (gas*10/100)
petrol = petrol + (petrol*10/100)
print("Electricity: ",electricity)
print("Gas: ",gas)
print("Petrol: ",petrol)
Lab 3
If –Else / use of logical operators in IF statement

1. Write a program that inputs marks and displays “Congratulations! You have passed.” If the
marks are 40 or more.
marks = int(input("Enter marks: "))
if marks>=40:
print("Congratulations! You are passed ")
2. Write a program that inputs marks of three subjects. If the average of marks is more than 80,
it displays two messages. “You are above standard!” and “Admission granted”.
marks1= int(input("Enter frst marks: "))
marks2= int(input("Enter second marks: "))
marks3= int(input("Enter third marks: "))
avg = (marks1 + marks2 + marks3)/2
if avg>=80:
print("You are above standard!")
print("Admission granted.")
3. Write a program that inputs a number and finds whether it is even or odd using if-else
statement.
num= int(input("Enter the number: "))
if num %2==0:
print("This is even number")
else:
print("this is odd number")
4. Write a program that inputs salary and grade. It adds 50% bonus if grade is greater than 15. It
adds 25% bonus if the grade is 15 or less and then displays the total salary.
salary= int(input("Enter the salary: "))
grade= int(input("Enter the grade: "))
bonus = 0
total_salary = 0
if grade >15:
bonus = salary * 50/100
else:
bonus = salary * 25/100
total_salary = bonus + salary
print("Salary: ",salary)
print("Bonus: ",bonus)
print("Total_salary: ",total_salary)
5. Write a program that inputs test score of a student and displays his grade according to the
following criteria:
Test score Grade
>= 90 A
80 – 89 B
70 – 79 C
60 – 69 D
BELOW 60 F

marks= int(input("Enter your marks: "))


if marks>=90 and marks<=100:
print("Grade A")
elif marks>=80 and marks<=89:
print("Grade B")
elif marks>=70 and marks<=79:
print("Grade C")
elif marks>=60 and marks<=69:
print("Grade D")
else:
print("Grade F")
6. Write a program that calculates the electricity bill. The rates of electricity per unit are as
follows:
a. If the units consumed are <=300, then cost is Rs. 2 per unit.
b. If the units consumed are >300 and <=500, then cost is Rs. 5 per unit.
c. If the units consumed exceeds 500, then cost is Rs. 7 per unit.
A line rent of Rs. 150 is also added to the total bill and a surcharge of 5% extra if the bill
exceeds Rs. 2000. Calculate the total bill with all the conditions given above.
units = int(input("Enter number of units: "))
cost = 0
scharge = 0
if units<=300:
cost = 2
elif units>300 and units<=500:
cost = 5
else:
cost = 7
bill = cost * units
lineRent=150
total = bill + lineRent
if total>2000:
scharge = total*5/100
total = total + scharge
print("Consumed units: ",units)
print("Total Bill: ",total)
7. Write a program that inputs salary. If salary is 20,000 or more, it deduces 7% of salary. If
salary is 10,000 or more but less than 20,000, it deduces 1000 from salary. If salary is less
than 10,000 it deduces nothing. It finally displays the net salary.
salary = int(input("Enter the salary: "))
deduct = 0
if salary>=20000:
deduct = salary *7/100
elif salary>= 10000 and salary<20000:
deduct = 1000
else:
deduct = 0
total_salary = salary - deduct
print("Net salary: ",total_salary)
8. Write a program that inputs three numbers and displays the maximum and minimum number
by using logical operators.

9. Write a program that allows the user to enter any character through keyboard and determine
whether is capital letter, small letter, a digit number or a special symbol.

10. Write a program that inputs three digits and displays all possible combinations of these
digits.
Lab 4
Loop Introduction / For Loop
1. Write a program that displays “Pakistan” for five times using for loop.
for i in range(0,5):
print("Pakistan")
2. Write a program that displays counting from 1 to 10 using for loop.
for i in range(1,11):
print(i)
3. Write a program that displays first five numbers and their sum using for loop.
sum = 0
for i in range(1,6):
print(i)
sum = sum + i
print("Sum = ",sum)
4. Write a program that inputs a number from the user and displays a table of that number using
for loop.
table = int(input("Enter table: "))
for i in range(1,11):
print(table, " * ", i, "=", table * i)
5. Write a program that displays sum of the following series using for loop.

r=1
for c in range(2,101,2):
r = r + 1/c

print("Sum = ",round(r,2))
6. Write a program that inputs starting and ending number from the user and displays all even
numbers in the given range using for loop.
start = int(input("Enter starting value: "))
end=int(input("Enter ending value: "))
for i in range(start,end+1):
if i%2==0:
print(i)
7. Write a program that finds the sum of the squares of integers from 1 to n. where n is a
positive value entered by the user. ( )
n = int(input("Enter value: "))
sum =0
for i in range(1,n+1):
sum = sum + (i*i)
print("Sum of squares = ",sum)
8. Write a program that displays counting from 1 to 10 using while loop.
i=1
while i<=10:
print(i)
i = i+1
9. Write a program that displays first five numbers and their sum using while loop.
sum = 0
i=1
while i<=5:
print(i)
sum = sum + i
i=i+1
10. Write a program that inputs a number from the user and displays a table of that number using
while loop.
table = int(input("Enter table: "))
i=1
while i<=10:
print(table, " * ", i, "=",table*i)
i=i+1
Lab 5
While Loop
1. Write a program that uses a while loop to enter number from the user and then display it. The
loop is terminated when user enters -1.
num = int(input("Enter the number: "))
while num != -1:
print(num)
num = int(input("Enter the number: "))
2. Write a program that inputs a number from the user and displays a table of that number using
while loop.
table = int(input("Enter table: "))
i=1
while i<=10:
print(table, " * ", i, "=",table*i)
i=i+1
3. Write a program that inputs an integer and displays the sum of its digits. For example the
program should displays 9 if user enter 135.
num = (input("Enter a number: "))
sum = 0
for digit in str(num):
sum += int(digit)

print("Sum of",num,"is",sum)
4. Write a program that displays sum of the following series using while loop.

c=2
r=1
while c<=100:
r = r + 1/c
c+=2

print("Sum = ",round(r,2))
5. Write a program that inputs a number and check whether it is an Armstrong number or not. A
number is armstrong if the sum of the cubes of its digits is equal to the number itself. For
example, 371 is an armstrong number since
num = int(input("Enter a number: "))
n = num
sum = 0
r=0
while n!=0:
r = n%10
sum = sum + (r**3)
n = n//10

if sum == num:
print(num,"is an armstrong number")
else:
print(num,"is not an armstrong number")
6. Write a program that inputs numbers until the user enters a negative number. The program
calculates the average, maximum and minimum of all positive numbers.
n = int(input("Enter a number: "))
sum=0
minimum = n
maximum = n
avg=0
c=0
while n>0:
sum = sum + n
c+=1
if n>maximum:
maximum = n
elif n<minimum:
minimum = n
n = int(input("Enter a number: "))
if c == 0:
print("No Positive number entered!!")
else:
avg = sum/c
print("You entered",c,"numbers")
print("Average: ",avg)
print("Maximum: ", maximum)
print("Minimum: ", minimum)
7. Write a program that inputs a number and checks whether it is a palindrome or not. A
palindrome is a number that reads the same backwards as forwards such 62526 and 4994
num=int(input("Enter a number:"))
temp=num
rev=0
while(num>0):
dig=num%10
rev=rev*10+dig
num=num//10
if(temp==rev):
print("The number is palindrome!")
else:
print("Not a palindrome!")

Lab#6 Nested Loop


Sr# Programs
Write a program to display the following pattern:
* * *
* * *
1
* * *
* * *
* * *
rows = 5
columns = 3
for i in range(1,rows+1):
for j in range(1,columns+1):
print("*", end=' ')
print('\n')
Write a program that inputs the height of triangle and displays a triangle of
characters. For example, if the user enters 5, it displays the following:
A
2 BC
DEF
GHIJ
KLMNO

Write a program that displays the following output:


*******
******
*****
3
****
***
**
*
n = int(input("How many rows do you want: "))
for x in range(1,n+1):
for y in range(x,n+1):
print("*",end="")
print(" ")
Write a program that displays the following using do-while loop.
4444
4 333
22
1
num =int(input("Enter the number of rows: "))
for i in range(num,0,-1):
for j in range(i,0,-1):
print(i,end="")
print(" ")
Write a program that displays the following using nested for loop
*****
****
5
***
**
*
num = int(input("Enter the num of rows: "))
for x in range(1,num+1):
for y in range(1,x):
print(" ", end="")
for z in range(x,num+1):
print("*",end="")
print(" ")
Write a program that displays the following output
123456
12345
6 1234
123
12
1
num = int(input("Enter the num of rows: "))
for x in range(1,num+1):
for y in range(x,1, -1):
print(" ", end="")
for z in range(1,num+2-x):
print(z,end="")
print(" ")
Write a program that displays a diamond of asterisks using loop.
*
***
*****
*******
7
*********
*******
*****
***
*
rows = 5
k = 2 * rows - 2
for i in range(0, rows):
for j in range(0, k):
print(end=" ")
k=k-1
for j in range(0, i + 1):
print("* ", end="")
print("")

k = rows - 2

for i in range(rows, -1, -1):


for j in range(k, 0, -1):
print(end=" ")
k=k+1
for j in range(0, i + 1):
print("* ", end="")
print("")
Lab 7
Intro to Functions
1. Write a program that displays a message ‘Programming makes life interesting’ on screen
using functions.
def test():
print("Programming makes life interesting!!")
test()
2. Write a program that inputs a number in main function and passes the number to a
function. The function displays table of that number.
def test(n):
for i in range(1,11):
print(n,"*",i,"=",n*i)
num = int(input("Enter a number: "))
test(num)
3. Write a program that inputs two numbers and one arithmetic operator in main function
and passes them to a function. The function applies arithmetic operation on two numbers
on the basis of the operator entered by user using switch statement.
def test(num1, num2, op):
if op == "+":
print(num1,"+",num2,"=",num1+num2)
elif op == "-":
print(num1,"-",num2,"=",num1-num2)
elif op == "*":
print(num1,"*",num2,"=",num1*num2)
elif op == "/":
print(num1,"/",num2,"=",num1/num2)
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
opr = input("Enter operator: ")
test(a, b, opr)
4. Write a program that inputs a number in main function and passes the number to a
function. The function displays the factorial of that number.
def test(n):
f=1
for i in range(1,n+1):
f = f*i
return f
num = int(input("Enter a number: "))
factorial = test(num)
print("Factorial of",num,"is",factorial)
5. Write a program that inputs two integers. It passes first integer to a function that
calculates and returns its square. It passes second integer to another function that
calculates and returns its cube. The main function adds both returned values and displays
the result.
def sqr(n):
return n*n

def cb(n):
return n**3

num = int(input("Enter a number: "))


square = sqr(num)
cube = cb(num)
total = square + cube
print("Sum = ",total)
6. Write a program the inputs base and height of a triangle in the main function and passes
them to a function. The function finds the area of triangle and return it to main function
where it is displayed on screen.
def area(b,h):
return (b*h)/2

b = int(input("Enter base of triangle: "))


h = int(input("Enter height of triangle: "))
ar = area(b,h)
print("Area = ",ar)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy