AI IX Python Content Term1
AI IX Python Content Term1
CLASS –IX
SUB: ARTIFICIAL INTELLIGENCE (417)
NOTES WORKSHEET
Unit – 1 Introduction to Python
Algorithm
An algorithm is a step-by-step set of instructions or a procedure used to solve a problem or complete a task.
Think of it as a recipe that guides you through the process of doing something.
Example:
Imagine you want to make a sandwich. The algorithm (or recipe) for making a sandwich might look like this:
1. Get two slices of bread.
2. Spread butter on one side of each slice.
3. Place a slice of cheese on one of the buttered slices.
4. Add a slice of ham on top of the cheese.
5. Put the other slice of bread on top, buttered side down.
6. Cut the sandwich in half.
7. Enjoy your sandwich!
This is an algorithm for making a sandwich. It’s a sequence of clear and simple steps that, when followed,
lead to the desired outcome: a ready-to-eat sandwich.
Similarly, in computing, an algorithm is a sequence of instructions a computer follows to solve a problem or
perform a task, like sorting numbers or searching for information.
Flowchart
A flowchart is the graphical or pictorial representation of an algorithm with the help of different symbols,
shapes and arrows in order to demonstrate a process or a program. While computers work with numbers at
ease, humans need visual representations to understand the information well and communicate it effectively.
Thus, flowcharts are used to break a process into smaller parts and elaborate it using visual representations.
1
Here’s a simple flowchart to represent the algorithm for adding two numbers:
Algorithm Flowchart
1. Start: Begin the process.
2. Input Number 1: Take the first number as
input.
3. Input Number 2: Take the second number
as input.
4. Add the Numbers: Add the two numbers
together and store the result in a variable
called sum.
5. Display the Sum: Show the value of sum.
6. End: End the process.
2
An algorithm to find the greater number between two numbers:
Algorithm Flowchart
Input : Two numbers a and b
Step 1: Start
Step 6: Stop
What is a program?
A computer program is a collection of instructions that perform a specific task when executed by a
computer. It is usually written by a computer program in a programming language.
There are various programming languages like Lisp, Prolog, C++, Java and Python, which can be used for
developing applications of AI. Out of these, Python gains a maximum popularity because of the following
reasons:
3
Applications of Python
IDLE (GUI integrated) is the standard, most popular Python development environment. IDLE is an acronym
of Integrated Development Environment. It lets one edit, run, browse and debug Python Programs from a
single interface. This environment makes it easy to write programs.
Python IDLE Shell account has >>> as Python prompt, where simple mathematical expressions and single
line Python commands can be written and can be executed simply by pressing enter.
Python Script/Program: Python statements written in a particular sequence to solve a problem is known as
Python Script/Program.
In this section we will learn about Python statements, why indentation is important and how to use
comments in programming.
4
Python Statement Instructions written in the source code for execution are called statements.
Keywords
Keywords are the reserved words in Python used by Python interpreter to recognize the structure of the
program.
5
Identifiers
In Python, an identifier is a name used to identify a variable, function, class, module, or other objects in
your code. Identifiers are important because they allow you to reference and manipulate data stored in
memory.
Variable
In Python, a variable is a symbolic name that references or stores data in memory. Variables are used to
hold values that can be referenced and manipulated throughout the program.
Assigning Values to Variables
To assign a value to a variable in Python, you use the assignment operator =.
The syntax is straightforward:
variable_name = value
6
# All variables assigned the same value
a = b = c = 50
Types of Variables
The type of a variable in Python is determined by the value assigned to it:
Integer: When you assign a whole number without a decimal point.
number = 10
Example:
# Defining and assigning values to variables
name = "John"
age = 30
height = 6.1
is_employed = True
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.
Example : Declaring and assigning value to a constant
Create a info.py
NAME = "Ajay"
AGE = 24
Create a main.py
import info
print(info.NAME)
print(info.AGE)
7
When you run the program the output will be,
Ajay
24
Datatypes
What Are Data Types?
Data types are classifications of data that tell a programming language how to interpret and handle
that data. Each data type defines a set of values and the operations that can be performed on them.
Data types help ensure that operations are performed correctly and that data is stored and
manipulated efficiently.
1) Python Numbers
Number data type stores Numerical Values. These are of three different types:
a) Integer & Long
b) Float / floating point
Integer & Long Integer
Range of an integer in Python can be from -2147483648 to 2147483647, and long
integer has unlimited range subject to available memory.
Integers are the whole numbers consisting of + or – sign with decimal digits like
100000, -99, 0, 17. While writing a large integer value, don’t use commas to
separate digits. Also, integersshould not have leading zeros.
Floating Point
Numbers with fractions or decimal point are called floating point numbers.
A floating-point number will consist of sign (+,-) sequence of decimals digits and a dot
such as 0.0, -21.9, 0.98333328, 15.2963. These numbers can also be used to represent
anumber in engineering/ scientific notation.
-2.0 x 105 will be represented as -2.0e52.0X10-5 will be 2.0E-5.
2) None
This is special data type with single value. It is used to signify the absence of value/false in a
situation. It is represented by None.
3) Sequence
A sequence is an ordered collection of items, indexed by positive integers. It is a combination
of mutable and non-mutable data types. Three types of sequence data type available in Python
are:
a) Strings b) List c) Tuples
a) Strings
String is an ordered sequence of letters/characters. They are enclosed in single quotes (‘ ‘) or
double (“ “). The quotes are not part of string. They only tell the computer where the string
constant begins and ends. They can have any character or sign, including space in them.
Note - Remaining Data Types will be covered later
8
Python Operators
Operators are special symbols which represent computation. They are applied on operand(s), which can be
values or variables. Same operators can behave differently on different data types. Operators when applied
on operands form an expression. Operators are categorized as Arithmetic, Relational, Logical and Assignment.
Value and variables when used with operator are known as operands.
Arithmetic Operator
Comparison operators
Comparison operators are used to compare values. It either returns True or False according to the
condition.
9
Logical operators
Assignment operators
Assignment operators are used in Python to assign values to variables.
10
Python Input and Output
User input
In python, input() function is used for the same purpose.
11
Type Conversion
The process of converting the value of one data type (integer, string, float, etc.) to another
data type is called type conversion. Python has two types of type conversion.
Implicit Type Conversion
Explicit Type Conversion
Implicit Type Conversion
In Implicit type conversion, Python automatically converts one data type to another data type. This process
doesn't need any user involvement.
12
SIMPLE PYTHON PROGRAM CODES –
13
Python Code Sample Output
# Program to Add two numbers with user input Enter first number: 1.5
Enter second number: 6.3
num1 = input('Enter first number: ') The sum of 1.5 and 6.3 is
num2 = input('Enter second number: ') 7.8
14
Python Code Sample Output
# Python program to accept distance in KM and Enter value in kilometers: 3.5
convert it 3.50 kilometers is equal to
# into Miles 2.17 miles
# conversion factor
conv_fac = 0.621371
# calculate miles
miles = kilometers * conv_fac
15