Python Basics: Complete Revision Notes 9th-AI

Introduction
1. Python File Extension
Python programs are saved with the .py extension. When you create a Python file, always name it something like program.py or calculator.py.
Remember:
- Correct:
myprogram.py - Incorrect:
myprogram.python,myprogram.pt
2. Operators and Order of Operations
Arithmetic Operators
Python follows the PEMDAS/BODMAS rule for order of operations:
- Parentheses/Brackets – ()
- Exponentiation – **
- Multiplication & Division – *, /, //, %
- Addition & Subtraction – +, –
Example:
print(10 + 5 * 2) # Output: 20
# Multiplication happens first: 5 * 2 = 10
# Then addition: 10 + 10 = 20
Important Operators
| Operator | Purpose | Example |
|---|---|---|
+ | Addition | 5 + 3 = 8 |
- | Subtraction | 5 - 3 = 2 |
* | Multiplication | 5 * 3 = 15 |
/ | Division (with decimal) | 5 / 2 = 2.5 |
// | Floor Division (integer) | 5 // 2 = 2 |
% | Modulus (remainder) | 5 % 2 = 1 |
** | Exponentiation (power) | 5 ** 2 = 25 |
Key Difference: / vs //
/(Division): Gives the complete answer with decimalsprint(7 / 2) # Output: 3.5//(Floor Division): Gives only the whole number partprint(7 // 2) # Output: 3
3. Data Types in Python
Boolean Data Type (bool)
The bool data type stores only two values:
TrueFalse
Example:
is_student = True
is_adult = False
print(5 > 3) # Output: True
Common Data Types Summary
| Data Type | Description | Example |
|---|---|---|
int | Whole numbers | 10, -5, 0 |
float | Decimal numbers | 3.14, -0.5 |
str | Text/String | "Hello", 'Python' |
bool | True/False | True, False |
4. Essential Python Functions
The print() Function
Used to display output on the screen.
print("Hello World")
print(10 + 5)
print("Sum:", 15)
Variables
A variable is a named memory location that stores data.
name = "Rahul"
age = 15
marks = 95.5
is_passed = True
Rules for Variable Names:
- Can contain letters, numbers, and underscores
- Must start with a letter or underscore
- Case-sensitive (
Nameandnameare different) - No spaces allowed
Type Conversion Functions
int() – Converts to integer
num = int("25") # Converts string "25" to integer 25
x = int(3.9) # Converts float 3.9 to integer 3
Other conversion functions:
float()– Converts to decimal numberstr()– Converts to string
5. Python Syntax Rules
Case Sensitivity
Python is case-sensitive, meaning uppercase and lowercase letters are treated differently.
Name = "John"
name = "Jane"
print(Name) # Output: John
print(name) # Output: Jane
Comments
The # symbol is used for single-line comments.
# This is a comment
print("Hello") # This is also a comment
Indentation
Indentation (spaces at the beginning of a line) is used to define a block of code in Python.
if age >= 18:
print("You can vote") # This line is indented
print("Welcome!") # This line is also indented
6. Taking User Input
Use the input() function to get input from users:
name = input("Enter your name: ")
age = int(input("Enter your age: ")) # Convert to integer
7. Sample Programs You Must Practice
Program 1: Sum of Two Numbers
# Taking input from user
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# Calculating sum
sum = num1 + num2
# Displaying output
print("Sum =", sum)
Program 2: Simple Interest Calculator
# Input
principal = float(input("Enter principal amount: "))
rate = float(input("Enter rate of interest: "))
time = float(input("Enter time period (years): "))
# Calculate Simple Interest
simple_interest = (principal * rate * time) / 100
# Output
print("Simple Interest =", simple_interest)
Formula: SI = (P × R × T) / 100
Program 3: Check Even or Odd Number
# Input
number = int(input("Enter a number: "))
# Check if even or odd
if number % 2 == 0:
print(number, "is Even")
else:
print(number, "is Odd")
Logic: If a number is divisible by 2 (remainder is 0), it’s even. Otherwise, it’s odd.
Program 4: Voting Eligibility Checker
# Input
age = int(input("Enter your age: "))
# Check eligibility using simple if statement
if age >= 18:
print("You are eligible to vote")
if age < 18:
print("You are not eligible to vote")
Note: Voting age in India is 18 years.
Quick Revision Checklist
make sure you can:
✅ Write the correct Python file extension (.py)
✅ Calculate expressions using BODMAS rule
✅ Identify data types (int, float, str, bool)
✅ Know all arithmetic operators, especially ** and //
✅ Use print() to display output
✅ Create and use variables
✅ Convert strings to integers using int()
✅ Write comments using #
✅ Use proper indentation in if statements
✅ Take input from users using input()
✅ Write programs for: sum, simple interest, even/odd, voting eligibility
Common Mistakes to Avoid
- Forgetting to convert input():
input()always returns a string, so convert it to int or float when needed. - Incorrect operator: Use
**for power, not^ - Wrong division operator: Remember the difference between
/and// - Indentation errors: Always indent code inside if statements properly
- Case sensitivity:
printandPrintare different in Python
Practice Tips
- Write code by hand – Practice writing programs on paper
- Run programs – Type and execute each program on your computer
- Modify examples – Change values and see what happens
- Solve previous papers – Practice similar questions
- Understand logic – Don’t just memorize, understand why the code works
