Basic Python Notes for Class 9
1. Introduction to Python
Python is a high-level, interpreted programming language.
Created by Guido van Rossum and released in 1991.
Name inspired by Monty Python’s Flying Circus (a BBC comedy show).
Used for web development, data analysis, machine learning, game development, desktop
applications, and database applications.
Features of Python
✔ Easy to Read & Learn – High-level and expressive language.
✔ Cross-Platform – Runs on Windows, macOS, Linux.
✔ Open Source & Free – Available for everyone.
✔ Large Standard Library – Comes with built-in functions.
✔ Interpreted Language – Executes code line by line.
✔ Automatic Memory Management – Handles memory efficiently.
✔ Exception Handling – Allows error handling in programs.
Limitations of Python
✖ Slower execution (compared to compiled languages like C or Java).
✖ Weak in type-binding (variable types can change dynamically).
✖ Limited Libraries (compared to C, Java, and Perl).
2. Python Basics
Python Character Set
Letters: a-z, A-Z
Digits: 0-9
Special Symbols: +, -, *, /, { }, ( ), ;, :, etc.
White Spaces: Space, Tab, Enter
Tokens in Python
Smallest units in a program:
1. Keywords – Reserved words (e.g., if, else, for, while, print, def)
2. Identifiers – Variable names (name, age, sum_value)
3. Literals – Fixed values (123, "Hello", True, None)
4. Operators – Symbols for calculations (+, -, *, /, %)
5. Punctuators – Used for structure ({, }, (, ), #, =)
3. Working with Python
Installation & IDEs
Download from [Link] (includes Python interpreter & IDLE).
Other IDEs: Anaconda (Spyder), PyCharm, Jupyter Notebook.
Python Modes
✔ Interactive Mode – One command at a time (Python Shell).
✔ Script Mode – Writing and saving Python programs as .py files.
4. Variables & Data Types
Variables
Store data and can be changed later.
Python uses dynamic typing (no need to declare type).
x = 10 # Integer
y = 5.5 # Float
name = "John" # String
is_active = True # Boolean
Data Types
Integer (int) – Whole numbers (10, -5, 0)
Float (float) – Decimal numbers (5.5, -3.14, 0.0)
String (str) – Text ("Hello", 'Python')
Boolean (bool) – True or False
Complex (complex) – Numbers with j (3 + 5j)
None – Represents no value.
Type Conversion
Convert between data types using:
int("10") # Converts string to integer
float(5) # Converts integer to float
str(100) # Converts number to string
5. Input & Output in Python
Printing Output
print("Hello, World!")
print("My age is", 15) # Multiple values
Taking User Input
name = input("Enter your name: ")
age = int(input("Enter your age: ")) # Convert input to integer
print("Hello", name, "You are", age, "years old.")
6. Operators in Python
Arithmetic Operators
✔ + Addition
✔ - Subtraction
✔ * Multiplication
✔ / Division
✔ // Floor Division (No decimal)
✔ % Modulus (Remainder)
✔ ** Exponentiation
Example:
a = 10
b=3
print(a + b) # Output: 13
print(a // b) # Output: 3
print(a ** b) # Output: 1000
Comparison Operators
✔ == Equal to
✔ != Not equal to
✔ > Greater than
✔ < Less than
✔ >= Greater than or equal to
✔ <= Less than or equal to
Logical Operators
✔ and – True if both conditions are true.
✔ or – True if at least one condition is true.
✔ not – Reverses the result.
Example:
x=5
y = 10
print(x > 0 and y > 0) # True
print(x < 0 or y > 0) # True
print(not(x > 0)) # False
7. Conditional Statements (if-else)
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
✔ elif (else if) – Multiple conditions:
marks = int(input("Enter marks: "))
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 50:
print("Grade: C")
else:
print("Grade: F")
8. Loops in Python
For Loop
Repeats a block of code multiple times.
for i in range(1, 6):
print("Hello", i)
While Loop
Repeats as long as the condition is true.
x=1
while x <= 5:
print(x)
x += 1
✔ Break & Continue Statements
for i in range(1, 6):
if i == 3:
break # Stops the loop
print(i)
for i in range(1, 6):
if i == 3:
continue # Skips 3 and continues
print(i)
9. Functions in Python
Reusable block of code.
def greet():
print("Hello, Welcome!")
greet() # Function call
✔ Function with Parameters
def add(a, b):
return a + b
result = add(5, 3)
print(result) # Output: 8
10. Python Collections (Data Structures)
✔ List – Ordered, changeable collection.
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
✔ Tuple – Ordered, immutable collection.
numbers = (1, 2, 3)
print(numbers[1]) # Output: 2
✔ Dictionary – Key-value pairs.
student = {"name": "John", "age": 15}
print(student["name"]) # Output: John
✔ Set – Unordered, unique values.
colors = {"red", "blue", "green"}
11. Basic Python Programs
✔ Find the sum of two numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum:", a + b)
✔ Check even or odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
These notes cover the fundamentals of Python with examples for Class 9 students. 🚀