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

Lab Manual 1

The lab manual outlines the objectives and tasks for the CL461 Artificial Intelligence Lab at National University of Computer and Emerging Sciences, focusing on Python programming concepts such as data types, operators, conditionals, loops, and functions. Students will use Google Colab for coding exercises and complete specific tasks, including string manipulation and calculating square roots. Submission instructions emphasize the importance of renaming and submitting the Jupyter notebook correctly.

Uploaded by

ridox82537
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab Manual 1

The lab manual outlines the objectives and tasks for the CL461 Artificial Intelligence Lab at National University of Computer and Emerging Sciences, focusing on Python programming concepts such as data types, operators, conditionals, loops, and functions. Students will use Google Colab for coding exercises and complete specific tasks, including string manipulation and calculating square roots. Submission instructions emphasize the importance of renaming and submitting the Jupyter notebook correctly.

Uploaded by

ridox82537
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

National University of Computer and Emerging Sciences

Lab Manual 01
CL461-Art ficial Intell gence Lab

Table of Contents

Department of Computer Science FAST-NU,


Lahore, Pakistan
• Objectives_______________________________________________________________3
• Task Distribution 3
• Online Python Interpreter________________________________________________4

• Object ves
After performing this lab, students shall be able to understand:
• Python data types.
• Python operators (math, comparison, boolean)
• Python condition and loops
• Python functions

• Task Distribut on
Total Time 170 M nutes
Python data types 15 Minutes

Python operators 10 Minutes

Python if-else conditions 10 Minutes

Python loops 15 Minutes

Python functions 20 Minutes

Exercise 90 Minutes

Online Submission 10 Minutes

• Online Python Interpreter


We will be working on Google Collab to run Python programs.

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.

Follow the instructions given below:


• Visit the following link: https://colab.research.google.com/
• Sign in using your nu email id.
• You will be directed to ‘Welcome to Colaboratory’ page.
• Go to Fi e->New Notebook.
• A new notebook is created by the name ‘Untitled0.ipynb’.
• Rename the notebook to your roll number.
• Write your first Python program by typing the following
statement in the first cell o print(‘Hello World’)
• Execute this cell by clicking the play button on the left of the
cell or by pressing Ctrl+Enter.
• You will notice that the notebook will connect to a
runtime. RAM and Disk resources are allocated. (Refer to
the screenshot below)

For offline usage, PyCharm IDE is recommended. Installation details for


PyCharm will be shared later.

• 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.

• Bu lt-in Types
The following chart summarizes the standard data types that are built into
the Python interpreter.

Sr# Categories Data Type Examples

1 Numeric Types int -2, -1, 0, 1, 2, 3, 4, 5, int(20)

2 float -1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25,


float(20.5)
3 complex 1j, complex(1j)

4 Text Sequence str 'a', 'Hello!', str("Hello World")


Type

5 Boolean Type bool True, False, bool(5)


6 Sequence Types list ["apple", "banana", "cherry"],
list(("apple", "banana", "cherry"))

7 tuple ("apple", "banana", "cherry"),


tuple(("apple", "banana", "cherry"))

8 range range(6)

9 Mapping Type dict {"name" : "John", "age" : 36},


dict(name="John", age=36)

10 Set Types set {"apple", "banana", "cherry"},


set(("apple", "banana", "cherry"))

11 frozenset frozenset({"apple", "banana",


"cherry"})
12 Binary bytes b"Hello", bytes(5)
Sequence
13 Types bytearray bytearray(5)

14 memoryvi memoryview(bytes(5))
ew

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.

• Typecast ng
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.

Notice thetype() 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.

• Operators
This section contains the details of different Python operators i.e. Math
operators, comparison operators and Boolean operators.
• 1 Math Operators
From Highest to Lowest precedence:

Operators Operat on Example


** Exponent 2 ** 3 = 8

% Modulus/Remainder 22 % 8 = 6

Operators Operat on Example

// Integer division 22 // 8 = 2

/ Division 22 / 8 = 2.75

* Multiplication 3*3=9

- Subtraction 5-2=3

+ Addition 2+2=4

• Comparison Operators
Operator Meaning

== Equal to

!= Not equal to

< Less than

> Greater Than

<= Less than or Equal to

>= Greater than or Equal to

• Boolean Operators
There are three Boolean
operators:and,or, andnot. Theand
Operator’sTruth Table:
Expression Evaluates to

True and True True

True and False False

False and True False

False and False False


Theor Operator’sTruth Table:

Expression Evaluates to

True or True True

True or False True

False or True True

False or False False

Thenot Operator’sTruth Table:

Expression Evaluates to

not True False

not False True

• If-else Condit ons


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 indentat on instead of
curly-brackets to define the scope in the code.
• if Statement Example:
• if-else Statement Example:

• if-elif-else Statement Example:


Python conditional statements only requireif statement to execute.
B o t h elif andelse are optional and used as per requirement.

• 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.
• Whi e Loop Example with break Statement
With thewhile loop we can execute a set of statements as long as a
condition is true or the loop execution reaches abreak statement.
input() in the above example is a built-in Python function which is
discussed in the functions section below.
• Whi e Loop Example with cont nue Statement
When the program reaches acontinue statement, the program
execution immediately jumps back to the start of the loop.

• for Loop Example with range()

• for Loop Example with range() arguments


Therange() function can also be called with three arguments. The first
two arguments will be the start and stop values, and the third will be
the step argument. The step is the amount that the variable is
increased by after each iteration.

• Funct ons
This section contains the details of Python user defined or custom
function along with a few examples of Python built-in functions.
• Custom Functions
Programmers can define their own functions in Python. Functions can
contain all types of Python statements like variables, conditions and
loops etc.

• S mple Function Example


A function in Python starts withdef keyword followed by the function
name with round brackets. Function parameters can be passed
depending on the requirement.

• Function Example with Return Statement


A return statement consists of the following:
• The return keyword.
• The value or expression that the function should return.

• Bu lt-in Funct ons


The Python interpreter has a number of functions built into it that are
always available.

We have already covered a few built-in functions in the datatypes


section above. Refer to this link for the complete list of Python built-in
functions.

• Bui t in Funct on Examples


Execute the code given below in your Jupyter notebook to find the results
of built-in functions.
• Exercise (25 Marks)
Complete all tasks given below

• Reverse String (5 Marks)


Write a function that reverses a string. The input string is given as an
array of characters. You can use Python list to create the array of
characters. Do not allocate extra space for another array, you must do
this reversal by modifying the input array in- place with O(1) extra
memory.

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"]
• Valid Pal ndrome (10 Marks)
Given a strings, determine if it is a palindrome, considering only
alphanumeric characters and ignoring cases. Refer to this link to learn
about Python string functions which might come handy in this task.

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.

• Sqrt(x) without any bu t in methods (10 Marks)


Given a non-negative integer x, compute and return the square root of x.
Since the return type is an integer, the decimal digits are truncated,
and only the integer part of the result is returned.
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.

• Submission Instruct ons


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 z p or rar your submission file
• Submit this file on Google Classroom under the relevant assignment.
• Late submissions will not be accepted.

You might also like