Lab Manual 1-AI
Lab Manual 1-AI
Lab Manual 01
CL461-Artificial Intelligence Lab
Table of Contents
1 Objectives 3
2 Task Distribution 3
3 Online Python Interpreter 4
4 Data Types 5
4.1 Built-in Types 5
4.2 Typecasting 6
5 Operators 6
5.1 Math Operators 6
5.2 Comparison Operators 7
5.3 Boolean Operators 7
6 If-else Conditions 8
6.1 if Statement Example: 8
6.2 if-else Statement Example: 8
6.3 if-elif-else Statement Example: 8
7 Loops 9
7.1 While Loop Example with break Statement 9
7.2 While Loop Example with continue Statement 9
7.3 for Loop Example with range() 9
7.4 for Loop Example with range() arguments 9
8 Functions 10
8.1 Custom Functions 10
8.1.1 Simple Function Example 10
8.1.2 Function Example with Return Statement 10
8.2 Built-in Functions 11
8.2.1 Built-in Function Examples 11
9 Exercise (25 Marks) 12
9.1 Reverse String (5 Marks) 12
9.2 Valid Palindrome (10 Marks) 12
9.3 Sqrt(x) without any built-in methods (10 Marks) 12
CL461: Artificial Intelligence Lab
10 Submission Instructions 13
1 Objectives
After performing this lab, students shall be able to understand:
✔ Python data types.
✔ Python operators (math, comparison, boolean)
✔ Python condition and loops
✔ Python functions
2 Task Distribution
Total Time 170 Minutes
Exercise 90 Minutes
Colaboratory, or "Colab" for short, allows you to write and execute Python in your browser
without any configuration. To do that we need to create a Colab notebook.
Colab notebooks allow you to combine executable code and rich text in a single document, along
with images, HTML, LaTeX and more. When you create your own Colab notebooks, they are
stored in your Google Drive account. You can easily share your Colab notebooks with
co-workers or friends, allowing them to comment on your notebooks or even edit them.
Colab notebooks are Jupyter notebooks that are hosted by Colab. The Jupyter Notebook is
an open-source web application that allows you to create and share documents that contain live
code, equations, visualizations and narrative text. Uses include: data cleaning and
transformation, numerical simulation, statistical modeling, data visualization, machine learning,
and much more. Learn more about Jupyter here.
For offline usage, PyCharm IDE is recommended. Installation details for PyCharm will be
shared later.
4 Data Types
The following section describes the standard types that are built into the Python interpreter.
These datatypes are divided into different categories like numeric, sequences, mapping etc.
Typecasting is also discussed below.
8 range range(6)
CL461: Artificial Intelligence Lab
14 memoryview memoryview(bytes(5))
Python has no command for declaring a variable for any datatype. A variable is created the
moment you first assign a value to it. Variable names are case-sensitive. Just like in other
languages, Python allows you to assign values to multiple variables in one line.
4.2 Typecasting
The process of explicitly converting the value of one data type (int, str, float, etc.) to another data
type is called type casting. In Type Casting, loss of data may occur as we enforce the object to a
specific data type.
# cast to float
x=float(2)
y=float(30.0)
z=float("20")
print(x)
print(y)
print(z)
# cast to str
x=str(2)
y=str(30.0)
z=str("20")
print(x)
print(y)
print(z)
Notice the type() function used in the above example. Find out what it does. Execute the given
example in Jupyter notebook to observe the result of type casting.
5 Operators
This section contains the details of different Python operators i.e. Math operators, comparison
operators and Boolean operators.
** Exponent 2 ** 3 = 8
% Modulus/Remainder 22 % 8 = 6
// Integer division 22 // 8 = 2
/ Division 22 / 8 = 2.75
* Multiplication 3 * 3 = 9
- Subtraction 5 - 2 = 3
+ Addition 2 + 2 = 4
== Equal to
!= Not equal to
Expression Evaluates to
Expression Evaluates to
Expression Evaluates to
6 If-else Conditions
Python supports conditional statements i.e. if, elif, else. Comparison operators and
Boolean operators written in the previous section can be used in if-elif-else statements.
Python uses indentation instead of curly-brackets to define the scope in the code.
CL461: Artificial Intelligence Lab
7 Loops
Python has two types of loops i.e. while, for. Use Jupyter notebook to execute all code
snippets given in the examples below to observe their results.
while True:
print('Please type your name.')
name = input()
if name == 'your name':
break
print('Thank you!'
input() in the above example is a built-in Python function which is discussed in the functions
section below.
while True:
print('Who are you?')
name = input()
if name != 'Joe':
continue
print('Hello, Joe. What is the password? (It is a fish.)')
password = input()
if password == 'swordfish':
break
print('Access granted.')
8 Functions
This section contains the details of Python user defined or custom function along with a few
examples of Python built-in functions.
def hello(name):
print('Hello {}'.format(name))
hello('Alice') #Hello Alice
hello('Bob') #Hello Bob
r = random.randint(1, 9)
fortune = getAnswer(r)
print(fortune)
# input function
x = input('Enter your name:')
print('Hello, ' + x)
# max function
CL461: Artificial Intelligence Lab
# print usage
print('Hands-on','python','programming','lab',sep='\n')
# sum function
my_list = [1,3,5,2,4]
print "The sum of my_list is", sum(my_list)
Example 1:
Input: ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
Example 1:
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
Example 1:
Input: x = 4
Output: 2
Example 2:
Input: x = 8
Output: 2
Explanation: The square root of 8 is 2.82842…, and since the decimal part is truncated, 2 is
returned.
10 Submission Instructions
Always read the submission instructions carefully.
● Rename your Jupyter notebook to your roll number and download the notebook as .ipynb
extension.
● To download the required file, go to File->Download .ipynb
● Only submit the .ipynb file. DO NOT zip or rar your submission file
● Submit this file on Google Classroom under the relevant assignment.
● Late submissions will not be accepted