Fa22 BCS 232
Fa22 BCS 232
Artificial Intelligence
Sahiwal Campus
Lab#01_Activities:
Activity 1:
Let us take an integer from user as input and check whether the given value is
even or not. If the given value is not even then it means that it will be odd. So here we
need to use if-else statement an demonstrated below.
Program
n=int(input("Enter a number"))
if n%2==0:
print(n,"The given number is even ")
else:
print(n,"The given number is odd")
Output
Enter a number3
3 The given number is odd
---------------------------------------------------------------------------------------------------------------------
Activity 2:
Write a Python code to keep accepting integer values from user until 0 is entered.
Display sum of the given values.
Program
sum=0
s=int(input("Enter an interger value..."))
n=int(s)
while n!=0:
sum=sum+n
s=int(input("Enter the integer value..."))
n=int(s)
print("sum of given values is ",sum)
Output
Enter an integer value...2
Enter the integer value...3
Enter the integer value...4
Enter the integer value...5
Enter the integer value...0
sum of given values is 14
---------------------------------------------------------------------------------------------------------------------
Activity 3:
Write a Python code to accept an integer value from user and check that whether the
given value is
prime number or not.
Program
# Write a Python code to accept an integer value from user and check that whether the
given value is
# prime number or not
isPrime =True
i=2
n=int(input("Enter s number ="))
while i<n:
remainder=n%i
if remainder==0:
isPrime=False
break
else:
i=+1
if isPrime:
print("Number is prime ")
else:
print("Number is not prime")
output
Enter s number =2
Number is prime
---------------------------------------------------------------------------------------------------------------------
Activity 4:
Accept 5 integer values from user and display their sum. Draw flowchart before
coding in python.
Program
# Accept 5 integer values from user and display their sum. Draw flowchart before
# coding in python.
#
sum=0
i=0
while i<=4:
s=int(input("Enter a numbe = "))
n=int(s)
sum=sum+n
i=i+1
print("sum is ",sum)
output
Enter a numbe = 3
Enter a numbe = 4
Enter a numbe = 5
Enter a numbe = 6
Enter a numbe = 7
sum is 25
---------------------------------------------------------------------------------------------------------------------
Activity 5:
Calculate the sum of all the values between 0-10 using while loop.
Program
# Accept 5 integer values from user and display their sum. Draw flowchart before
# coding in python.
sum=0
i=1
while i<=10:
sum=sum+i
i=i+1
print("the sum of first ten number is ",sum)
output
the sum of first ten number is 55
---------------------------------------------------------------------------------------------------------------------
Activity 6:
Take input from the keyboard and use it in your program.
Program
# Take input from the keyboard and use it in your program.
name=input("What is your name: ")
print("Hello Brother ",name)
Lab 02
Python Lists and Dictionaries
Activity 1
Accept two lists from user and display their join.
Program
# Accept two lists from user and display their join.
myList1=[]
print("Enter objects of first list...")
for i in range(5):
val=int(input("Enter a value: "))
n=int(val)
myList1.append(n)
myList2=[]
print("Enter the object of second list ...")
for i in range(5):
val=int(input("Enter the value "))
n=int(val)
myList2.append(n)
list3=myList2+myList1
print(list3)
output
Enter objects of first list...
Enter a value: 3
Enter a value: 4
Enter a value: 5
Enter a value: 6
Enter a value: 7
Enter the object of second list ...
Enter the value 8
Enter the value 9
Enter the value 7
Enter the value 6
Enter the value 5
[8, 9, 7, 6, 5, 3, 4, 5, 6, 7]
---------------------------------------------------------------------------------------------------------------------
Activity 2:
A palindrome is a string which is same read forward or backwards.
For example: "dad" is the same in forward or reverse direction. Another example is
"aibohphobia"
which literally means, an irritable fear of palindromes.
Write a function in python that receives a string and returns True if that string is a
palindrome and
False otherwise. Remember that difference between upper and lower case characters are
ignored
during this determination.
Program
def isPalindrome(word):
tem=word[::-1]
if tem.capitalize()==word.capitalize():
return True
else:
return False
print(isPalindrome("deed"))
output
True
---------------------------------------------------------------------------------------------------------------------
Activity 3:
Imagine two matrices given in the form of 2D lists as under; a = [[1, 0, 0], [0, 1, 0], [0, 0,
1] ]
b = [[1, 2, 3], [4, 5, 6], [7, 8, 9] ]
Write a python code that finds another matrix/2D list that is a product of and b, i.e., C=a*b
Program
# Imagine two matrices given in the form of 2D lists as under; a = [[1, 0, 0], [0, 1, 0], [0,
0, 1] ]
# b = [[1, 2, 3],
# ask to teacher
for indrow in range(3):
c=[]
a=[]
b=[]
c.append([])
for indcol in range(3):
c[indrow].append(0)
for indaux in range(3):
c[indrow][indcol ] += a[indrow][indaux] * b[indaux][indcol]
print(c)
output
---------------------------------------------------------------------------------------------------------------------
Activity 4:
A closed polygon with N sides can be represented as a list of tuples of N connected
coordinates, i.e.,
[ (x1,y1), (x2,y2), (x3,y3), . . . , (xN,yN) ]. A sample polygon with 6 sides (N=6).
Program
# Write a python function that takes a list of N tuples as input and returns the perimeter
of the
# polygon. Remember that your code should work for any value of N.
def perimeter(listing):
leng=len(listing)
perimeter=0
for i in range(0,leng-1):
dist=(((listing[i][0]-listing[i+1][0])**2)+ ((listing[i][1]-listing[i+1][1])**2))**0.5
perimeter=perimeter+dist
perimeter=perimeter + (((listing[0][0]-listing[leng-1][0])**2)+ ((listing[0][1]-listing[leng-1]
[1])**2))**0.5
return perimeter
L=[(1,3),(2,7),(2,9),(-1,8)]
print(perimeter(L))
output
14.670548092920545
---------------------------------------------------------------------------------------------------------------------
Activity 5:
Imagine two sets A and B containing numbers. Without using built-in set functionalities,
write your
own function that receives two such sets and returns another set C which is a symmetric
difference
of the two input sets. (A symmetric difference between A and B will return a set C which
contains
only those items that appear in one of A or B. Any items that appear in both sets are not
included in
C). Now compare the output of your function with the following built-in
functions/operators.
Program
def symmDiff(a,b):# function defined
e=set() #empty set
for i in a:#for loop used to access in a
if i not in b:
e.add(i)
for i in b:#for loop used to access in b
if i not in a:
e.add(i)
return e
set1={0,1,2,4,5}
set2={3,4,5,6,7}
print(symmDiff (set1,set2))
# varification using inbuilt function
print(set1.symmetric_difference(set2))
print(set2.symmetric_difference(set1))
print(set1^set2)
print(set2^set1)
output
{0, 3, 6, 7}
{0, 1, 2, 3, 6, 7}
{0, 1, 2, 3, 6, 7}
{0, 1, 2, 3, 6, 7}
{0, 1, 2, 3, 6, 7}
--------------------------------------------------------------------------------------------------------------------