Unit 5: Introduction to Python- CLASS 9 AI

πŸ”° 1. Python Basics

Your PDF starts by explaining why Python is so popular today.

βœ” We live in a world full of data

Phones, laptops, smart TVs, watches generate huge amounts of data.

βœ” Python handles this data efficiently

It has powerful libraries for:

  • Data handling
  • Data modelling
  • Data visualization
  • AI & Machine Learning

βœ” Creator of Python

  • Guido van Rossum
  • First version: Python 0.9.0 (1991)
  • Python 3.x is the most widely used today

πŸ”° 2. Uses of Python (Page 241)

Python is used for:

  • Web applications
  • Standalone applications
  • Scientific & statistical programs
  • Graphical user interfaces
  • AI & Machine Learning

Simple Definition

Python is a powerful, simple-to-learn programming language used everywhereβ€”from websites to AI machines.


πŸ”° 3. Features of Python (Pages 241–243)

Your PDF lists several important features:


⭐ 1. Easy to Learn (Syntax like English)

Example (from PDF):

age = 33
if age < 100:
    print("Wow! I am alive!")

Simple, readable, clean.


⭐ 2. Open Source & Free

You can download free from python.org
You can modify and redistribute Python freely.


⭐ 3. High Level Language

You don’t need to worry about:

  • Memory
  • Devices
  • Hardware

Python handles it automatically.


⭐ 4. Huge Standard Library

Contains modules for:

  • Math
  • GUI
  • OS interactions
  • Networking
  • Scientific computing

⭐ 5. Portable

Runs on:

  • Windows
  • Mac
  • Linux
  • Mobile devices
  • Web servers

⭐ 6. Both Compiler & Interpreter based

The PDF explains this clearly:

Source code β†’ Bytecode (Compiler)
Bytecode β†’ Machine code (Interpreter/PVM)

Compiler β†’ catches syntax errors
Interpreter β†’ stops program when runtime error occurs


⭐ 7. Indentation Makes Python Readable

Example in PDF (Page 242):
The block after if must be properly indented.


⭐ 8. Dynamic Typing

No need to declare type of variables.

x = 10
x = "Hello"

⭐ 9. Automatic Garbage Collection

Python automatically frees unused memory.


⭐ 10. Extensible Language

Can integrate with:

  • C
  • C++

πŸ”° 4. Installing Python (Page 243)

Download: www.python.org/downloads
Install β†’ Click Install Now
Then open from Start menu.


πŸ”° 5. Python Execution Process (Compiler + Interpreter)

Your PDF explains Python’s working beautifully:

βœ” Step 1

You write source code β†’ program.py

βœ” Step 2

Python compiler converts source code β†’ bytecode (.pyc)

βœ” Step 3

Python Interpreter (PVM) executes bytecode line-by-line.


πŸ”° 6. Python IDLE (Page 244)

IDLE = Integrated Development and Learning Environment.

Contains:

  • Editor
  • Shell
  • Debugger

Two modes:


⭐ Interactive Mode

  • Type one command
  • Get output immediately
  • Prompt: >>>

Example:

>>> (1 + 7) * 2
16

⭐ Script Mode

  • Write program
  • Save file (.py)
  • Run using F5

πŸ”° 7. Working with Numbers (Page 245)

The PDF gives a table of arithmetic examples.

Examples:

CommandDescription
2+2Simple addition
43 - 7*9Mixed arithmetic
(23+2*5)/5Division
15/27.5 (float result)
17//3Floor division
5%2Remainder
5**2Exponentiation (power)

πŸ”° 8. Strings in Python (Page 245–246)

Strings = Characters inside quotes.
Examples:

"Delhi"
"12345"
'Python is cool.'

⭐ Single quotes inside single quotes causes error

PDF example:

'Python isn't good' ❌ ERROR
"Python isn't good" βœ” Correct

πŸ”° 9. Indexing in Strings (Page 246)

Index starts from 0.

P S E U D O C O D E
0 1 2 3 4 5 6 7 8 9

Negative index starts from end:

E = -1
D = -2
O = -3

Accessing elements:

str[0] = P
str[-1] = E

πŸ”° 10. Script Mode (Page 247)

To create a program:

  1. File > New File
  2. Write code
  3. Save as .py
  4. Run β†’ Run Module / Press F5

Errors appear in the Shell.


πŸ”° 11. Comments in Python (Page 247)

Two types:

βœ” Single-line comment

# This is a comment

βœ” Multi-line comment

'''
comment lines
'''

OR

"""
comment lines
"""

πŸ”° 12. Variables (Page 248)

Variable = Memory container for values.

Example:

name = "Arjun"
age = 16
price = 78.5

⭐ Naming Rules (Identifiers)

  • Start with letter or underscore
  • No special symbols
  • No spaces
  • Cannot use keywords

Valid:
DOB, date_of_birth, ctr1

Invalid:
12name, my-name, a@b


πŸ”° 13. Data Types (Pages 248–250)


⭐ String

Inside quotes: "123", "Hello"


⭐ Integer (int)

Whole numbers
Memory: 4 bytes


⭐ Long

Very large integers (Only in old Python 2.x)
Example:
149L


⭐ Float

With decimals: 3.14, 5.78

Scientific notation:
x = 7.8e3


⭐ Boolean

True / False
Internally stored as 1 / 0.


πŸ”° 14. Arithmetic in Python (Page 249–250)

Operands = values
Operators = + – * / // % **

Unary operator

-5 converts positive to negative.


πŸ”° 15. Type Conversion (Page 251)


⭐ Implicit Conversion

Python converts automatically

a = 2 * 3.5  # becomes float

⭐ Explicit Conversion

Using functions:

int("3")
float(5)
str(62)

πŸ”° 16. Input and Output (Page 250–251)

input() β†’ always returns string
print() β†’ shows output

Example:

n = input("What is your name?")
print("Welcome", n)

πŸ”° 17. Dual Role of + Operator (Page 251)

βœ” Addition (numbers)

5 + 7 = 12

βœ” Concatenation (strings)

"5" + "7" = "57"

πŸ”° 18. Decision Making in Python (Pages 256–258)


⭐ if Statement

if number % 2 == 0:
    print("Even")

⭐ if–else Statement

if marks >= 33:
    print("Pass")
else:
    print("Fail")

πŸ”° 19. Loops in Python (Pages 257–260)


⭐ While Loop

Runs until condition becomes false.

i = 1
while i <= 10:
    print(i)
    i += 1

⭐ For Loop

Used for iterating through a list or range.

for i in range(1, 11):
    print(i)

⭐ break

Stops the loop.

⭐ continue

Skips to next iteration.


πŸ”° 20. Python Lists (Data Structures) (Pages 262–271)

Very detailed in PDF.


⭐ List Creation

cities = []
nums = [10, 20, 30]
mixed = [23, 'apple', 53.4]

⭐ List Indexing

mylist[0] β†’ first item  
mylist[-1] β†’ last item  

⭐ Slicing

mylist[2:6]
mylist[-5:-2]

⭐ Modify items

mylist[4] = "BIKE"

⭐ Add items

mylist.append("A")
mylist.extend([1,2,3])
mylist.insert(1, "New")

⭐ Remove items

mylist.remove("apple")
mylist.pop()     # last item

⭐ Sort & Reverse

mylist.sort()
mylist.reverse()

⭐ Count items

mylist.count("apple")

⭐ Copy list

new = mylist.copy()

⭐ Nested List

traincar = [['Raj',1], ['vacant',2]]
traincar[0][0]  # Raj