Lab Manual 1
Lab Manual 1
Lab Manual 01
CS3151-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 Power function (3 Marks)...............................................................................................12
9.2 Array Manipulation (7 Marks)........................................................................................12
9.3 Counter (7.5 Marks)........................................................................................................12
9.4 Median (7.5 Marks).........................................................................................................13
CS3151: Artificial Intelligence Lab
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
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.
3 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)
"banana", "cherry"))
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.
3.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.
CS3151: Artificial Intelligence Lab
4 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
CS3151: Artificial Intelligence Lab
Expression Evaluates to
Expression Evaluates to
5 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.
else:
print('Hello, stranger.')
6 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.')
CS3151: Artificial Intelligence Lab
7 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
number = [3, 2, 8, 5, 10, 6]
largest_number = max(number);
print("The largest number is:", largest_number)
# 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)
For Example:
myPower(2,3) return 8
0 1 2 3 4
5.8 | 2.6 | 9.1 | 3.4 | 7.0
when the function returns, the array will have been changed so that it looks like this:
0 1 2 3 4
5.8 | 8.4 | 17.5 | 20.9 | 27.9
because 5.8 + 2.6 = 8.4 and 5.8 + 2.6 + 9.1 = 17.5 and so on. Note that the contents of cell 0 are
not changed. The function should return the average of elements of original array.
Sample Run:
Input:
Hello, I am a student of UMT University.
Values returned:
No. of letters are: 34
No. of spaces are: 7
No. of uppercase letters are: 7
No. of lowercase letters: 25
CS3151: Artificial Intelligence Lab
Note:
How to find Median
1. Arrange your numbers in numerical order.
2. Count how many numbers you have.
3. If you have an odd number, divide by 2 and round up to get the position of the me-
dian number.
4. If you have an even number, divide by 2. Go to the number in that position and average
it with the number in the next higher position to get the median.
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 LMS under the relevant assignment.
Late submissions will not be accepted