0% found this document useful (0 votes)
7 views15 pages

Python Basics for Class IX Students

This document provides an introduction to Python programming for Class IX students, covering its uses, features, and basic syntax. Key topics include Python variables, lists, tuples, sets, dictionaries, and functions, along with examples of each. It emphasizes Python's simplicity, versatility, and the importance of indentation and variable naming rules.

Uploaded by

gtf22t2bm2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

Python Basics for Class IX Students

This document provides an introduction to Python programming for Class IX students, covering its uses, features, and basic syntax. Key topics include Python variables, lists, tuples, sets, dictionaries, and functions, along with examples of each. It emphasizes Python's simplicity, versatility, and the importance of indentation and variable naming rules.

Uploaded by

gtf22t2bm2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

"

!
#

SAGARMATHA S. B. SCHOOL

INTRODUCTION TO PYTHON – Class IX Notes-BY HEMANT POKHREL

What is Python?

Python is a popular programming language created by Guido van Rossum and released in 1991.

It is used for:
• Web development (server-side)
• Software development
• Mathematics
• System scripting

What Can Python Do?


• Python can be used on a server to create web applications.
• Python can be used alongside software to create workflows.
• Python can connect to database systems and read/modify files.
• Python can handle big data and perform complex mathematics.
• Python can be used for rapid prototyping or production-ready software
development.

Why Python?
• Works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
• Has simple syntax similar to English
• Requires fewer lines of code than many languages
• Runs on an interpreter system (quick prototyping)
• Supports procedural, object-oriented, and functional styles of
programming

Modes of Python
1. Script Mode
2. Command Mode

The First Python Program

print() is used to display content on the screen.

Example:

print("Hello, World!")

Note: Save your file with the extension .py

Python Indentation

Indentation refers to spaces at the beginning of a code line.


In Python, indentation is very important because it defines blocks of code.

Example:

if 5 > 2:
print("Five is greater than two!")

Python Variables

Variables are created when you assign a value to them.


Example:

x = 5
y = "Hello, World!"
print(x)
print(y)

Python variables:
• Do not need explicit declaration
• Can change type after assignment

Example:

x = 4 # int
x = "Sally" # str
print(x)

Variable Naming Rules


• Must start with a letter or underscore (_)
• Cannot start with a number
• Can contain only letters, numbers, and underscores
• Are case-sensitive (age, Age, AGE are different)
• Cannot use Python keywords

Legal variable names:

myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"

Illegal variable names:


2myvar = "John"
my-var = "John"
my var = "John"

Comments in Python

Used for documentation or explanations in code.

Single-line comment:

# This is a comment
print("Hello, World!")

Multi-line comment:

# This is a comment
# written in
# more than one line
print("Hello, World!")

Casting

Specify variable types manually if needed.

Example:

x = str(3) # '3'
y = int(3) # 3
z = float(3) # 3.0

Get the Data Type

Use the type() function to check the variable type.

x = 5
y = "John"
print(type(x))
print(type(y))

Single or Double Quotes

Both work the same for strings.

x = "John"
x = 'John'

Case Sensitivity

Python variable names are case-sensitive.

a = 4
A = "Sally"
# A will not overwrite a

Python Lists

Lists are used to store multiple items in a single variable.


They are ordered, changeable, and allow duplicates.

Example:

fruits = ["apple", "banana", "cherry"]


print(fruits)

Properties of Lists
1. Ordered – items have a defined order.
2. Changeable – you can modify the list.
3. Allow duplicates – repeated values are permitted.

Example:

fruits = ["apple", "banana", "cherry", "apple", "cherry"]


print(fruits)

List Length

fruits = ["apple", "banana", "cherry"]


print(len(fruits))

Different Data Types in Lists

list1 = ["apple", "banana", "cherry"]


list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]

Lists can contain mixed data types:


list1 = ["abc", 34, True, 40, "male"]

Type of a List

fruits = ["apple", "banana", "cherry"]


print(type(fruits))

Creating a List Using Constructor

fruits = list(("apple", "banana", "cherry"))


print(fruits)

Accessing List Items

fruits = ["apple", "banana", "cherry"]


print(fruits[1]) # Output: banana
print(fruits[-1]) # Output: cherry

Range of Indexes

fruits = ["apple", "banana", "cherry", "orange", "kiwi"]


print(fruits[1:4])


Check if Item Exists

fruits = ["apple", "banana", "cherry"]


if "apple" in fruits:
print("Yes, apple is in the fruits list")

Changing List Items

fruits = ["apple", "banana", "cherry"]


fruits[1] = "blackcurrant"
print(fruits)

Insert and Append

[Link](2, "watermelon")
[Link]("orange")

Remove Elements

[Link]("banana")

Loop Through a List

fruits = ["apple", "banana", "cherry"]


for x in fruits:
print(x)

List Comprehension

fruits = ["apple", "banana", "cherry", "kiwi", "mango"]


newlist = [x for x in fruits if "a" in x]
print(newlist)

Sort and Copy

[Link]()
copy_list = fruits×copy()

Join Lists

list1 = ["a", "b", "c"]


list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)

Tuples

Tuples are ordered and unchangeable collections.


Defined using round brackets.

fruits = ("apple", "banana", "cherry")


print(fruits)

Tuples can:
• Be indexed
• Allow duplicates
• Contain mixed data types

Tuple Operations

fruits = ("apple", "banana", "cherry")


print(len(fruits))
print(fruits[1])
print(fruits[-1])

Tuple Packing and Unpacking

fruits = ("apple", "banana", "cherry")


(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)

Join and Multiply Tuples

tuple1 = ("a", "b")


tuple2 = (1, 2)
tuple3 = tuple1 + tuple2
print(tuple3)

mytuple = fruits × 2
print(mytuple)

Sets

Sets are unordered, unchangeable, and do not allow duplicates.


Defined using curly braces {}.

Example:

fruits = {"apple", "banana", "cherry"}


print(fruits)

Add Items to a Set

thisset = {"apple", "banana", "cherry"}


[Link]("orange")

Add Multiple Items

tropical = {"pineapple", "mango"}


[Link](tropical)

Dictionaries

Dictionaries store data in key:value pairs.

mydict = {
"brand": "Ford",
"model": "Mustang",
"year": 1969
}
print(mydict["brand"])

• Ordered (from Python 3.7+)


• Changeable
• No duplicate keys

Python Modules

Modules are Python files containing code (functions, classes, or variables).

Example: [Link]

def add(x, y):


return x + y

def subtract(x, y):


return x - y

Using the module:

import calc
print([Link](10, 2))

Import with Alias

import calc as c
print([Link](5, 3))

Import Specific Items


Import Specific Items

from calc import add


print(add(10, 20))

Python Dates

import datetime
x = [Link]()
print(x)

Formatting Example:

x = [Link](2018, 6, 1)
print([Link]("%B"))

Python Math

import math
x = [Link](64)
print(x)

Other functions:

[Link](1.4)
[Link](1.4)
[Link]

Python Functions
Functions are defined using the def keyword.

Example:

def my_function():
print("Hello from a function")

my_function()

Function with Arguments

def greet(name):
print("Hello", name)

greet("Amit")

Default Parameter

def my_function(country="India"):
print("I am from", country)

my_function("Japan")
my_function()

*args and **kwargs

def my_function(*kids):
print("The youngest child is", kids[2])

my_function("Ram", "Mohan", "Raju")


def my_function(**child):
print("His last name is", child["lname"])

my_function(fname="Raju", lname="Amit")

You might also like