0% found this document useful (0 votes)
29 views9 pages

Lab 2

Uploaded by

220391
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)
29 views9 pages

Lab 2

Uploaded by

220391
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/ 9

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING

EXPERIMENT NO 2

Lab Title: Python: Syntax, Libraries and Uninformed vs. Informed search
Student Name: Ibrahim, Huzaifa, Hassaan Reg. No: 220391, 222590, 222563

Objective:

LAB ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes
(5) (4) (3) (2) (1)
Ability to Conduct
Experiment
Ability to assimilate the
results
Effective use of lab
equipment and follows
the lab safety rules

Total Marks: Obtained Marks:

LAB REPORT ASSESSMENT:


Excellent Good Average Satisfactory Unsatisfactory
Attributes (5) (4) (3) (2) (1)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks:

Date: Signature:
ABSTRACT
In this lab we will be studying types of searches

Muhammad Ibrahim
220391
BCE 5-A

INTRODUCTION TO AI
Lab 2
Page 1 of 7

LAB 2

Code:
# Task 1

x=int(input("Enter a Number: "))


if (x==0):
print("Factorial of 0 is 1")
elif (x==1):
print("Factorial of 1 is 1")
elif (x==2):
print("Factorial of 2 is 2")
elif (x<0):
print("You entered a negative number which will result in complex number!")
elif (x>2):
fact=1
for i in range (1,x+1):
fact=fact*i
print("Factorial of",x,"is",fact)

Output:

September 25, 2024


Page 2 of 7

Code:
# Task 2

x=int(input("Enter a Number: "))


if (x==0):
print("The Number is Zero")
elif (x%2==0):
if (x<0):
print("The Number is Even Negative")
else:
print("The Number is Even Positive")
else:
if (x<0):
print("The Number is Odd Negative")
else:
print("The Number is Odd Positive")

Output:

September 25, 2024


Page 3 of 7

Code:
def calculate_letter_grade(average):
if average >= 90:
return 'A'
elif average >= 80:
return 'B'
elif average >= 70:
return 'C'
elif average >= 60:
return 'D'
else:
return 'F'

# Function to display results in a readable format


def display_results(students):
print("\nStudent Grades:")
print("----------------------------")
for student in students:
print(f"Name: {student['name']}, Average Grade:
{student['average']:.2f}, Letter Grade: {student['letter_grade']}")

# Main program
def main():
# Asking for the number of students (with validation for at least 3)
num_students = int(input("Enter the number of students (minimum 3): "))
while num_students < 3:
print("Please enter at least 3 students.")

September 25, 2024


Page 4 of 7

num_students = int(input("Enter the number of students (minimum 3):


"))

# Predefined subjects
subjects = ["Math", "Science", "English"]

# List to store student data


students = []

# Loop through the number of students


for _ in range(num_students):
student = {}
student['name'] = input("\nEnter the student's name: ")

# List to store grades for each subject


grades = []

# Get grades for each subject


for subject in subjects:
grade = float(input(f"Enter the grade for {subject} (0-100): "))
while grade < 0 or grade > 100:
print("Please enter a valid grade between 0 and 100.")
grade = float(input(f"Enter the grade for {subject} (0-100):
"))
grades.append(grade)

# Calculate the average grade for the student


average_grade = sum(grades) / len(subjects)
student['average'] = average_grade

# Determine the letter grade based on the average


student['letter_grade'] = calculate_letter_grade(average_grade)

# Add the student to the list


students.append(student)

# Display the results


display_results(students)

# Call the main function to run the program


main()

September 25, 2024


Page 5 of 7

Output:

September 25, 2024


Page 6 of 7

Code:

graph = { # graph Declaration


'A': ['B', 'D'],
'B': ['C', 'F'],
'C': ['E', 'G', 'H'],
'D': ['F'],
'E': ['B', 'F'],
'F': ['A'],
'G': ['E', 'H'],
'H': ['A']
}

def search(graphs, start, target):


visited = [start] # List to keep track of visited nodes.
stack = [start] # Initialize a stack for DFS

if start == target: # If start is target, return start


return start

else:
while stack: # Continue as long as there are nodes to explore
temp = stack.pop() # Pop from the stack (LIFO)
if target == temp:
return visited
else:
for sibling in graphs[temp]: # Explore each adjacent node
if sibling not in visited: # If not visited, add to
visited and stack
stack.append(sibling)
visited.append(sibling)

def get_key(tree, val): # Function to find the parent key


for key, value in tree.items(): # Search for the key and value in the
graph
if val in value: # If found, return the key
return key

def short(trees, start, end):


shortest = [end] # List to store the shortest path
temp = end
while start != temp: # Backtrack to find the path
temp = get_key(trees, temp) # Get the parent node
shortest.append(temp) # Append the parent node to the path

September 25, 2024


Page 7 of 7

return shortest

# Run DFS search


Visited = search(graph, 'A', 'H')
print(f"\nVisited : {Visited}")

# Get the shortest path


shortest = short(graph, 'A', 'H')
print(f"\nShortest : {shortest[::-1]}")

Output:

Conclusions:
In this lab, we explored fundamental Python programming concepts such as user input,
conditional statements, loops, and functions. These tasks provided hands-on experience in
writing Python code, handling conditions, and utilizing loops efficiently. Through these
exercises, we gained a deeper understanding of Python's control structures and built essential
problem-solving skills.

September 25, 2024

You might also like