0% found this document useful (0 votes)
70 views17 pages

Python Programming Notes for Class IX

Uploaded by

dweej2324
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)
70 views17 pages

Python Programming Notes for Class IX

Uploaded by

dweej2324
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

Class IX (2025-26)

PYTHON NOTES

Introduction
Python was created in 1991 with a focus on code readability and its ability to
express concepts in fewer lines of code.

• Python is a high-level, interpreted, and object-oriented programming


language.
• It was created by Guido van Rossum in 1991.
• Python is known for its simplicity, readability, and ease of learning.

Features of Python
• Simple and Easy to Learn – Its syntax is close to English.
• Interpreted Language – Code is executed line by line.
• Platform Independent – Runs on Windows, Mac, and Linux.
• Open Source – Free to use and modify.
• Extensive Libraries – Has many built-in modules and packages.
• Versatile – Used in web development, data science, AI, gaming, etc.

What can we do with Python?


Python is used for:
• Web Development: Frameworks like Django, Flask.
• Data Science and Analysis: Libraries like Pandas, NumPy, Matplotlib.
• Machine Learning and AI: TensorFlow, PyTorch, Scikit-learn.
• Automation and Scripting: Automate repetitive tasks.
• Game Development: Libraries like Pygame.
• Web Scraping: Tools like BeautifulSoup, Scrapy.
• Desktop Applications: GUI frameworks like Tkinter, PyQt.
• Scientific Computing: SciPy, SymPy.
• Internet of Things (IoT): MicroPython, Raspberry Pi.
• DevOps and Cloud: Automation scripts and APIs.
• Cybersecurity: Penetration testing and ethical hacking tools.

Python IDEs (Integrated Development Environments)


• Some commonly used IDEs for Python are:
• IDLE (comes with Python)
• PyCharm
• Jupyter Notebook
• VS Code

1
Python Variables
In Python, variables are used to store data that can be referenced and
manipulated during program execution.

A variable is essentially a name that is assigned to a value. Unlike many other


programming languages, Python variables do not require explicit declaration of
type. The type of the variable is inferred based on the value assigned.

Variables act as placeholders for data. They allow us to store and reuse values in
our program.
# Variable 'x' stores the integer value 10
x=5

# Variable 'name' stores the string "Samantha"


name = "Samantha"

print(x)
print(name)

Output
5
Samantha

Rules for Naming Variables


To use variables effectively, we must follow Python’s naming rules:
• Variable names can only contain letters, digits and underscores (_).
• A variable name cannot start with a digit.
• Variable names are case-sensitive (myVar and myvar are different).
• Avoid using Python keywords (e.g., if, else, for) as variable names.

VALID:
age = 21
_colour = "lilac"
total_score = 90

INVALID:
name = "Error" # Starts with a digit
class = 10 # 'class' is a reserved keyword
user-name = "Doe" # Contains a hyphen

Datatypes
Every value in Python has a datatype. It refers to the type of data that a variable
holds. There are various data types in Python. Some of the important types are
mentioned below:

1) Python Numbers
2
Number data type stores Numerical Values. These are also of 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, integers
should 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 a number in engineering/ scientific notation. -2.0 x 105 will be
represented as -2.0e5

2.0X10-5 will be 2.0E-5

String

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.

Lists

List is also a sequence of values of any type. Values in the list are called elements
/ items. These are indexed / ordered. List is enclosed in square brackets.

Example:

dob = [19,"January",1990]
marks=[23.45.67,12]

Assigning Values to Variables

Basic Assignment
Variables in Python are assigned values using the = operator.

x=5
y = 3.14
z = "Hi"

3
Python variables are dynamically typed, meaning the same variable can hold
different types of values during execution.

x = 10
x = "Now a string"

Multiple Assignments
Python allows multiple variables to be assigned values in a single line.

Assigning the Same Value


Python allows assigning the same value to multiple variables in a single line, which
can be useful for initializing variables with the same value.

a = b = c = 100
print(a, b, c)

Output
100 100 100

Assigning Different Values


We can assign different values to multiple variables simultaneously, making the
code concise and easier to read.

x, y, z = 1, 2.5, "Python"
print(x, y, z)

Output
1 2.5 Python

Input and Output in Python


Understanding input and output operations is fundamental to Python programming.
With the print() function, we can display output in various formats, while the input()
function enables interaction with users by gathering input during program execution.

Printing Output using print() in Python


This function allows us to display text, variables and expressions on the console.
Let's begin with the basic usage of the print() function:

In the example given below, "Hello, World!" is a string literal enclosed within double
quotes. When executed, this statement will output the text to the console.

print("Hello, World!")

Output
Hello, World

4
Printing Variables
We can use the print() function to print single and multiple variables. We can print
multiple variables by separating them with commas. Example:

