AI Lab 01
AI Lab 01
1. Introduction to Python:
Python has become one of the most popular programming languages in the world in recent
years. It was created by Guido van Rossum, and released in 1991. It's used in everything from
machine learning to building websites and software testing. It can be used by developers and
non-developers alike. Python, one of the most popular programming languages in the world,
has created everything from Netflix’s recommendation algorithm to the software that controls
self-driving cars. Python is a general-purpose language, which means it’s designed to be used in
a range of application including:
data science, software and web development, automation, and generally getting stuff
done. Python is designed to be highly readable. It uses English keywords frequently. The
alternative are low- level languages, whose instructions are tied to a specific computer architecture
and consist of either numbers or special, non-intuitive four or five letter instructions. Obviously,
we prefer to program in high-level languages. Such languages not only make the programming
process faster, they also make programs easier to debug, and easier to extend and maintain. It
has fewer syntactical constructions than other languages. Python also allows very powerful
programs to be created with much less effort than some other programming languages. So even
though it is a good beginning language, it is also a powerful tool for application development.
1
Step 01: Open your browser, in the address bar, type www.pyhton.org/downloads/
Step # 02: Download the latest available version of python interpreter and then install it.
Step # 03: Now, after successfully installing python interpreter into your computer, you have to
assure it by using the following command:
Step # 04: Open your browser, in the address bar, type https://code.visualstudio.com/download
2
Step # 05: Download the latest available version of Microsoft Visual Studio Code IDE and then
install it.
3
Step # 06: Install the Python Extension from Microsoft Visual Studio Code, to create, save and
execute the python files. Choose the first option from the suggested list, as shown in the figure given
below:
4
4. Python Keywords and Identifiers:
5
To import specific parts
S. No. Keyword Description 15 from
of a module
To declare a global
4 break To break out of a loop 16 global
variable
To make a conditional
5 class To define a class 17 if
statement
To check if a value is
7 def To define a function 19 in present in a list, tuple,
etc.
6
27 raise To raise an exception 31 while To create a while loop
To make a try...except
30 try
statement
7
5.1 Python Statement:
Instructions that a Python interpreter can execute are called statements. For example, a = 1 is an
assignment statement. if statement, for statement, while statement, etc. are other kinds of
statements.
Multi-Line Statement:
In Python, the end of a statement is marked by a newline character. But we can make a
statement extend over multiple lines with the line continuation character (\). For example:
In Python, line continuation is implied inside parentheses (), brackets [ ], and braces { }. For
instance, we can implement the above multi-line statement as:
Here, the surrounding parentheses ( ) do the line continuation implicitly. Same is the case with [
] and { }. For example:
We can also put multiple statements in a single line using semicolons, as follows:
Another way of doing this is to use triple quotes, either ' ' ' or " " ".
6. Python Variables:
A variable is a named location used to store data in the memory. It is helpful to think of variables as
a container that holds data that can be changed later in the program. For example:
9
Here, we have created a variable named number. We have assigned the value 34 to the variable. You
can think of variables as a bag to store books in it and that book can be replaced at any time.
Initially, the value of number was 34. Later, it was changed to 35.9.
1
0
6.1 Assigning values to Variables in Python:
As you can see from the above example, you can use the assignment operator = to assign a value
to a variable.
Output:
In the above program, we assigned a value python.com to the variable website. Then, we printed
out the value assigned to website i.e. python.com.
Output:
If we want to assign the same value to multiple variables at once, we can do this as:
6.2 Constants:
A constant is a type of variable whose value cannot be changed. It is helpful to think of constants
as containers that hold information which cannot be changed later. You can think of constants as
a bag to store some books which cannot be replaced once placed inside the bag.
11
Assigning value to constant in Python:
In Python, constants are usually declared and assigned in a module. Here, the module is a new
file containing variables, functions, etc. which is imported to the main file. Inside the module,
constants are written in all capital letters and underscores separating the words.
Create a firstProgram.py:
Output:
In the above program, we create a constant.py module file. Then, we assign the constant value to
PI, GRAVITY and RADIUS. After that, we create a firstProgram.py file and import the
constant module. Finally, we print the constant value.
Output:
8. Python Operators:
Operators are special symbols in Python that carry out arithmetic or logical computation. The value
that the operator operates on is called the operand.
Example: 2 + 3 = 5.
Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the output of the
operation.
Python divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
13
Logical operators
Identity operators
Membership operators
Bitwise operators
14
8.3 Python Comparison Operators:
Comparison operators are used to compare two values:
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
15
8.7 Python Bitwise Operators:
Bitwise operators are used to compare (binary) numbers:
Operator Name Description
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill left shift Shift left by pushing zeros in from the right and let
the leftmost bits fall off
>> Signed right shift Shift right by pushing copies of the leftmost bit in
from the left, and let the rightmost bits fall off
16
Exercise
17
18