0% found this document useful (0 votes)
2 views

Lab Manual_ Week 1 - Programming for Artificial Intelligence

This document is a lab manual for a Python programming course aimed at introducing students to basic concepts such as syntax, control flow (conditions and loops), and functions. It includes examples of Python code for various topics, including conditional statements, loops, and function definitions, along with practical tasks for students to complete. The tasks are designed to reinforce understanding of fundamental Python concepts and their application in programming.

Uploaded by

hamnafajar23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab Manual_ Week 1 - Programming for Artificial Intelligence

This document is a lab manual for a Python programming course aimed at introducing students to basic concepts such as syntax, control flow (conditions and loops), and functions. It includes examples of Python code for various topics, including conditional statements, loops, and function definitions, along with practical tasks for students to complete. The tasks are designed to reinforce understanding of fundamental Python concepts and their application in programming.

Uploaded by

hamnafajar23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1

CONTENTS

Lab 1: Basic Python, Input, Conditions, loops,functions ......................................................................... 2


Objective: ............................................................................................................................................................ 3
Basic Python: ...................................................................................................................................................... 3
Conditions: .......................................................................................................................................................... 3
Loops ................................................................................................................................................................... 5
Functions ............................................................................................................................................................ 6
Home Task: ......................................................................................................................................................... 8

Lab Manual: Week 1 - Programming for Artificial


Intelligence

2
Objective:
The objective of this lab session is to introduce students to the basics of Python
programming, including fundamental syntax, control flow structures (conditions and
loops), lists, and functions. These foundational concepts are crucial for understanding and
implementing artificial intelligence algorithms and techniques.

1. Basic Python:
Python is a high-level, interpreted programming language known for its simplicity and
readability. In this section, we'll cover basic Python syntax and common operations.

Python Code
# Example 1: Printing "Hello, World!"
print("Hello, World!")

# Example 2: Taking user input


name = input("Enter your name: ")
print("Hello,", name)

2. Conditions and Programs:


Conditional statements allow the execution of different code blocks based on certain
conditions. Common conditional statements include if, elif (short for "else if"), and else.

Python code of IF, Else and ELIF


#using if statement:

# Program to check if a number is positive, negative, or zero


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

if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:

3
print("Negative number")

#Using if and else statements:


# Program to check if a number is even or odd
num = int(input("Enter a number: "))

if num % 2 == 0:
print("Even number")
else:
print("Odd number")

#Using if, elif, and else statements:


# Program to determine the grade of a student based on their score
score = int(input("Enter your score: "))

if score >= 90:


print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
else:
print("Grade: F")

3. Loops:
Loops are used to repeat a block of code multiple times until a condition is met. Python
supports while and for loops.

4
Python code(WhileLoop , For Loop in Range())
#While loop: This loop iterates until a given condition is true.
# While loop example
number = 0
while number < 5:
print(number)
number += 1

#Simple loop: This is a basic loop that iterates over a sequence


of elements.
# Simple loop example
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

#List iteration loop: This is similar to a simple loop but more


compact, iterating directly over elements of a list.
# List iteration loop example
fruits = ["apple", "banana", "cherry"]
[print(fruit) for fruit in fruits]

#Loop using range: This loop iterates over a range of numbers.


# Loop using range example
for i in range(5):
print(i)

5
5. Functions:
Functions are blocks of reusable code that perform a specific task. They help in
modularizing code and promoting reusability.

Python code
Question 1:
Define a function modify_list that takes a list as input and modifies it by appending the
sum of its elements to itself.
Solution:
def modify_list(lst):
lst.append(sum(lst))

# Example usage:
my_list = [1, 2, 3]
modify_list(my_list)
print(my_list) # Output: [1, 2, 3, 6]

Question 2:
Define a function swap_values that takes two variables as input and swaps their values
without using a return statement.
Solution:

def swap_values(a, b):


print(f"Before swapping inside function: a = {a}, b = {b}")
a, b = b, a
print(f"After swapping inside function: a = {a}, b = {b}")
return a, b

# Example usage:
x = 5
y = 10
print(f"Before swapping: x = {x}, y = {y}")
x, y = swap_values(x, y)
print(f"After swapping: x = {x}, y = {y}")

6
Question 3:
Define a function remove_duplicates that takes a list as input and removes duplicates, but
the original order of elements should be preserved.
Solution:
def remove_duplicates(lst):
seen = set()
lst[:] = [x for x in lst if x not in seen and not seen.add(x)]

# Example usage:
my_list = [1, 2, 3, 2, 1, 4, 5, 6, 4]
remove_duplicates(my_list)
print(my_list) # Output: [1, 2, 3, 4, 5, 6]

Question 4:
Define a function find_max that takes a variable number of arguments and returns the
maximum value among them.
Solution:
def find_max(*args):
return max(args)

# Example usage:
print(find_max(5, 10, 3, 7)) # Output: 10

Question 5:
Define a function merge_dicts that takes two dictionaries as input and merges them. If
there are common keys, the values should be concatenated into a list.
Solution:
def merge_dicts(dict1, dict2):
merged = dict1.copy()
for key, value in dict2.items():
if key in merged:

7
merged[key] = [merged[key], value] if not
isinstance(merged[key], list) else merged[key] + [value]
else:
merged[key] = value
return merged

# Example usage:
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 3, 'd': 4, 'e': 5}
print(merge_dicts(dict1, dict2)) # Output: {'a': 1, 'b': [2, 3],
'c': 3, 'd': 4, 'e': 5}

Lab Tasks:
1. Write a Python program to find the maximum of three numbers.
2. Write a program to check whether a year is a leap year or not.
3. Create a program to print the Fibonacci sequence up to a certain number of terms.
4. Write a function to reverse a given list.
5. Write a program to count the number of vowels in a given string.
6. Create a Python program to find the factorial of a number using recursion.
7. Write a function to check if a number is prime or not.
8. Write a program to find the sum of all elements in a list.
9. Create a program to find the largest element in a list.
10. Write a function to remove duplicate elements from a list.
11. These tasks will help students reinforce their understanding of basic Python
concepts and apply them to solve various problems.

You might also like