# Single variable
s = "Bob"
print(s)

# Multiple Variables
s = "Alice"
age = 25
city = "New York"
print(s, age, city)

Output
Bob
Alice 25 New York

How to Change the Type of Input in Python


By default input() function helps in taking user input as string. If any user wants to
take input as int or float, we just need to typecast it.

Accepting strings
The code prompts the user to input a string (the color of a rose), assigns it to the
variable color and then prints the inputted color.
# Taking input as string
color = input("What color is rose?: ")
print(color)

Output
What color is rose?: Red
Red

Accepting integers
The code prompts the user to input an integer representing the number of roses,
converts the input to an integer using typecasting and then prints the integer value.
# Taking input as int
# Typecasting to int
n = int(input("How many roses?: "))
print(n)

Output
How many roses?: 88
88
Accepting floating point / decimal numbers

5
The code prompts the user to input the price of each rose as a floating-point number,
converts the input to a float using typecasting and then prints the price.
# Taking input as float
# Typecasting to float
price = float(input("Price of each rose?: "))
print(price)
Output
Price of each rose?: 50.3050.3
50.3050.3

Find DataType of Input in Python


In the given example, we are printing the type of variable x. We will determine the
type of an object in Python.

a = "Hello World"
b = 10
c = 11.22
d = ["Geeks", "for", "Geeks"]

print(type(a))
print(type(b))
print(type(c))
print(type(d))

Output
<class 'str'>
<class 'int'>
<class 'float'>
<class 'list'>

Practical Examples

1. Swapping Two Variables


Using multiple assignments, we can swap the values of two variables without
needing a temporary variable.

a, b = 5, 10
a, b = b, a
print(a, b)

Output
10 5

2. Counting Characters in a String

6
word = "Python"
length = len(word)
print("Length of the word:", length)

Output
Length of the word: 6

Python Operators
In Python programming, Operators in general are used to perform operations on
values and variables. These are standard symbols used for logical and arithmetic
operations. In this article, we will look into different types of Python operators.
• OPERATORS: These are the special symbols. Eg- + , * , /, etc.
• OPERAND: It is the value on which the operator is applied.

Types of Operators in Python


