Lab Manual 1
Lab Manual 1
Lab Manual 01
CL461-Art ficial Intell gence Lab
Table of Contents
• 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
Exercise 90 Minutes
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.
• 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.
8 range range(6)
14 memoryvi memoryview(bytes(5))
ew
• 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:
% 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
• Comparison Operators
Operator Meaning
== Equal to
!= Not equal to
• Boolean Operators
There are three Boolean
operators:and,or, andnot. Theand
Operator’sTruth Table:
Expression Evaluates to
Expression Evaluates to
Expression Evaluates to
• 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.
• 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.
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.
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.