0% found this document useful (0 votes)
72 views11 pages

Class 9th AI ( (Introduction To Python)

This document is a study material for 9th-grade students on Artificial Intelligence, specifically focusing on the Python programming language. It covers Python's features, its applicability in AI, programming modes, variables, data types, operators, input/output functions, conditional statements, looping statements, and lists. The document provides examples and explanations for each topic to facilitate understanding.

Uploaded by

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

Class 9th AI ( (Introduction To Python)

This document is a study material for 9th-grade students on Artificial Intelligence, specifically focusing on the Python programming language. It covers Python's features, its applicability in AI, programming modes, variables, data types, operators, input/output functions, conditional statements, looping statements, and lists. The document provides examples and explanations for each topic to facilitate understanding.

Uploaded by

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

Arti

`
Study Material
Level – 9th Subject: Artificial Intelligence

Introduction to Python
Python is a high-level programming language. It was developed by Guido van Rossum in
February 1991.Python is also called an interpreted language which means that the Python
interpreter reads the source code line by line and converts it into object code (machine
language).
Features of Python
1. Python is a free and open source language as you can download and install it on your
computer without paying any fee.
2. Python is extensible means you can add or modify the syntax of the language.
3. Python is a platform independent language. Python programs can run on any platform
without making any changes to the code.
4. Python is a case sensitive language.

Why Python for AI


1. Python is a simple and easy to learn and code.,
2. Python is compatible with other programming languages which makes it simple to
combine AI with already existing applications.
3. Python includes a massive library for AI.
Applications of AI
1. Python is used to create web and AI based applications.
2. It is widely used for scientific and numeric computing.
3. Python along with its libraries like pygame, provides a platform for game
development.

Programming modes in Python:


There are two modes in Python that you can use for programming.
1. Interactive mode 2. Script mode
Interactive mode:
This is the default mode that appears when you launch Python. In this mode we type Python
commands line by line, and Python immediately shows the output. It is good for quick
testing or learning.
Script mode:
In this mode, we write Python code in a file (with .py extension). Then we save and run the
file. It is best for long programs and projects.
Variables:
Named storage locations that are used to store data are called variables. Variables act as
containers that hold different types of data such as numbers, text, or logical values.
We assign value to a variable using = sign.
Example: a=10
name=”Ayesha”
weight=”30.9”

Data Types in Python


Data types define the type of value that a variable can store.
In Python, every value belongs to a particular data type, such as a number, text, or logical
value.
Following are the basic data types in Python:
Data Type Example What does it Store
Int 10, -5, 0 Whole numbers
Float 3.14, -2.5 Decimal numbers
Str "Hello" Text/characters
Bool True, False Logical values
Operators
An operator is a symbol that is used to perform an operation on values or variables.
The values or variables on which the operator works are called operands.
Types of Operators:
1. Arithmetic Operators 2. Relational Operators
2. Assignment Operator 4. Logical Operator

1. Arithmetic Operators

Arithmetic Operators are used for basic mathematical calculations.

Operator Example Result


+ (Addition) 10 + 5 15
- (Subtraction) 10 - 5 5
* (Multiplication) 10 * 5 50
/ (Division) 10 / 3 3.333...
% (Modulus) 10 % 3 1
Relational Operators

Relational Operators are used to compare two values. They either return True or False
according to the condition.

Operator Example Result


== (Equal to) 5 == 5 True
!= (Not equal to) 5 != 3 True
> (Greater than) 10 > 5 True
< (Less than) 4<2 False
>= (Greater or equal) 5 >= 5 True
<= (Less or equal) 3 <= 4 True

Logical Operators.
Logical Operators are used to compare two or more than two Boolean expressions.
Operator Example Result
and (5 > 2) and (10 > 3) True
or (5 > 2) or (10 < 3) True
not not(5 > 2) False

Assignment Operators.

Assignment operators are used to assign the values to variables. The most commonly used
assignment operator is: =

Example: a=5
Name = ”Sara”
Input & Output in Python

In Python, the input() function is used to take input from user and the print() function is used
to display the output.

print() function:
The print() function is used to print a message or a value of the variable.
Output:

#Program to Calculate the area of rectangle

Output:

Input()function

The input() function is used to take data (input) from the user through the keyboard while
a program is running. Whatever the user types is returned as a string (text).

Syntax:
● “Message shown to user" → Prompt that appears on the screen.
● variable → Stores the input given by the user.

Example 1: Taking String Input

Output:

Example 3: Converting Input into Numbers


To work with numbers, we must convert the input:

Output:

Remember:
1. input() always takes values as string.
2. Convert input into int or float when you need numbers.
3. A prompt/message can be given inside input() to guide the user.
Program to calculate the area of rectangle by taking input from user:

Output:

Conditional Statements in Python

Conditional statements are used to make decisions in a program.


They allow the program to execute certain blocks of code only if a condition is True.
Types of Conditional Statements
1.if
2.if..else
3. if..elif..else

1.if statement:
The if statement executes a block of code only when the condition is True.
Example:

Output:
2. if…else statement
Provides two choices: one block runs if the condition is True, and another runs if it is False.
Example
Program to find greatest of two numbers entered by user

Output

3. if…elif…else statement
● Used when there are multiple conditions to check.
● elif stands for else if.
● Only the first condition that is True gets executed.
Example:
Output:

Looping Statements in Python


Looping statements are used to repeat a block of code multiple times until a certain
condition is met.
Instead of writing the same code again and again, loops help in making programs shorter
and easier.
Types of Loops in Python
1. while loop
2. for loop
1. while loop
● Repeats a block of code as long as the condition is True.
● Condition is checked before each iteration.

Example:
Program to print numbers from 1 to 5

Output:

2. for loop
● Used to iterate over a sequence (like a range, list, string).
● Runs a fixed number of times.
Example:
Using range() to print numbers 1 to 5

Output:

Example: Iterating through a string

Output:

Lists in Python
A list in Python is a collection of items. The items can be of any data type. The items in a
list are separated by comma(,) and enclosed within square brackets[].
Example:
fruits = ["apple", "banana", "mango", "orange"]
numbers = [10, 20, 30, 40, 50]
student = ["Rida", 25,"7th Purple"]
Accessing List Elements
The index is used to access the list items. It starts from 0.
Example:

Output:

Changing List Elements

Output:

Common list operations


1. Length of the list: The len()function returns the number of the elements in the list
Example: fruits=[“apple”,”mango”,”grapes”]
print(len(fruits))
Output: 3
2. Adding elements at the end of the list:
The append() function is used to add elements at the end of the list.
Example: fruits = ["apple", "banana"]
fruits.append("mango")
print(fruits)
Output: ['apple', 'banana', 'mango']

3. Removing elements
remove(value) → removes specific value
Example: fruits = ["apple", "mango",”banana”]
fruits.remove("mango")

You might also like