




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































Introduction This lab is aimed to provide students a complete insight of the implementation of different Artificial Intelligence algorithms. In order to achieve the set goal, lab will be divided into three main modules;
List of Experiments of AI Lab # Description
Statement Purpose: This lab is an introductory session on Python. It is a powerful object-oriented programming language, comparable to Perl, Ruby, Scheme and Java. Some of Python’s notable features are: Easy to use that makes it simple to get your first programs working Easy to learn that makes it an excellent choice for beginners Runs everywhere, including Mac OS X, Windows, Linux and Unix
The lab will teach students to: Basic Operations on strings and numbers Basic use of conditionals Basic use of loops
As a pre-lab activity, read Chapters 3-5 from the book ( Introduction to Programming Using Python - Y. Liang (Pearson, 2013) ) to gain an insight about python programming and its fundamentals. LAB # 01
Display strings on screen.
Use Python as a calculator.
g. 28 / 4 (note: division returns a floating point number) h. 26 / 4 i. 23.4 / 3.
Get an integer answer from division operation. Also get remainder of a division operation in the output.
Calculate 4^3 , 4^10 , 4^29 , 4^150 , 4^1000
#!/usr/bin/env python name = input('What is your name? ') print('Hello ' + name) job = input('What is your job? ') print('Your job is ' + job)
Take input from the keyboard and use it in your program.
In Python and many other programming languages you can get user input. In Python the input() function will ask keyboard input from the user.The input function prompts text if a parameter is given. The function reads input from the keyboard, converts it to a string and removes the newline (Enter).Type and experiment with the script below. Activity 9: Let us take an integer from user as input and check whether the given value is even or not. Solution: A. Create a new Python file from Python Shell and type the following code.
x = "Nancy" print(x)
s = "My lucky number is %d, what is yours?" % 7 print(s)
s = "My lucky number is " + str(7) + ", what is yours?" print(s)
B. Run the code by pressing F5. You will get the following output.
Let us modify the code to take an integer from user as input and check whether the given value is even or odd. 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. Solution: A. Create a new Python file from Python Shell and type the following code. B. Run the code by pressing F5. You will get the following output. Activity 11: Calculate the sum of all the values between 0-10 using while loop. Solution: Create a new Python file from Python Shell and type the following code. Run the code by pressing F5.
Activity 13: Write a Python code to keep accepting integer values from user until 0 is entered. Display sum of the given values. Solution: Create a new Python file from Python Shell and type the following code. Run the code by pressing F5. You will get the following output. Activity 14: Write a Python code to accept an integer value from user and check that whether the given value is prime number or not. Solution: Create a new Python file from Python Shell and type the following code. Run the code by pressing F5.
You will get the following output(s). 3) Stage v (verify)
Activity 1: Write a Python code to accept marks of a student from 1-100 and display the grade according to the following formula. Grade F if marks are less than 50 Grade E if marks are between 50 to 60 Grade D if marks are between 61 to 70 Grade C if marks are between 71 to 80 Grade B if marks are between 81 to 90 Grade A if marks are between 91 to 100 Activity 2: Write a program that takes a number from user and calculate the factorial of that number.
This lab will give you practical implementation of different types of sequences including lists, tuples, sets and dictionaries. We will use lists alongside loops in order to know about indexing individual items of these containers. This lab will also allow students to write their own functions.
This lab teaches you the following topics: How to use lists, tuples, sets and dictionaries How to use loops with lists How to write customized functions
As a pre-lab activity, read Chapters 6, 10 and 14 from the book ( Introduction to Programming Using Python - Y. Liang (Pearson, 2013) ) to gain an insight about python programming and its fundamentals. LAB # 2
1) Stage J (Journey) Introduction: Python provides different types of data structures as sequences. In a sequence, there are more than one values and each value has its own index. The first value will have an index 0 in python, the second value will have index 1 and so on. These indices are used to access a particular value in the sequence. Python offers different types of sequences. Lists are the most important type of sequence being used in Python. It is a collection of same or different type of objects. These objects are separated by commas to distinguish from each other enclosed in square brackets. The following activities show that how lists are used in Python. A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. The data type "set", which is a collection type, has been part of Python since version 2.4. A set contains an unordered collection of unique and immutable objects. The set data type is, as the name implies, a Python implementation of the sets as they are known from mathematics. This explains, why sets unlike lists or tuples can't have multiple occurrences of the same element. A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values. 2) Stage a1 (apply)
Activity 1: Use loops to accept 5 values from user and store them in a list. Display all the values (objects) of the list.
Solution: E. Create a new Python file from Python Shell and type the following code. F. Run the code by pressing F5. You will get the following output. Activity 4: Accept two lists from user and display their join. Solution:
You will get the following output. Activity 5: Write a Python code to accept a list from user and find a required element in it. Solution: You will get the following output. If we run the program again and enter 55 to find in the list then the output will be as below.