Tab 1
,📘 Python Handbook — Chapter 1
Introduction, Setup, First Programs
1.1 What is Python?
Python is a high-level, interpreted, general-purpose programming language.
● High-level: you write instructions close to human language, not machine code.
● Interpreted: you don’t compile it manually — Python’s interpreter reads and runs
your code line by line.
● General-purpose: not limited to one domain. You can build web apps, AI,
automation scripts, data pipelines, games, etc.
1.2 Installing Python
● Windows: download from python.org. During install, check ✅ “Add Python to
PATH”.
Linux/Mac: usually pre-installed; check with:
python3 --version
● If missing, install with package manager (sudo apt install python3).
1.3 Writing First Program
You can use:
REPL (Read–Eval–Print Loop): type python3 in terminal → interactive mode.
>>> print("Hello World")
Hello World
, 1. Each line is executed immediately.
Script mode: save file first.py with:
print("Hello World")
Run:
python3 first.py
2.
1.4 Printing Multiple Lines
Python supports triple quotes (''' or """) for multi-line strings.
Code (Problem1.py):
print('''Twinkle Twinkle little star
How I wonder what you are
Up above the world so high
Like a diamond in the sky''')
Explanation:
● print() outputs text to console.
● '''...''' keeps newlines and formatting exactly as typed.
Output:
Twinkle Twinkle little star
How I wonder what you are
Up above the world so high
Like a diamond in the sky
●
Logic: instead of writing multiple print() statements, triple quotes let you print a whole
block.
, 1.5 Comments
Lines starting with # are comments. They don’t run, but explain code.
# This program was done using REPL
Use comments for documentation, debugging, or reminders.
1.6 Text-to-Speech Example
Code (Problem3.py):
import pyttsx3
engine = pyttsx3.init() # initialize TTS engine
engine.say("Hey I am good") # queue text
engine.runAndWait() # process and speak
Explanation:
● import pyttsx3: loads text-to-speech library.
● init(): sets up engine with default driver.
● say(): queue text.
● runAndWait(): actually speaks text and waits until finished.
Logic flow:
Program imports → initializes → queues → speaks.
Practice exercise: Modify to read a poem aloud.
1.7 Listing Files in Directory
Code (Problem4.py):
import os
,📘 Python Handbook — Chapter 1
Introduction, Setup, First Programs
1.1 What is Python?
Python is a high-level, interpreted, general-purpose programming language.
● High-level: you write instructions close to human language, not machine code.
● Interpreted: you don’t compile it manually — Python’s interpreter reads and runs
your code line by line.
● General-purpose: not limited to one domain. You can build web apps, AI,
automation scripts, data pipelines, games, etc.
1.2 Installing Python
● Windows: download from python.org. During install, check ✅ “Add Python to
PATH”.
Linux/Mac: usually pre-installed; check with:
python3 --version
● If missing, install with package manager (sudo apt install python3).
1.3 Writing First Program
You can use:
REPL (Read–Eval–Print Loop): type python3 in terminal → interactive mode.
>>> print("Hello World")
Hello World
, 1. Each line is executed immediately.
Script mode: save file first.py with:
print("Hello World")
Run:
python3 first.py
2.
1.4 Printing Multiple Lines
Python supports triple quotes (''' or """) for multi-line strings.
Code (Problem1.py):
print('''Twinkle Twinkle little star
How I wonder what you are
Up above the world so high
Like a diamond in the sky''')
Explanation:
● print() outputs text to console.
● '''...''' keeps newlines and formatting exactly as typed.
Output:
Twinkle Twinkle little star
How I wonder what you are
Up above the world so high
Like a diamond in the sky
●
Logic: instead of writing multiple print() statements, triple quotes let you print a whole
block.
, 1.5 Comments
Lines starting with # are comments. They don’t run, but explain code.
# This program was done using REPL
Use comments for documentation, debugging, or reminders.
1.6 Text-to-Speech Example
Code (Problem3.py):
import pyttsx3
engine = pyttsx3.init() # initialize TTS engine
engine.say("Hey I am good") # queue text
engine.runAndWait() # process and speak
Explanation:
● import pyttsx3: loads text-to-speech library.
● init(): sets up engine with default driver.
● say(): queue text.
● runAndWait(): actually speaks text and waits until finished.
Logic flow:
Program imports → initializes → queues → speaks.
Practice exercise: Modify to read a poem aloud.
1.7 Listing Files in Directory
Code (Problem4.py):
import os