SAGARMATHA S. B.
SCHOOL
CLASS IX – COMPUTER SCIENCE
CHAPTER: INTRODUCTION TO PYTHON BY HEMANT POKHREL
1. Introduction to Python
Python is a high-level, general-purpose programming language.
It was developed by Guido van Rossum in 1991.
Python is known for its simplicity, clarity, and versatility, making it one of the most popular
programming languages in the world today.
Uses of Python
Python can be used for:
• Web development (server-side web applications)
• Software and app development
• Data analysis and big data handling
• Artificial Intelligence (AI) and Machine Learning (ML)
• Mathematical and scientific computation
• System automation and scripting
Why Python is Popular
• Simple and easy to learn: Syntax similar to English.
• Cross-platform: Works on Windows, Mac, Linux, and Raspberry Pi.
• Interpreted language: Executes line by line (no compilation needed).
• Extensive libraries: Comes with thousands of built-in modules.
• Multiple paradigms: Supports procedural, object-oriented, and functional
programming styles.
2. Modes of Python
Python can be executed in two main ways:
1. Command Mode (Interactive Mode)
• Used for testing short codes directly.
• Executes immediately after pressing Enter.
• Example:
>>> print("Hello, Python!")
Hello, Python!
2. Script Mode
• Used for writing long programs.
• The file is saved with the extension .py.
• Example:
print("This is script mode.")
3. The First Python Program
To display any message or result on the screen, we use the print() function.
Example:
print("Hello, World!")
Output:
Hello, World!
Note:
Always save your Python programs with the extension .py
(e.g., first_program.py)
4. Python Indentation
In Python, indentation (spaces or tabs before a line) is very important.
It defines the block of code.
Unlike other languages, Python does not use curly braces {}.
If you miss or misuse indentation, Python will show an IndentationError.
Example:
if 5 > 2:
print("Five is greater than two!")
Correct
Incorrect (no indentation)
5. Python Variables
A variable is a name used to store data in memory.
It acts like a container that holds a value.
Example:
x = 5
y = "hemant"
print(x)
print(y)
Here,
• x stores an integer value (5)
• y stores a string (“hemant”)
Rules for Naming Variables
• Must start with a letter or underscore (_)
• Cannot start with a number
• Can contain only letters, numbers, and underscores
• Case-sensitive (age, Age, and AGE are different)
• Cannot be a Python keyword
Examples:
student_name, _roll, marks2
2name, my-name, class
Changing Variable Type
Python variables are dynamically typed, meaning the type can change.
x = 4 # int
x = "Sally" # now x becomes str
print(x)
6. Comments in Python
Comments are used to explain code and are ignored during execution.
• Single-line comment:
# This is a comment
print("Hello, World!")
• Multi-line comment:
# This is a comment
# written in more than one line
7. Data Types in Python
Common data types in Python are:
Data Type Example Description
int 5 Integer numbers
float 3.14 Decimal numbers
str “Hello” String (text)
bool True / False Boolean values
list [1,2,3] Ordered, changeable
collection
tuple (1,2,3) Ordered, unchangeable
collection
set {1,2,3} Unordered, no
duplicates
dict {“name”:“hemant”, Key-value pair
“age”:15}
8. Type Casting
You can convert one data type into another using casting.
Example:
x = str(3)
y = int(3)
z = float(3)
print(x, y, z)
9. Lists
A list is an ordered and changeable collection of items.
Example:
fruits = ["apple", "banana", "cherry"]
print(fruits)
Properties of Lists
• Ordered: Maintains item order
• Changeable: Items can be updated
• Allow duplicates
Example:
fruits[1] = "orange"
print(fruits)
Adding items:
[Link]("mango")
Removing items:
[Link]("apple")
Loop through list:
for item in fruits:
print(item)
10. Tuples
Tuples are ordered but unchangeable collections.
Example:
fruits = ("apple", "banana", "cherry")
print(fruits)
Supports duplicates
Can be accessed by index (fruits[0])
Cannot modify or remove items
Example of Unpacking:
(a, b, c) = fruits
print(a)
11. Sets
Sets are unordered and do not allow duplicates.
Example:
fruits = {"apple", "banana", "cherry", "apple"}
print(fruits) # duplicate "apple" is ignored
Add item:
[Link]("mango")
Join sets:
set1 = {"a", "b"}
set2 = {1, 2}
set3 = [Link](set2)
12. Dictionaries
Dictionaries store data in key:value pairs.
Example:
student = {
"name": "Amit",
"class": 9,
"age": 14
}
print(student["name"])
Add/Change values:
student["age"] = 15
Add new key:
student["address"] = "Dang"
13. Python Modules
A module is a separate file that contains Python code (functions or variables) which can be
reused.
Example ([Link]):
def add(x, y):
return x + y
Using the module:
import calc
print([Link](5, 3))
14. Date and Time
To work with dates, we use the datetime module.
Example:
import datetime
x = [Link]()
print(x)
Formatting date:
print([Link]("%B")) # prints full month name
15. Math Module
Python’s math module provides mathematical functions.
Example:
import math
print([Link](64))
print([Link])
16. Python Functions
A function is a block of code that runs only when called.
Defining a function:
def greet():
print("Hello Students!")
Calling the function:
greet()
Function with parameters:
def add(x, y):
print(x + y)
add(4, 5)
Function with default argument:
def country(name="Nepal"):
print("I am from", name)
country()
Summary for Exam
Topic Key Point
Python High-level, interpreted, simple syntax
Variable Used to store data
Data Type int, float, str, bool, list, tuple, set, dict
List Ordered, changeable
Tuple Ordered, unchangeable
Set Unordered, unique elements
Dictionary Key-value pairs
Module Separate file for reusable code
Function Block of reusable code
Date & Math Built-in modules for time and
calculations