Computer Siksha

Introduction to Python for Class 9

Introduction to Python Programming

Introduction to Python for Class 9

What is Python?

History of Python Programming Language

Features of Python

Why Learn Python in Class 9?

Applications


Basic Syntax

Python has a clean and simple syntax. Unlike other languages, Python does not use curly brackets {} to define blocks of code. Instead, Python uses indentation (spaces or tabs). Therefore, proper spacing is very important in Python.

Key Syntax Rules

  • Python is case-sensitive. For example, Name and name are two different variables.
  • Each statement goes on a new line.
  • Indentation (4 spaces or 1 tab) defines blocks of code inside loops, functions, and conditions.
  • Comments begin with the # symbol. Python does not execute them.
  • Strings go inside single quotes 'hello' or double quotes "hello".

Example of Indentation

if 5 > 3:
    print("Five is greater than three")

Adding Comments

# This is a single-line comment
print("Comments help explain the code")

First Program

Traditionally, every programmer writes the Hello World program first. Similarly, in Python, you can display a message on the screen using just one line of code.

Program

print("Hello, World!")

Output

Hello, World!

The print() function displays output on the screen. Furthermore, you can use it to print numbers, text, and results of calculations.

More Examples of print()

print("Welcome to Python")
print(10 + 20)
print("Sum of 5 and 3 is", 5 + 3)

Output

Welcome to Python
30
Sum of 5 and 3 is 8

Keywords and Identifiers

Keywords

In Python, keywords are reserved words. They carry a fixed meaning and purpose in the language. Therefore, you cannot use them as variable names or function names.

  • Python 3 has 35 keywords.
  • All keywords are case-sensitive. For example, True is a keyword but true is not.

Some commonly used Python keywords:

KeywordPurpose
ifHandles conditional statements
elseWorks with if to handle an alternate condition
forCreates a loop
whileCreates a while loop
printDisplays output
inputTakes input from the user
True / FalseBoolean values
importImports a module
defDefines a function
returnReturns a value from a function

Identifiers

An identifier is the name you give to a variable, function, or class in Python. You create identifiers to label and use different values in your program.

Rules for writing identifiers:

  • An identifier can contain letters (A–Z, a–z), digits (0–9), and underscores (_).
  • It must start with a letter or an underscore, not a digit.
  • Identifiers are case-sensitive. So age and Age are different.
  • You cannot use a Python keyword as an identifier.
  • Spaces are not allowed in identifiers. Use an underscore instead, e.g., student_name.

Valid identifiers: name, age1, _score, student_name

Invalid identifiers: 1name, student name, for, my-score


Variables and Data Types in Python

Variables

A variable is a named storage location in the computer’s memory. You use variables to store data that your program can use and change later. In Python, you create a variable simply by assigning a value to it using the = sign.

name = "Rahul"
age = 14
marks = 95.5

Note that Python automatically detects the type of the variable. You do not need to declare the type separately. This feature is called dynamic typing.

Data Types

A data type tells Python what kind of value a variable holds. Python provides several built-in data types. The most common ones for Class 9 are:

Data TypeDescriptionExample
intInteger (whole number)age = 14
floatDecimal numbermarks = 95.5
strText / Stringname = "Rahul"
boolTrue or False valuepassed = True
listOrdered collection of itemsmarks = [90, 85, 78]

Checking Data Type with type()

You can use the type() function to check the data type of any variable.

x = 10
print(type(x))    # Output: <class 'int'>

y = "Hello"
print(type(y))    # Output: <class 'str'>

Input and Output

Output — print() Function

You use the print() function to display output on the screen. Additionally, you can print multiple values at once by separating them with commas.

print("My name is Rahul")
print("Age:", 14)
print("Marks:", 95.5)

Input — input() Function

The input() function allows your program to accept data from the user during runtime. Python always stores the input as a string by default.

name = input("Enter your name: ")
print("Hello,", name)

Taking Numeric Input

Since input() returns a string, you need to convert it to an integer or float to use it in calculations. You can do this using int() or float().

age = int(input("Enter your age: "))
print("You are", age, "years old.")

Example Program — Simple Calculator

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
total = a + b
print("Sum =", total)

Output

Enter first number: 10
Enter second number: 20
Sum = 30

Advantages and Disadvantages

Advantages

  • Easy to Learn: Python’s simple syntax makes it the best language for beginners. As a result, students can start writing programs very quickly.
  • Free and Open Source: You can download and use Python at no cost. Furthermore, you can share it freely.
  • Large Standard Library: Python comes with thousands of built-in modules, so you do not need to write code from scratch for common tasks.
  • Platform Independent: Python programs run on any operating system without modification.
  • Versatile: Python works well in web development, AI, automation, data science, and many other fields.
  • Strong Community Support: A large community of developers provides tutorials, forums, and free resources.
  • Readable Code: Python code is clean and easy to read, which consequently makes maintenance and debugging simpler.

Disadvantages

  • Slower Speed: Python runs slower than languages like C, C++, and Java because the interpreter executes it line by line, unlike compiled languages.
  • High Memory Use: Python consumes more memory compared to low-level languages, which is a concern for resource-limited devices.
  • Not Ideal for Mobile Development: Python is not commonly used for mobile app development.
  • Runtime Errors: Since Python is dynamically typed, some errors appear only when you run the program, not before.
  • Global Interpreter Lock (GIL): Python’s GIL limits the performance of multi-threaded programs.
  • Not Suitable for Low-Level Programming: Developers do not use Python for system-level programming like writing operating systems or device drivers.

FAQs

Q1. Who created Python?

Guido van Rossum created Python. He began developing it in 1989 and released the first version publicly in 1991.

Q2. Why is Python called a high-level language?

Python is called a high-level language because it is closer to human language than machine language. It automatically manages memory and other low-level tasks, so programmers focus on writing logic.

Q3. What is the file extension for Python programs?

Python saves programs with the .py extension. For example, hello.py or calculator.py.

Q4. What is the difference between int and float in Python?

int stores whole numbers like 10, 25, or 100. float stores decimal numbers like 3.14, 9.5, or 100.0.

Q5. What is indentation in Python?

Indentation means adding spaces or tabs at the beginning of a line to show that it belongs to a code block. Python uses indentation instead of curly brackets to define blocks in loops, functions, and conditions.

Q6. What does the input() function do?

The input() function takes data from the user during the execution of a program. It always returns the data as a string. Therefore, you need to convert it to int or float for numeric calculations.

Q7. Is Python used in real jobs?

Yes, Python is one of the most in-demand programming languages in the job market. Companies use it for web development, data analysis, artificial intelligence, and automation.


Conclusion

Python is a powerful yet beginner-friendly programming language that every Class 9 student should learn. To summarise, Python offers simple syntax, dynamic typing, platform independence, and a wide range of applications in AI, web development, and data science. As a result, it is now a core part of the CBSE computer science curriculum. By understanding the basics — such as variables, data types, input/output, keywords, and identifiers — students build a strong foundation for advanced programming in higher classes. Therefore, start practising Python regularly, write small programs every day, and gradually you will become confident in your coding skills.


Scroll to Top