Arithmetic Operators
Python Arithmetic operators are used to perform basic mathematical operations like
addition, subtraction, multiplication and division.
In Python 3.x the result of division is a floating-point while in Python 2.x division of 2
integers was an integer. To obtain an integer result in Python 3.x floored (// integer) is
used.

# Variables
a = 15
b=4

# Addition
print("Addition:", a + b)

# Subtraction
print("Subtraction:", a - b)

# Multiplication
print("Multiplication:", a * b)

# Division
print("Division:", a / b)

# Floor Division
print("Floor Division:", a // b)

# Modulus
print("Modulus:", a % b)

# Exponentiation
print("Exponentiation:", a ** b)

7
Output
Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3.75
Floor Division: 3
Modulus: 3
Exponentiation: 50625

Comparison Operators
In Python, Comparison (or Relational) operators compares values. It either returns
True or False according to the condition.

a = 13
b = 33

print(a > b)
print(a < b)
print(a == b)
print(a != b)
print(a >= b)
print(a <= b)

Output
False
True
False
True
False
True

Logical Operators
Python Logical operators perform Logical AND, Logical OR and Logical
NOT operations. It is used to combine conditional statements.
The precedence of Logical Operators in Python is as follows:
1. Logical not
2. logical and
3. logical or

a = True
b = False
print(a and b)

8
print(a or b)
print(not a)

Output
False
True
False

Assignment Operators
Python Assignment operators are used to assign values to the variables. This
operator is used to assign the value of the right side of the expression to the left side
operand.
Example

a = 10
b=a
print(b)
b += a
print(b)
b -= a
print(b)
b *= a
print(b)
b <<= a
print(b)

Output
10
20
10
100
102400

Precedence and Associativity of Operators


In Python, Operator precedence and associativity determine the priorities of the
operator.

1. () : Parentheses (highest precedence) -> Associativity: Left to right


2. ** : Exponentiation -> Associativity: Right to left
3. +x, -x, ~x : Unary plus, unary minus -> Associativity: Right to left
4. *, /, //, % : Multiplication, matrix multiplication, division, floor division, remainder
-> Associativity: Left to right
5. +, - : Addition and subtraction -> Associativity: Left to right

9
Operator Precedence
This is used in an expression with more than one operator with different precedence
to determine which operation to perform first.

expr = 10 + 20 * 30
print(expr)
name = "Alex"
age = 0

print(name == "Alex" or name == "John" and age >= 2)


print((name == "Alex" or name == "John") and age >= 2)

Output
610
True
False

Operator Associativity
If an expression contains two or more operators with the same precedence then
Operator Associativity is used to determine. It can either be Left to Right or from
Right to Left.

print(100 / 10 * 10)
print(5 - 2 + 3)
print(5 - (2 + 3))
print(2 ** 3 ** 2)

Output
100.0
6
0
512

Conditional Statements in Python


Conditional statements in Python are used to execute certain blocks of code based
on specific conditions. These statements help control the flow of a program, making
it behave differently in different situations.

If Conditional Statement
If statement is the simplest form of a conditional statement. It executes a block of
code if the given condition is true.

If Statement

age = 20

10
if age >= 18:
print("Eligible to vote.")

Output
Eligible to vote.

Short Hand if
Short-hand if statement allows us to write a single-line if statement.

age = 19
if age > 18: print("Eligible to Vote.")

Output
Eligible to Vote.

This is a compact way to write an if statement. It executes print statement if the


condition is true.

If else Conditional Statement


If Else allows us to specify a block of code that will execute if the condition(s)
associated with an if or elif statement evaluates to False. Else block provides a way
to handle all other cases that don't meet the specified conditions.
If Else Statement

age = 10
if age <= 12:
print("Travel for free.")
else:
print("Pay for ticket.")

Output
Travel for free.

elif Statement
elif statement in Python stands for "else if." It allows us to check multiple conditions,
providing a way to execute different blocks of code based on which condition is true.
Using elif statements makes our code more readable and efficient by eliminating the
need for multiple nested if statements.

age = 25

if age <= 12:


print("Child.")
elif age <= 19:
print("Teenager.")
elif age <= 35:
print("Young adult.")
else:
print("Adult.")

11
Output
Young adult.
The code checks the value of age using if-elif-else. Since age is 25, it skips the first
two conditions (age <= 12 and age <= 19), and the third condition (age <= 35) is
True, so it prints "Young adult.".

Nested if..else Conditional Statement


Nested if..else means an if-else statement inside another if statement. We can use
nested if statements to check conditions within conditions.
Nested If Else

age = 70
is_member = True

if age >= 60:


if is_member:
print("30% senior discount!")
else:
print("20% senior discount.")
else:
print("Not eligible for a senior discount.")

Output
30% senior discount!

Practice Questions
Multiple Choice Questions
1. Who created Python?
a) Dennis Ritchie b) Guido van Rossum c) James Gosling d) Charles Babbage
2. Python was created in the year —
a) 1989 b) 1991 c) 2000 d) 1995
3. Which of the following is not a feature of Python?
a) Platform independent b) Interpreted c) Complex syntax d) Open source
4. Which of the following IDEs comes bundled with Python?
a) PyCharm b) IDLE c) VS Code d) Spyder
5. What will be the output of:
print("Hello", "Python")
a) HelloPython b) Hello,Python c) Hello Python d) Error
6. Variable names in Python —
a) Can start with a digit b) Can contain underscores
c) Can contain hyphens d) Are not case sensitive
7. Which of these is a valid variable name?
a) 2num b) my-name c) _score d) class

12
8. Python variables are —
a) Statically typed b) Dynamically typed c) Strongly typed d) None of these
9. Which function is used to take input from the user?
a) input() b) get() c) scan() d) read()
10. By default, the input() function returns data as —
a) int b) float c) str d) bool
11. What will be the output of the following code?
x, y, z = 1, 2.5, "Python"
print(y)
a) 1 b) 2 c) 2.5 d) Python

12. What is the output of the following code?


a = 10
a += 5
print(a)
a) 5 b) 10 c) 15 d) Error
13. Which of the following is an arithmetic operator?
a) and b) // c) == d) =
14. The output of
15 // 4
will be —
a) 3.75 b) 4 c) 3 d) Error
15. What is the result of this expression?
2 ** 3 ** 2
a) 512 b) 64 c) 256 d) 8
16. Identify the comparison operator from the following —
a) = b) == c) += d) and
17. Which logical operator has the highest precedence?
a) and b) or c) not d) All have same precedence
18. What will the following code print?
a, b = 5, 10
a, b = b, a
print(a, b)
a) 5 10 b) 10 5 c) 15 d) Error
19. What is the output of the code below?
name = "Alex"
age = 0
if name == "Alex" or name == "John" and age >= 2:
print("Hello! Welcome.")
else:
print("Good Bye!!")
a) Hello! Welcome. b) Good Bye!! c) Error d) None

