Lab Manual_ Week 1 - Programming for Artificial Intelligence
Lab Manual_ Week 1 - Programming for Artificial Intelligence
CONTENTS
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!")
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
3
print("Negative number")
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
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
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:
# 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.