Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
A programming language is a language which specifies a set of instructions that can be used to produce various kinds of output. In simple Words, a programming language is a set of grammatical rules for instructing a computer to perform specific tasks. There are many different programming languages such as BASIC, Pascal, C, C++, Java, Haskell, Ruby, Python, etc.
What is a program?
A computer program is a collection of instructions that perform a specific task when executed by a computer.
What is Python?
Python is a programming language created by Guido Van Rossum when he was working at Centrum Wiskunde & Informatica. The language was released in 1991. Python has become one of the most popular programming languages worldwide. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
Why Python for AI?
Artificial intelligence is the trending technology of the future. 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:
1. Easy to learn, read and maintain: Python has few keywords, simple structure, and a clearly defined syntax.
2. A Broad Standard library: Python has a huge bunch of libraries with plenty of built in functions to solve variety of problems.
3. Interactive Mode: Python has support for an interactive mode which allows interactive testing and debugging of snippets of code.
4. Portability and Compatibility: Python can run on a wide variety of operating systems and hardware platforms, has the same interface on all platforms.
5. Extendable: We can add low-level modules to the Python interpreter. These modules enable programmers to customize their tools to be more efficient.
6. Databases And Scalable: Python provides interfaces to all major open source and commercial databases along with a better structure and support for large programs than shell scripting.
Applications of Python
Python is used for a large number of applications. Few of them are given below:
- Web and Internet Development
- Desktop GUI Applications
- Business Applications
- Software Development
- Games and 3D Graphics
- Database Access
Getting started with Python
Python is a cross-platform programming language, meaning, it runs on multiple platforms like Windows, MacOS, Linux. To write and run Python program, we need to have Python interpreter installed in our computer.
Downloading and Setting up Python for use
- Download Python from python.org.
- Select appropriate download link as per Operating System.
- for example: Select the following link for Windows 64 Bit OS
Download: Windows x86-64 executable installer
When we install Python, an IDE named IDLE is also installed. IDLE is the standard, most popular Python development environment. IDLE is an acronym of Integrated Development and Learning Environment. It lets one edit, run, browse and debug Python Programs from a single interface.
Modes in Python to execute code
There are two modes/ways in python to execute code:
1. Interactive Mode: In this mode, Python commands are executed one at a time and the results will be displayed as soon as we press enter key from keyboard.
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.
The first expression 3+10 written on the Python prompt shows 13 as output in the next line.
The second expression 2+7 * 6 + 7 written on the Python prompt shows 51 as output in the next line.
The third statement print(“Hello World”) written on the Python prompt shows Hello World as output in the next line.
2. Script Mode
In script mode, we type Python program in a file and save it and the run the code by pressing F5 from the keyboard or clicking on menu Run > Run Module
Note: Result produced by Interpreter in both the modes, viz., Interactive and script mode is exactly the same.
Interactive Mode | Script Mode |
Code runs directly in Python shell | Code runs from a saved file |
Executes one line at a time immediately. | Executes the whole program at once. |
Good for testing small code | Good for whole program |
Python Statement
Instructions written in the source code for execution are called statements. There are different types of statements in the Python programming language like Assignment statement, Conditional statement, Looping statements etc. For example, n = 50 is an assignment statement, if n==7 is a conditional statement.
Multi-line statement
Statements in Python can be extended to one or more lines using parentheses (), braces {}, square brackets [], semi-colon (;), continuation character slash ().
Type of Multi-line Statement | Usage |
Using Continuation Character (/) | >>> s = 11 + 12 + 13 + \ 14 + 15 + 16 + \ 17 + 18 + 19 >>>s >>>135 |
Using Parentheses () | >>> n = (1 * 2 * 3 + 4 – 5) >>> n >>> 5 |
Using Square Brackets [ ] | >>> Fruit = [“Mango”, “Guava”, “Banana”] >>> Fruit >>> [‘Mango’, ‘Guava’, ‘Banana’] |
Using braces { } | >>> x = {11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19} >>> x >>> {135} |
Python Comments
A comment is text that doesn’t affect the outcome of a code, it is just a piece of text to let someone know what you have done in a program. In other words we can say that non executable line is called comment. In Python, we use the hash (#) symbol to start writing a comment.
There are two main types of comments in Python:
1. Single Line Comments: These comments are used for one line. It is indicated by the #
symbol. for example
# This is a single-line comment
print("Hello - World") # This prints a message to the console
2. Multi Line Comments: These comments are used for more than one line. Triple quotes ('''
or """
) are used to create comments that span multiple lines.
'''
This is a multi-line comment.
It can span multiple lines.
'''
print("Good Morning")
"""
This is also a multi-line comment.
It uses double quotes instead of single quotes.
"""
print("Good afternoon")
Python Keywords and Identifiers
Keywords: Keywords are the reserved words in Python used by Python interpreter to recognize the structure of the program. The list of all the keywords is given below
False | class | finally | lambda | yield |
None | continue | for | nonlocal | try |
True | def | from | raise | with |
and | del | global | pass | while |
as | elif | if | or | return |
assert | else | import | not | |
break | except | in | is |
Identifier: An identifier is a name given to entities like class, functions, variables, etc. An Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_).
Naming conventions of identifier in Python
- Identifier name can not start with number
- Identifier name can not have space.
- Identifier name can not contain special characters except underscore( _ )
- Keywords can not be used as identifier name.
Examples of invalid variable names
Invalid Variable Names/Identifier Name | Reason |
1number | It can not start with number |
your age | It can not have space |
num@1 | It can not contain special characters. |
Variables and Datatypes
Variable: A variable is a named storage location used to store data. We can consider a variables as a container that holds data which can be changed later. For example
>>>A = 89
>>> B = 7
In above example value 89 is assigned to variable ‘A’ and value 7 is assigned to variable ‘B’
More examples on variables
Task | Sample Code | Output |
Assigning a value to a variable | >>>X = 78 >>>print(X) | 78 |
Changing value of a variable | >>>R = 70 >>>R = 90 >>>print(R) | 90 |
Assigning different values to different variables | >>>a, b, c = 25, 35, 45 >>>print(a) >>>print(b) >>>print(c) | 25 35 45 |
Assigning same value to different variable | >>>a= b= c = “Hello” >>>print(a) >>>print(b) >>>print(c) | Hello Hello Hello |
Constants: A constant is a type of variable whose value cannot be changed. In other words we can say that constants act as containers that hold information which cannot be changed later.
Assigning Value to a constant in Python
In Python, constants are usually declared and assigned on a module. Here, the module means 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.
Example : Declaring and assigning value to a constant
Create a file in python with the code given below and save it by name “constant.py”
MAX_SPEED = 120
MIN_SPEED = 0
DEFAULT_COLOUR = "blue"
Now create another file named “check.py” and import the module created above
import Test.py
print(MAX_SPEED)
print(MAX_SPEED)
print(DEFAULT_COLOUR)
When you execute the above file “check.py” the output will be
120
0
blue
In the above program, we created a constant.py module file. Then, we assign the constant value to MAX_SPEED, MIN_SPEED and DEFAULT_COLOUR. After that, we created a check.py file and import the constant module. Finally, we print the constant value.
NOTE: In reality, we don’t use constants in Python. The global or constants module is used throughout the Python programs.
What are Data Types?
Data type identifies the type of data values a variable can hold and the operations that can be performed on that data. Various data types in Python are as follows:
- Numbers
- Integer
- Boolean
- Floating Point
- Complex
- Integer
- Sequences
- Strings
- Lists
- Tuples
- Mappings
- Dictionary
Number
This data type stores numerical values only. It is further classified into three different types: int, float and complex.
Data type | Description | Examples |
int | This data type stores integers. | 42, -15, 2, -19 |
float | This data type stores floating point numbers. | -2.45, 4.26 |
complex | This data type stores complex numbers. | 1+3j, 22+5j |
Boolean data type (bool) is a sub type of integer. This data type stores two values, True and False. Boolean True value is non-zero. Boolean False is the value zero.
marks1 = 34 marks2 = 23.12 m1 = True print(type(marks1)) print(type(marks2)) print(type(m1)) OUTPUT: <class 'int'> <class 'float'> <class 'bool'> NOTE: type( ) function determine the data type of the variable.
Variables of simple data types like integers, float, boolean, etc., hold single values. But such variables are not useful to hold a long list of information, for example, marks of all students in a class test, names of the months in a year, names of students in a class. For this, Python provides data types like tuples, lists, dictionaries and sets.
Sequence :
It is an ordered collection of items, where each item is indexed by an integer. The three types of sequence data types available in Python are Strings, Lists and Tuples.
1. String : String is a group of characters(like alphabets, digits or special characters including spaces). Strings are enclosed in Single quotes(‘ ‘) or in double quotes(” “). for example
st1 = “Anuj”
st2 = ‘2342’
NOTE: Numerical functions can not be performed on String
2. List : List is a sequence of items enclosed in square brackets [ ] and items are separated by commas. for example
L1 = [23, ‘a’, 4, 2.3, ‘b’] #It is a list containing items of different data types.
L2 = [‘a’, ‘b’, ‘c’, ‘d’]
3. Tuple : Tuple is a sequence of items separated by commas and items are enclosed in parenthesis ( ).
T1 = (1, 3, 56, ‘s’, 5)
Difference between List and Tuple :
List | Tuple |
It is mutable | It is immutable |
Items are enclosed in [ ] | Items are enclosed in ( ) |
Mapping :
Mapping is an unordered data type in Python. Mapping data type in Python is Dictionary.
Dictionary : Dictionary in Python holds data items in key-value pairs. Items in a dictionary are enclosed in curly brackets { }. Every key is separated from its value using a colon (:) sign. The key : value pairs of a dictionary can be accessed using the key. for example:
D1 = {“a” : “Apple”, “b” : “Ball”, “c” : “Cat”} #”a”,”b”, “c” are keys and “Apple”, “Ball”, “Cat” are respective valu
Operators
Operators are special symbols which are used to perform some mathematical or logical operations on values. The values on which the operators work are called operands. for example in 34 + 31, here ‘+’ is a mathematical addition operator and the values 34 and 31 are operands. Python supports various kinds of operators like
1. Arithmetic Operators :
Arithmetic operators are used to perform the following Mathematical operations.
Operator Symbol | Operator Name | Explanation | Examples |
‘+’ | Addition | This operator help to add the two numeric values. This operator can also be used to concatenate two strings on either side of the operator | >>>5 + 9 14 >>>12+8 20 >>>’a’ + ‘b’ ab >>>’C’ + ‘S’ CS |
‘-‘ | Subtraction | This operator help to find the difference between two numbers. | >>>9 – 8 1 >>>130-31 99 |
‘*’ | This operator help us to find the product of two numeric values. | >>>7 * 4 28 >>>2 * 5 60 | |
‘/’ | Division | This operator help us to divide the two numeric values and return the quotient with decimal. | >>>7/2 3.5 >>>8/2 4.0 |
‘%’ | Mod | This operator help us to divide the two numbers and return the remainder. NOTE: If first number is smaller than second, it will return the first number | >>>8%2 0 >>>40%6 4 >>>2%4 2 |
‘//’ | Floor Division | This operator divides the two numbers and return quotient without decimal. It is also called integer division. | >>>6//4 1 >>>9//2 4 |
‘**’ | Exponent | It performs exponential (power) calculation on operands | >>>2**2 4 >>>4**3 64 |
2. Comparison operators or Relational Operators
Comparison operators are used to compare values. It either returns True or False according to the condition.
Operator Symbol | Operator Name | Explanation | Examples |
> | Greater than | This operator returns True if the number on the left side of operator is larger than number on the right side. | >>>9 > 4 True >>>20 > 100 False |
< | Less than | This operator returns Tue if the number on the left side of operator is smaller than number on the right side. | >>>56 < 43 False >>>34 < 50 True |
>= | Greater than equals to | If the value of the left-side operand is greater than or equal to the value of the right-side operand, then condition is True, otherwise it is False | >>>20>=7 True >>>8>=10 False |
<= | Less than equals to | If the value of the left-side operand is less than or equal to the value of the right-side operand, then condition is True, otherwise it is False | >>>27<=27 True >>>15<=65 False |
== | Equal to | This operator returns True , if both the operands are equal. | >>>4==4 True >>>5==6 False |
!= | Not equal to | This operator returns True , if both the operands are not equal. | >>>7!=9 True >>>2!=2 False |
3. logical Operators :
Operator | Explanation | Examples |
and | It returns True if all the conditions are True | >>>x = 7 >>>y = 9 >>>x >5 and y >7 True >>>x<10 and y >10 False |
or | It returns True if any one of the condition is True | >>>x = 7 >>>y = 4 >>>x >5 or y >7 True >>>x >15 or y >7 False |
not | It negates the truth. It makes True to False and False to True. | >>>x = 7 >>>y = 4 >>>not(x >5 or y >7) False |
4. Assignment Operators :
This operator assigns or changes the value of the variable on its left.
Operator | Explanation | Examples |
= | Assigns value of right-side operand to left-side operand. | >>>x = 7 >>>x 7 >>>a = “India” >>>a ‘India’ |
+= | x += y is same as x = x + y It adds the value of right-side operand to the left-side operand and assigns the result to the left-side operand | >>>x = 9 >>>y = 10 >>>x += y >>>x 19 |
-= | x -= y is same as x = x – y It subtracts the value of right-side operand from the left-side operand and assigns the result to the left-side operand | >>>x = 20 >>>y = 10 >>>x -= y >>>x 10 |
*= | x *= y is same as x = x * y It multiply the value of right-side operand and left-side operand and assigns the result to the left-side operand | >>>x = 2 >>>y = 8 >>>x *= y >>>x 16 |
/= | x /= y is same as x = x / y It divides the value of right-side operand by the left-side operand and assigns the result to the left-side operand. | >>>x = 12 >>>y = 6 >>>x /= y >>>x 2.0 |
//= | x //= y is same as x = x // y It performs floor division using two operands and assigns the result to left-side operand. | >>>x = 12 >>>y = 6 >>>x //= y >>>x 2 |
**= | x **= y is same as x = x ** y It performs exponent operation using two operands and assigns the result to left-side operand. | >>>x = 2 >>>y = 5 >>>x **= y >>>x 32 |
%= | x %= y is same as x = x % y It performs modulus operation using two operands and assigns the result to left-side operand. | >>>x = 12 >>>y = 6 >>>x %= y >>>x 0 |
Practice Exercise
Q1. Write the output of the following code
OUTPUT of above code
Python Input and Output
Python Output Using print() function: We use the print() function to output data to the standard output device (screen). An example is given below.
T = "Hello csiplearninghub"
print(T)
The output of the above code will be:
Hello csiplearninghub
Code | Output |
a = 35 b = 7 print(a + b) | 42 |
print(10 + 30) | 40 |
print(“My name is Amit”) | My name is Amit |
name = “Amit” print(“My name is : “,name) | My name is : Amit |
x = 25 print(“x = \n”, x) | x = 25 |
m = 6 print(” I have %d apples”, m) | I have 6 apples |
Python Input Using input() function: In python, input() function is used to take input from the user. for example
name = input("Enter your name") #For String input
num1 = int(input("Enter any number")) #For integer
L = float(input("Enter length of rectangle")) #For float
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.
1. Implicit Type Conversion
2. Explicit Type Conversion
1. 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.
principle_amount = 2000
roi = 4.5
time = 10
simple_interest = (principle_amount * roi * time)/100
print("datatype of principle amount : ", type(principle_amount))
print("datatype of rate of interest : ", type(roi))
print("value of simple interest : ", simple_interest)
print("datatype of simple interest : ", type(simple_interest))
Output of the above program
datatype of principle amount : <class 'int'>
datatype of rate of interest : <class 'float'>
value of simple interest : 900.0
datatype of simple interest : <class 'float'>
Explanation of the above program
1. We calculate the simple interest by using the variable priniciple_amount and roi with time divide by 100
2. We will look at the data type of all the objects respectively.
3. In the output we can see the datatype of principle_amount is an integer, datatype of roi is a float.
4. Also, we can see the simple_interest has float data type because Python always converts smaller data type to larger data type to avoid the loss of data.
Let we take one more example.
a = 7
b =9
c = 8.5
s = a + b + c
print(s)
print(type(a))
print(type(b))
print(type(c))
print(type(s))
Output of the above program
24.5
<class 'int'>
<class 'int'>
<class 'float'>
<class 'float'>
Code | Output | Explanation |
a = 10 b = “Hello” print(a+b) | print(a+b) TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’ | The output shows an error which says that we cannot add integer and string variable types. |
c = ‘Ram’ N = 3 print(c*N) | RamRamRam | The output shows that the string is printed 3 times when we use a multiply operator with a string. |
x = True y = 10 print(x + 10) | 11 | The output shows that the boolean value x will be converted to integer and as it is true will be considered as 1 and then give the output. |
m = False n = 23 print(n – m) | 23 | The output shows that the boolean value m will be converted to integer and as it is false will be considered as 0 and then give the output. |
2. Explicit Type Conversion
In Explicit Type Conversion, users convert the data type of an object to required data type by using the predefined functions like int(), float(), str(), etc .
NOTE: This type of conversion is also called typecasting because the user casts (changes) the data type of the objects.
Syntax: (required_datatype)(expression)
a = "10"
b = "12.5"
c = 90
print("Data type before casting is")
print(type(a))
print(type(b))
print(type(c))
a = int(a)
b = float(b)
c = str(c)
print("Data type after casting is")
print(type(a))
print(type(b))
print(type(c))
Output of the above program
Data type before casting is
<class 'str'>
<class 'str'>
<class 'int'>
Data type after casting is
<class 'int'>
<class 'float'>
<class 'str'>
Code | Output | Explanation |
P = 10 Q = “Computer” print(str(P)+ Q) | 10Computer | Writing str(P) will convert integer “A” into a string and then will add to the string “Q”. |
X = 15.5 P = 15 print(int(X) + P) | 30 | Writing int(X) will convert a float number to integer and then perform the operation. |
B = False A = 15 print(Bool(A)+ B) | 1 | Writing Bool() will convert the integer value to Boolean. If it is zero then it is converted to False, else to True |
NOTE: There is no difference in single or double quoted string. However, if either single or double quote is a part of the string itself, then the string must be placed in double or single quotes respectively.
Practice Questions
Q1. Find the result of (75+85+65)/3 i.e. the average of three marks
Q2. Find the result of “###” * 3
Q3. Find the result of 22/7 * 5 * 5 i.e. the area of circle having radius as 5
Q4. Find the result of “RAVI”+”Kant”
Q5. Take a Boolean value and add a string to it
Q6. Take a string and float number and try adding both
Q7. Take a Boolean and a float number and try adding both.
Q8. To calculate Area of a triangle with Base and Height
Q9. To calculating average marks of 3 subjects
Q10. To calculate Surface Area and Volume of a Cuboid.
Disclaimer : I tried to give you the simple ”Ch 2 Introduction to Python Class 9 Notes” , but if you feel that there is/are mistakes in the Notes of “Ch 2 Introduction to Python Class 9 Notes “ given above, you can directly contact me at csiplearninghub@gmail.com. Reference for the notes is CBSE book uploaded on CBSE Website.
Important Links
Class IX A.I. Book
Class IX AI Curriculum 2025-26
Class X AI Book
Class X AI Curriculum 2025-26
Python Manual
——————————————————————————————————————————
Unit 1 – A.I. Reflection Project Cycle and Ethics Class 9 Notes Important Points
Unit 1 – A.I. Reflection Project Cycle and Ethics Class 9 MCQ
Unit 1 – A.I. Reflection Project Cycle and Ethics Class 9 Question Answers
—————————————————————————————————————————–
Unit 2 – Data Literacy NOTES Important Points
Unit 2 – Data Literacy MCQ
Unit 2 – Data Literacy Question Answers
——————————————————————————————————————————
Unit 3 – Math in AI NOTES Important Points
Unit 3 – Math in AI MCQ
Unit 3 – Math in AI Question Answers
——————————————————————————————————————————
Unit 4 – Generative AI NOTES Important Points
Unit 4 – Generative AI MCQ
Unit 4 – Generative AI Question Answers
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
Ch 2 Introduction to Python Class 9 Notes Important Points
v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v