Python Programming II
Python Programming II
com/introduction-to-python-class-9-notes-2/
What is an Algorithm?
To write a logical step-by-step method to solve the identified problem is called algorithm, in other words, an algorithm is a procedure for solving
problems.
What is a 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. The graphics below represent different parts of a flowchart.
1 of 19 5/11/2025, 8:43 PM
Introduction To Python Class 9 Notes | CBSE Skill Education [Link]
Example 1: Print 1 to 20
What is program?
A computer program is a collection of instructions that perform a specific task when executed by a computer. This computer program usually written
in a programming language. For example, C++, Java, Python, and Ruby are used to construct programs.
2 of 19 5/11/2025, 8:43 PM
Introduction To Python Class 9 Notes | CBSE Skill Education [Link]
Applications of Python
Python is a high-level, general-purpose programming language. It is different from other languages such as C and Java that are designed to be
compiled to machine code. Python is easy to learn and can be used to write virtually anything that can be described in code.
Installation of Python
Python is a cross-platform programming language, which means it runs on a variety of platforms including Windows, MacOS or Linux operating
system.
To write and run Python program, we need to have Python interpreter installed in our computer.
3 of 19 5/11/2025, 8:43 PM
Introduction To Python Class 9 Notes | CBSE Skill Education [Link]
After installing Python, you’ll need an IDE to write Python programmes. IDLE is a Python editor with a graphical user interface. IDLE stands for
Integrated Development Environment. This IDLE is also known as the Python shell, and it has two modes of operation: interactive mode and script
mode. Interactive Mode allows us to communicate with the operating system, whereas Script Mode allows us to generate and edit Python source
files.
Interactive Mode
Python IDLE Shell provides a Python prompt, You can write single line python commands and execute them easily.
Script Mode
In Python, the Script Mode allows you to add numerous lines of code. In script mode, we type a Python programme into a file and then use the
interpreter to run the code. Working in interactive mode is useful for beginners and for testing little parts of code because it allows us to test them
right away.
4 of 19 5/11/2025, 8:43 PM
Introduction To Python Class 9 Notes | CBSE Skill Education [Link]
Python Statement
A statement is a piece of code that can be executed by a Python interpreter. So, in simple words, anything written in Python is a statement. In The
Python programming language, there are various types of statements, such as assignment statements, conditional statements, looping statements
and so on. These assist the user in obtaining the desired result.
Multiline Statement
The token NEWLINE character is used at the end of a Python statement. However, Statements in Python can be extended to one or more lines
using parentheses (), braces {}, square brackets [], semi-colon (;), continuation character slash (). When we need to do long calculations and cannot
fit these statements into one line, we can make use of these characters.
s=1+2+3+\
Using Continuation Character (/) 4+5+6+\
7+8+9
Using Parentheses () n = (1 * 2 * 3 + 4 – 5)
footballer = [‘MESSI’,
Using Square Brackets [] ‘NEYMAR’,
‘SUAREZ’]
x = {1 + 2 + 3 + 4 + 5 + 6 +
Using braces {}
7 + 8 + 9}
Python Comments
In Python, comments are lines of code that are skipped by the interpreter while the programme is being run. Comments improve the readability of
the code and assist programmers in completely comprehending it. In Python there are two types of comment.
5 of 19 5/11/2025, 8:43 PM
Introduction To Python Class 9 Notes | CBSE Skill Education [Link]
Keywords – Keywords are reserved words in Python . Keywords are predefined words with specific meanings. The keyword can’t be used as a
variable name, function name, or identifier. Except for True and False, all keywords in Python are written in lower case.
Example of Keywords –
Identifiers – An identifier is a name given to a variable, function, class, module, or other object. The identification is made up of a series of digits and
underscores. The identification should begin with a letter or an Underscore and then be followed by a digit. A-Z or a-z, an UnderScore (_), and a
numeral are the characters (0-9). Special characters (#, @, $, percent,!) should not be used in identifiers.
Example of Identifier
1. Variables
A variable is a memory location where you store a value in a programming language. In Python, a variable is formed when a value is assigned to it.
Declaring a variable in Python does not require any further commands.
There are a certain rules and regulations we have to follow while writing a variable
1. A number cannot be used as the first character in the variable name. Only a character or an underscore can be used as the first character.
2. Python variables are case sensitive.
6 of 19 5/11/2025, 8:43 PM
Introduction To Python Class 9 Notes | CBSE Skill Education [Link]
Examples on Variables
Website = “[Link]”
Assigning a value to a variable [Link]
print (Website)
Website = “[Link]”
print (Website) [Link]
Changing value of a variable
Website = “[Link]” [Link]
print (Website)
a, b, c = 5, 3.2, “Hello”
5
print(a)
Assigning different values to different variables 3.2
print(b)
Hello
print(c)
x = y = z = “Same”
Same
print(x)
Assigning same value to different variable Same
print(y)
Same
print(z)
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.
Create a name that makes sense. Suppose, vowel makes more sense than v.
Use camelCase notation to declare a variable. It starts with lowercase letter. For example: myName
Use capital letters where possible to declare a constant. For example: PI
Never use special symbols like !, @, #, $, %, etc.
Constant and variable names should have combination of letters in lowercase or uppercase or digits or an underscore (_).
Create a [Link]
NAME = "Ajay"
AGE = 24
Create a [Link]
import info
print([Link])
print([Link])
Ajay
24
2. Datatype
In Python, each value has a datatype. Data types are basically classes, and variables are instances (objects) of these classes, because everything
in Python programming is an object.
7 of 19 5/11/2025, 8:43 PM
Introduction To Python Class 9 Notes | CBSE Skill Education [Link]
a. Number Datatype
Numerical Values are stored in the Number data type. There are four categories of number datatype –
1. Int – Int datatype is used to store the whole number values. Example: x=500
2. Float – Float datatype is used to store decimal number values. Example: x=50.5
3. Complex – Complex numbers are used to store imaginary values. Imaginary values are denoted with ‘j’ at the end of the number. Example:
x=10 + 4j
4. Boolean – Boolean is used to check whether the condition is True or False. Example: x = 15 > 6 type(x)
b. Sequence Datatype
A sequence is a collection of elements that are ordered and indexed by positive integers. It’s made up of both mutable and immutable data types. In
Python, there are three types of sequence data types:
1. String – Unicode character values are represented by strings in Python. Because Python does not have a character data type, a single
character is also treated as a string. Single quotes (‘ ‘) or double quotes (” “) are used to enclose strings. These single quotes and double
quotes merely inform the computer that the beginning of the string and end of the string. They can contain any character or symbol,
including space. Example: name =” Rakesh Kumar”
2. List – A list is a sequence of any form of value. The term “element” or “item” refers to a group of values. These elements are indexed in the
same way as an array is. List is enclosed in square brackets. Example: dob = [19,”January”,1995]
3. Tuples – A tuple is an immutable or unchanging collection. It is arranged in a logical manner, and the values can be accessed by utilizing the
index values. A tuple can also have duplicate values. Tuples are enclosed in (). Example: newtuple = (15,20,20,40,60,70)
c. Sets Datatype
A set is a collection of unordered data and does not have any indexes. In Python, we use curly brackets to declare a set. Set does not have any
duplicate values. To declare a set-in python we use the curly brackets.
Example:
>>> a = {1,2,2,3,3,3}
>>> a
{1,2,3}
d. Mapping
Dictionaries
In Python, Dictionaries are used generally when we have a huge amount of data. A dictionary is just like any other collection array. A dictionary is a
list of strings or numbers that are not in any particular sequence and can be changed. The keys are used to access objects in a dictionary. Curly
brackets are used to declare a dictionary. Example:
>>> d = {1:'Ajay','key':2}
>>> type(d)
<class 'dict'>
Operators
Operators are symbolic representations of computation. They are used with operands, which can be either values or variables. On different data
types, the same operators can act differently. When operators are used on operands, they generate an expression.
Arithmetic operators
8 of 19 5/11/2025, 8:43 PM
Introduction To Python Class 9 Notes | CBSE Skill Education [Link]
Assignment operators
Comparison operators
Logical operators
Arithmetic Operators
Mathematical operations such as addition, subtraction, multiplication, and division are performed using arithmetic operators.
+ Addition 20 + 20 40
– Subtraction 30 – 10 20
/ Division 30 / 10 20
// Integer division 25 // 10 2
% Reminder 25 % 10 5
** Raised to power 3 ** 2 9
Assignment Operator
= x = 10 x = 10
+= x += 10 x = x + 10
-= x -= 10 x = x = 10
*= x *= 10 x = x * 10
/= x /= 10 x = x / 10
Comparison Operator
The values are compared using comparison operators or relational operators. Depending on the criteria, it returns True or False.
== Equal To 20 == 20 True
Logical Operator
Logical operators are used to combine the two or more then two conditional statements –
Type Conversion
9 of 19 5/11/2025, 8:43 PM
Introduction To Python Class 9 Notes | CBSE Skill Education [Link]
Type conversion is the process of converting the value of one data type (integer, text, float, etc.) to another data type. There are two types of type
conversion in Python.
Python automatically changes one data type to another via implicit type conversion. There is no need for users to participate in this process.
Example:
In the above example, x is containing integer value, y is containing float value and in the variable z will automatically contain float value after
execution.
Users transform the data type of an object to the required data type using Explicit Type Conversion. To do explicit type conversion, we employ
predefined functions such as int(), float(), str(), and so on. Because the user casts (changes) the data type of the objects, this form of conversion is
also known as typecasting.
Output – To output data to the standard output device, we use the print() method.
a = 20
b = 10 30
print(a + b)
print(15 + 35) 50
a = “tarun”
My name is : tarun
print(“My name is :”,a)
x = 1.3 x=
print(“x = /n”, x) 1.3
m=6
I have 6 apples
print(” I have %d apples”,m)
Input – In python, input() function is used to take input from the users.
Syntax Meaning
There are three control flow statements in Python – if, for and while.
If Statement
The if statement is used to check a condition: if the condition is true, we run a block of statements (called the if-block).
10 of 19 5/11/2025, 8:43 PM
Introduction To Python Class 9 Notes | CBSE Skill Education [Link]
Note –
In Python, the body of the if statement is indicated by the indentation. Body starts with an indentation and the first unindented line marks the
end.
Python interprets non-zero values as True. None and 0 are interpreted as False.
Syntax:
if test expression:
statement(s)
if…else Statement
The if..else statement evaluates test expression and will execute body of if only when test condition is True. If the condition is False, body of else is
executed. Indentation is used to separate the blocks.
Example:
if…elif…else Statement
The elif is short for else if. It allows us to check for multiple expressions. If the condition for if False is, it checks the condition of the next elif block
and so on. If all the conditions are False, body of else is executed.
Example:
We can have an if…elif…else statement inside another if…elif…else statement. This is called nesting in computer programming. Any number of
these statements can be nested inside one another. Indentation is the only
way to figure out the level of nesting.
Example:
A for loop is a control flow statement in Python that allows you to execute a block of code repeatedly based on the condition given.
Syntax:
11 of 19 5/11/2025, 8:43 PM
Introduction To Python Class 9 Notes | CBSE Skill Education [Link]
val is the variable that takes the value of the item inside the sequence on each iteration.
Example:
sum = 0
# iterate over the list
The while statement allows you to repeatedly execute a block of statements as long as a condition is true. A while statement is an example of what
is called a looping statement. A while statement can have an optional else clause.
Syntax
while test_expression:
Body of while
Example:
n = 10
# initialize sum and counter
sum = 0
i=1
while i <= n:
sum = sum + i
i = i+1 # update counter
Introduction to Lists
List is a sequence of values of any type. Values in the list are called elements / items. List is enclosed in square brackets.
Example:
a = [1,2.3,"Hello"]
In Python programming, a list is created by placing all the items (elements) inside a square bracket [ ], separated by commas. It can have any
number of items and they may be of different types (integer, float, string etc.).
Example:
12 of 19 5/11/2025, 8:43 PM
Introduction To Python Class 9 Notes | CBSE Skill Education [Link]
#empty list
empty_list = []
#list of integers
age = [15,12,18]
Note: A list can also have another list as an item. This is called nested lists.
# nested list
student marks = ["Aditya", "10-A", [ "english",75]]
A list is made up of various elements which need to be individually accessed on the basis of the application it is used for. There are two ways to
access an individual element of a list:
List Index
Negative Indexing
List Index
A list index is the position at which any element is present in the list. Index in the list starts from 0, so if a list has 5 elements the index will start from
0 and go on till 4.
Negative Indexing
Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item and so on.
In Python, the append() method is used to add a single element to the end of a list.
List = []
print("Initial blank List: ")
print(List)
# Addition of Elements
# in the List
[Link](1)
[Link](2)
[Link](4)
13 of 19 5/11/2025, 8:43 PM
Introduction To Python Class 9 Notes | CBSE Skill Education [Link]
In Python, the insert() method is used to add a single element at the specified position.
# Creating a List
List = [1,2,3,4]
print("Initial List: ")
print(List)
# Addition of Element at
# specific Position
# (using Insert Method)
[Link](3, 12)
[Link](0, 'Kabir')
print("\nList after Insert Operation: ")
print(List)
Output:
Initial List:
[1, 2, 3, 4]
In Python, the extend() method is used to add multiple elements at the same time at the end of the list.
Output:
Initial List:
[1, 2, 3, 4]
In Python, the remove() method is used to remove the first occurrence of searched element from the list.
# Creating a List
List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12]
print("Intial List: ")
print(List)
Output:
Intial List:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
14 of 19 5/11/2025, 8:43 PM
Introduction To Python Class 9 Notes | CBSE Skill Education [Link]
Pop() function can also be used to remove and return an element from the set, but by default it removes only the last element of the set, to remove
an element from a specific position of the List, index of the element is passed as an argument to the pop() method.
# Removing element at a
# specific location from the
# Set using the pop() method
[Link](2)
print("\nContent after pop ")
print(List)
Output:
Slicing of a List
In python, there are multiple ways to print the whole List with all the elements, but to print a specific range of elements from the list, we use Slice
operation. Slice operation is performed on Lists with the use of colon(:).
Slicing
# Creating a List
List= ['G','O','O','D','M','O', 'R','N','I','N','G']
print("Initial List: ")
print(List)
Output:
Initial List:
['G','O','O','D','M','O','R','N','I','N','G']
# Creating a List
List= ['G','O','O','D','M','O', 'R','N','I','N','G']
print("Initial List: ")
print(List)
15 of 19 5/11/2025, 8:43 PM
# to a pre defined point using Slice
Sliced_List = List[:-6]
print("\nElements sliced till 6th element from last: ")
print(Sliced_List)
Output:
Initial List:
['G','O','O','D','M','O','R','N','I','N','G']
Disclaimer: We have taken an effort to provide you with the accurate handout of “AI Reflection Project Cycle and Ethics Class 9 Notes“. If you
feel that there is any error or mistake, please contact me at anuraganand2017@[Link]. The above CBSE study material present on our websites
is for education purpose, not our copyrights. All the above content and Screenshot are taken from Artificial Intelligence Class 9 CBSE
Textbook and Support Material which is present in CBSEACADEMIC website, This Textbook and Support Material are legally copyright
by Central Board of Secondary Education. We are only providing a medium and helping the students to improve the performances in the
examination.
For more information, refer to the official CBSE textbooks available at [Link]