13
20. Which of the following will not produce an error?
a) name = “123” b) 123name = “Sam” c) user-name = “Dev” d) class = “Ten”
21. Predict the output:
x = 10
y = "10"
print(x == y)
a) True b) False c) Error d) None
22. What is the output of:
print(100 / 10 * 10)
a) 1 b) 10 c) 100 d) 1000
23. What will happen if you run this code?
age = 12
if age <= 12:
print("Child")
elif age <= 19:
print("Teenager")
else:
print("Adult")
a) Child b) Teenager c) Adult d) Error
24. Consider this code:
price = float(input("Price of each rose?: "))
print(type(price))
If user enters 25, what will be displayed?
a) <class 'int'> b) <class 'str'> c) <class 'float'> d) Error
25. Which of these best describes Python’s operator precedence rule?
a) Operators evaluated from right to left only
b) All operators have equal priority
c) Operators with higher precedence evaluated first
d) Parentheses evaluated last
26. A student wrote this code:
num = input("Enter number: ")
print(num * 2)
and entered 5. The output will be —
a) 10 b) 55 c) Error d) None
27. Which concept allows using same variable for different data types?
a) Static typing b) Dynamic typing c) Polymorphism d) Overloading
28. Which of these examples demonstrates nested if?
a) An if inside an elif b) An if inside another if c) Two ifs side by side d) None
29. Analyze and find the output:
age = 70
is_member = True
if age >= 60:

14
if is_member:
print("30% senior discount!")
else:
print("20% senior discount.")
else:
print("Not eligible.")
a) 20% senior discount. b) Not eligible. c) 30% senior discount! d) Error

Answer Key
[Link] Answer [Link] Answer [Link] Answer

1 b 11 c 21 b

2 b 12 c 22 c
3 c 13 b 23 a

4 b 14 c 24 c
5 c 15 a 25 c

6 b 16 b 26 b

7 c 17 c 27 b
8 b 18 b 28 b

9 a 19 a 29 c
10 c 20 a

Short Answer Questions (2-Markers)


1. What is the difference between a variable and a constant in Python? Give one example of
each.
2. Why is indentation important in Python? Write a small example to show its use.
3. What is the output of the following code?
a=5
b=2
print(a ** b)
4. Define keywords. Give any two examples of Python keywords.
5. Predict the data type of the result of:
x = 10 / 4
Explain your answer.
6. Explain the purpose of the input() function with a simple example.
7. What will be the output of this code and why?
x=7
x += 3

15
print(x)
8. Distinguish between the == and = operators in Python.
9. Identify the error in the following code and correct it:
num = int("ten")
10. Explain the difference between int() and float() functions with an example.
11. Write a short Python code to swap two numbers without using a third variable.
12. Why is Python called a high-level and interpreted language?
13. Write a Python expression to calculate the area of a circle. Assume radius = r.
14. What will be the output and why?
print(3 + 2 * 4)
15. Write a short code that takes a number as input and prints whether it is positive or
negative (use if-else).
Short Answer Questions (3-Markers)
1. Explain any three features of Python that make it a popular programming language.
2. Write a program to accept two numbers from the user and display:
• Their sum
• Their difference
• Their product
3. What is the role of comments in Python? Write an example showing the use of both
single-line and multi-line comments.
4. Predict the output of the following code and explain your reasoning:
a = 10
b=5
c = a // b + a % b
print(c)
5. Write a Python program that takes a user’s name and age as input and prints a message
like:
Hello John, you are 15 years old.
6. Explain with examples how input() and print() functions are used for user interaction in
Python.
7. Identify and correct the errors in the following code snippet:
num = input("Enter number")
if num % 2 = 0:
print("Even")
else
print("Odd")
8. Write a Python program that calculates the average of three subject marks entered by the
user.

16
9. Compare and contrast the use of arithmetic, relational, and logical operators with one
example each.
10. What will be the output of the following code? Give reason for your answer.
x=4
y=2
z = x ** y + y ** x
print(z)
11. Define the term “syntax error.” Write a code snippet that generates a syntax error and
explain why it occurs.
12. Write a Python program to calculate the simple interest where
Principal = p, Rate = r, Time = t.
Use the formula SI = (p * r * t) / 100.
13. The following code gives an error. Identify the mistake and correct it.
Also explain what the corrected code will do.
name = "Priya"
print("Hello",name,"welcome to Python"

Practice Programming Questions


• Write a program to demonstrate the use of arithmetic operators with two variables.
• Write a program to find the area of a rectangle using given length and breadth.
• Accept the user’s name as input and display a greeting message.
• Write a program that takes a number as input and checks if it is positive, negative, or
zero.
• Write a program to swap two variables without using a third variable.
• Write a program to accept marks in three subjects and display the average.
• Write a program to check whether a given character is a vowel or a consonant using
match-case.
• Write a program that takes the number of roses and price per rose as input,
calculates the total cost, and displays it.
• Write a program to find the largest of three numbers entered by the user.
• Write a program to take temperature in Celsius and convert it to Fahrenheit.
• Write a Python program to input radius and calculate both the area and
circumference of a circle.

*********************************************************************************************************

17

You might also like