Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
40 views

AI IX Python Content Term1

Uploaded by

auroraepiclol
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

AI IX Python Content Term1

Uploaded by

auroraepiclol
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

DAYANAND ANGLO VEDIC PUBLIC SCHOOL, AIROLI (2024-25)

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.

Several standard graphics are applied in a flowchart:

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 2: Read number a

Step 3: Read number b

Step 4: if a > b , print a (Compare a


and b using greater than operator)

Step 5: else print b

Step 6: Stop

Output : Largest number among a


and b

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

Getting started with Python


Python is a cross-platform programming language, meaning, it runs on multiple platforms like Windows,
MacOS, Linux and has even been ported to the Java and .NET virtual machines.

Downloading and Setting up Python for use


• Download Python from python.org using link python.org/downloads
• Select appropriate download link as per Operating System [Windows 32 Bit/64 Bit, Apple iOS]

Run in the Integrated Development Environment (IDE)


When we install Python, an IDE named IDLE is also installed. We can use it to run Python on our computer.

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 shell can be used in two ways-


 Interactive mode - as the name suggests, allows us to interact with OS.
 Script mode - lets us create and edit Python source file.

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.

Python Statement and Comments

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.

Rules for Defining Identifiers in Python:


1. Letters and Digits: An identifier must start with a letter (a-z, A-Z) or an underscore (_). The rest of the
identifier can contain letters, digits (0-9), or underscores.
o Example: myVariable, _my_variable, var123
2. Case Sensitivity: Identifiers in Python are case-sensitive, meaning Variable and variable would be
considered different identifiers.
3. No Reserved Keywords: Identifiers cannot be the same as Python's reserved keywords. These are
words that Python uses for its syntax and cannot be used as identifiers.
o Examples of reserved keywords: if, else, while, for, def, class, etc.
4. No Special Characters: Identifiers cannot contain special characters such as @, #, !, $, etc., except for
the underscore _.
5. No Spaces: Identifiers cannot contain spaces. Use underscores (_) or camelCase (e.g.,
myVariableName) to simulate spaces.
6. Avoid Starting with Digits: While an identifier can contain digits, it cannot start with a digit.
o Correct: var1, _123abc
o Incorrect: 1var, 123_abc

Examples of Valid and Invalid Identifiers:

 Valid Identifiers: my_var, Data123, _privateVar, compute_sum


 Invalid Identifiers: 123abc, my-var, @total, class (since class is a reserved keyword)

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

Here are some examples:


# Assigning an integer value
age = 25
# Assigning a floating-point value
height = 5.9
# Assigning a string value
name = "Alice"
# Assigning a boolean value
is_student = True
# Multiple variables assigned in one line
x, y, z = 10, 20, 30

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

Float: When you assign a number with a decimal point.


pi = 3.14

String: When you assign text enclosed in quotes (single or double).


greeting = "Hello, world!"

Boolean: When you assign either True or False.


is_valid = False

Example:
# Defining and assigning values to variables
name = "John"
age = 30
height = 6.1
is_employed = True

# Printing variable values


print(name) # Output: John
print(age) # Output: 30
print(height) # Output: 6.1
print(is_employed) # Output: True
x = 10 # x is an integer
x = "Hello" # x is now a string

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.

Data Types in Python


Python is a dynamically-typed language, which means you don’t need to explicitly declare the data
type of a variable when you create it. Python automatically determines the type of data based on the
value assigned to the variable. Here’s an overview of the built-in data types in Python:

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

Python Output Using print() function


We use the print() function to output data to the standard output device (screen). We can also output data
to a file.

Example Code 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.

Explicit Type Conversion


In Explicit Type Conversion, users convert the data type of an object to required data type. We use the
predefined functions like int(), float(), str(), etc to perform explicit type conversion.
This type of conversion is also called typecasting because the user casts (changes) the data type of the
objects.
Syntax:
(required_datatype)(expression)
Typecasting can be done by assigning the required data type function to the expression.
Example: Adding of string and an integer using explicit conversion

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

# Add two numbers


sum = float(num1) + float(num2)

# Display the sum


print( “The sum of”, num1, “ and” , num2 , “is = “ , sum)

Python Code Sample Output


# Python Program to calculate the square root Enter the number: 8
# To take the input from the user The square root of 8 is = 2.828
num = float(input('Enter a number: '))

num_sqrt = num ** 0.5


print(“The square root of”, num , “is =”,num_sqrt)

Python Code Sample Output


# Python Program to find the area of triangle Enter first side: 5
Enter second side: 6
a = float(input('Enter first side: ')) Enter third side:7
b = float(input('Enter second side: '))
c = float(input('Enter third side: ')) The area of the triangle is = 14.70

# calculate the semi-perimeter


s = (a + b + c) / 2

# calculate the area


area = (s*(s-a)*(s-b)*(s-c)) ** 0.5

print(“The area of the triangle is =”,area)

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

# Taking kilometers input from the user


kilometers = float(input("Enter value in kilometers: "))

# conversion factor
conv_fac = 0.621371

# calculate miles
miles = kilometers * conv_fac

print(kilometer, “kilometers is equal to” ,miles ,


“miles”)

15

You might also like