Programming Concepts (Python) – Class 9 | Unit 7
Unit 7: Programming Concept – Class 9 ICT (Python)
7.1 Introduction to Programming Languages
Think of a programming language as a specialized language we use to communicate with computers. Just as we use English or Nepali to communicate with each other, we use languages like Python, C, or Java to give instructions to a computer.
7.2 Types of Programming Languages
Low-Level Languages: These are very close to the computer's native language (binary code). They are powerful but difficult for humans to understand.
High-Level Languages: These are designed to be easy for humans to read and write. Python is a perfect example! They use words and syntax that are similar to those of English.
Machine Language: This is the computer's mother tongue—a series of 1s and 0s. Ultimately, all languages must be converted into machine language for the computer to understand them.
7.3 Programming Tools: Flowchart and Algorithm
You wouldn't build a house without a plan, and you shouldn't write a program without one either!
Algorithm: An algorithm is a step-by-step plan to solve a problem. It's like a recipe for a dish. It lists all the steps you need to follow in the correct order to get the desired result. For example, the algorithm for making tea would be:
Start.
Boil water.
Add tea leaves.
Add sugar and milk.
Stir the mixture.
Pour into a cup.
Stop.
Flowchart: A flowchart is a diagram that visually represents your algorithm. It uses different shapes to show different types of actions and arrows to show the flow of control. This makes it very easy to see the logic of your program before you even start coding.
Oval: Start/End
Rectangle: A process or instruction (e.g., "Add 5 + 3")
Diamond: A decision (e.g., "Is age > 18?")
7.4 Coding, Testing, and Debugging
The process of creating a program looks like this:
Coding: Writing the instructions in a programming language.
Testing: Running the program to check if it works as expected.
Debugging: Finding and fixing errors, or "bugs," in your code. 🐛
7.5 Compiler and Interpreter
How does our easy-to-read Python code turn into 1s and 0s for the computer? Through a translator program.
Compiler: A compiler translates your entire program into machine code at once, creating a new file (an executable). It’s like translating a whole book and then giving you the finished translation. Languages like C++ use compilers.
Interpreter: An interpreter translates and runs your code line by line. It’s like having a live translator who translates each sentence as you speak. Python is an interpreted language, which makes it great for learning because you can see results instantly.
7.6 Introduction to Python Programming
Python is a high-level, interpreted language known for its simplicity and readability.
7.7 Basic Syntaxes
print("Hello, world!")
7.8 Input/Output Statements and String Formatting
name = input("Enter your name: ")
print(f"Hello, {name}!")
7.9 Data Types and Variables
age = 15 # Integer
name = "Ram" # String
price = 10.5 # Float
7.10 Type Casting
age = int("18")
7.11 Operators and Expressions
- Arithmetic: +, -, *, /
- Relational: ==, !=, >, <
- Logical: and, or, not
- Assignment: =, +=, -=
7.12 Conditional Statements
if age > 18:
print("Adult")
elif age == 18:
print("Just adult")
else:
print("Minor")
7.13 Iteration (Loops)
For Loop:
for i in range(5):
print(i)
While Loop:
i = 0
while i < 5:
print(i)
i += 1
7.14 List and Dictionary
fruits = ["apple", "banana", "cherry"]
student = {"name": "Sita", "age": 14}
7.15 Library Functions
String Functions:
name = "Ram"
print(name.upper())
print(name.lower())
print(name.center(10))
print(len(name))
Math Functions:
import math
print(abs(-5))
print(round(4.7))
print(pow(2, 3))
print(math.sqrt(16))
print(int(9.99))
POf course! I've expanded the previous guide to be more detailed, clear, and easy for a 9th-grade student to follow. I've added more analogies, explanations, and practical examples to make it longer and more comprehensive.
🚀 Unlock the World of Code: Your Ultimate Guide to Python for Class 9 ICT
Hello future programmer! Welcome to the most exciting part of your ICT journey. Have you ever used an app like eSewa or played a game on your phone and wondered, "How does this actually work?" The answer is code. And the great news is, you can learn to write it too!
This guide will walk you through the fundamental concepts of programming using Python, a language famous for being powerful yet simple to learn. Let's begin our adventure!
1. The Building Blocks of Programming
Before you write your first line of code, let's understand the basic ideas behind it all.
What is a Programming Language?
Imagine you want to ask your friend to get you a glass of water. You'd probably say, "Can you please get me a glass of water?" in English or Nepali. You both understand the language.
A programming language is a special language used to give instructions to a computer. A computer doesn't understand English or Nepali. It has its own languages, and our job as programmers is to learn one of them to tell the computer exactly what we want it to do. Popular examples include Python, Java, and C++.
Types of Programming Languages
Not all programming languages are the same. They are mainly divided into two types:
Low-Level Languages: These languages are very close to how a computer "thinks." They are a series of complex instructions that directly control the computer's hardware. It's like trying to operate a machine by pulling individual levers and flipping tiny switches. It's very powerful but also very difficult for humans.
High-Level Languages: These are the languages most programmers use today. They are designed to be easy for humans to read, write, and understand because their syntax is similar to English. Python is a high-level language. Instead of pulling levers, you just give a simple command like
print("Hello!"), and the language handles all the complex stuff for you.Machine Language: This is the computer's native tongue. It's made up of only two symbols:
1and0(binary code). Every single high-level or low-level instruction must eventually be translated into machine language for the computer's brain (the CPU) to understand it.
Planning Your Program: Algorithms & Flowcharts
You wouldn't start building a house without a blueprint, right? In programming, our plans are called algorithms and flowcharts.
Algorithm: An algorithm is a step-by-step plan to solve a problem. It's like a recipe for a dish. It lists all the steps you need to follow in the correct order to get the desired result. For example, the algorithm for making tea would be:
Start.
Boil water.
Add tea leaves.
Add sugar and milk.
Stir the mixture.
Pour into a cup.
Stop.
Flowchart: A flowchart is a diagram that visually represents your algorithm. It uses different shapes to show different types of actions and arrows to show the flow of control. This makes it very easy to see the logic of your program before you even start coding.
Oval: Start/End
Rectangle: A process or instruction (e.g., "Add 5 + 3")
Diamond: A decision (e.g., "Is age > 18?")
2. From Human Words to Machine Code
So, how does our friendly Python code, written in English-like words, get understood by the computer that only speaks in 1s and 0s? Through a special translator program.
Compiler: Imagine you wrote a book in Nepali and wanted your English-speaking friend to read it. You would give it to a translator who would translate the entire book at once and hand you back a complete English version. This is what a compiler does. It takes your whole program, translates it into machine code, and creates a new, executable file. Languages like C and C++ use compilers.
Interpreter: Now, imagine you are giving a speech in Nepali with a live interpreter. You say one sentence, and the interpreter immediately translates it for the audience. You say the next sentence, and they translate that one. This is exactly what an interpreter does. It reads your code line by line, translates it, and runs it. Python is an interpreted language. This is fantastic for beginners because if there's an error on a line, the program stops right there and tells you, making it easier to find and fix mistakes.
This whole process of creating a program is a cycle:
Coding: You write the program's instructions using a language like Python.
Testing: You run the program to see if it works correctly and gives the expected results.
Debugging: You find and fix the errors (called "bugs") in your code. Fun fact: the term "bug" came from a real moth that got stuck in an early computer!
3. Getting Started with Python 🐍
Python is loved by beginners and experts at companies like Google, YouTube, and NASA because its syntax is so clean and readable. Let's write some code!
Your First Program: "Hello, World!"
It's a tradition for every programmer to start with this simple program. It just prints a message to the screen.
# The print() is a function that displays whatever is inside the parentheses.
# Text (strings) must be enclosed in quotes "" or ''.
print("Hello, World!")
Talking to Your Program: Input and Output
A program is more fun when it's interactive. Let's ask the user for their name and greet them personally.
# The input() function displays a prompt and waits for the user to type something.
name = input("Please enter your name: ")
# We use an f-string (notice the 'f' before the quotes) to easily include variables in our text.
# The curly braces {} are placeholders for the variable's value.
print(f"Namaste, {name}! Welcome to the world of Python.")
Storing Information: Variables & Data Types
A variable is like a labeled box where you can store a piece of information. The type of information you store is called its data type.
Integer (
int): A box for whole numbers. Example:age = 15.String (
str): A box for text. Anything inside quotes is a string. Example:school_name = "Himalayan Secondary School".Float (
float): A box for numbers with decimal points. Perfect for prices or measurements. Example:price = 150.75.
# Declaring a few variables
student_name = "Sita" # This is a string (str)
student_age = 14 # This is an integer (int)
student_height = 5.2 # This is a float
Changing Data Types: Type Casting
What if you get a number from a user and want to do math with it? Remember, input() always gives you a string. You can't do math with a string like "14". You need to convert it to a number first. This conversion is called type casting.
age_string = input("How old are you? ") # Let's say the user types 14. age_string is "14".
# We must convert the string "14" into the integer 14.
age_number = int(age_string)
# Now we can perform math operations!
age_in_five_years = age_number + 5
print(f"In five years, you will be {age_in_five_years} years old.")
Making Decisions: Conditional Statements (if, elif, else)
This is how your program makes choices. It's like your own brain works: "IF it is raining, I will take an umbrella. ELSE IF it is sunny, I will wear a cap. ELSE, I will just go out."
temperature = 25 # degrees Celsius
if temperature > 30:
print("It's a hot day! Drink plenty of water.")
elif temperature < 15:
print("It's cold outside. Wear a jacket!")
else:
print("The weather is lovely and moderate.")
Repeating Actions: Loops (for & while)
Loops are essential for saving time and avoiding repetitive code.
For Loop: Use a
forloop when you know exactly how many times you want to repeat an action.# The range(5) function generates numbers from 0 up to (but not including) 5. print("I will count to 4 for you:") for number in range(5): print(number)While Loop: Use a
whileloop to repeat an action as long as a certain condition is true.print("\nA countdown from 5:")count = 5 while count > 0: print(count) count = count - 1 # This is crucial! It moves us closer to the end condition. print("Blast off! 🚀")
Organizing Data: Lists & Dictionaries
When you have multiple pieces of data, you need a way to store them together.
List: A list is an ordered collection of items, stored in square brackets
[]. Think of it as a numbered to-do list.# Computers start counting from 0, so the first item is at index 0.subjects = ["Math", "Science", "Computer", "English"] print(f"My first subject is: {subjects[0]}") # Accesses "Math"Dictionary: A dictionary stores data in
key: valuepairs, inside curly braces{}. It's like a student's profile card.student_profile = {"name": "Hari", "age": 15, "city": "Pathari"} print(f"The student's name is: {student_profile['name']}") # Accesses "Hari"
4. Using Python's Superpowers: Libraries & Functions
A function is a reusable block of code that performs a specific task. Python has many built-in functions. A library (or module) is a collection of pre-written functions and variables that you can import into your program to use.
String Functions
These are built-in functions to work with text.
print(message.upper()) # Output: PRACTICE MAKES PERFECT
print(message.lower()) # Output: practice makes perfect
print(len(message)) # Output: 20 (counts characters, including spaces)
Math Functions
For advanced math, you need to import the math library. It's like checking a math book out of the library to use its formulas.
import math # This line gives you access to the math library.
# Some useful functions are built-in (no import needed)
print(f"The absolute value of -10 is: {abs(-10)}") # Result: 10
print(f"7.8 rounded is: {round(7.8)}") # Result: 8
# These functions need the 'math' library
print(f"The square root of 64 is: {math.sqrt(64)}") # Result: 8.0
print(f"4 to the power of 3 is: {pow(4, 3)}") # Result: 64
Let's Get Practical! 💻
You learn programming by doing! Here’s your mission.
Install Python: Visit
and download the latest version for your computer.python.org Get an IDE: An IDE (Integrated Development Environment) is a code editor that makes programming easier. Thonny is perfect for beginners. VS Code and PyCharm are more advanced and powerful.
Code, Code, Code: Write your own small programs. Try changing the examples you saw above.
Comment Your Code: Always use the
#symbol to write notes explaining what your code does. This will help you and others understand your code later.
Project 1: A Simple Calculator
Let's build the simple calculator. Type this into your IDE and run it!
print("--- My Simple Calculator ---")
# 1. Get two numbers from the user.
num1_str = input("Enter the first number: ")
num2_str = input("Enter the second number: ")
# 2. Convert the string inputs to numbers (floats to allow decimals like 10.5).
num1 = float(num1_str)
num2 = float(num2_str)
# 3. Perform the calculations.
sum_result = num1 + num2
product_result = num1 * num2
# 4. Print the final results in a nice, clear format.
print("--- Results ---")
print(f"{num1} + {num2} = {sum_result}")
print(f"{num1} * {num2} = {product_result}")Project 2: Guess the Number Game
This is a super fun game that uses everything we've learned!
# --- Guess the Number Game ---
secret_number = random.randint(1, 20) # Generate a random number between 1 and 20
print("I am thinking of a number between 1 and 20.")
# Use a while loop to let the user guess multiple times
while True:
guess_str = input("Take a guess: ")
guess = int(guess_str) # Convert the guess to an integer
if guess < secret_number:
print("Your guess is too low.")
elif guess > secret_number:
print("Your guess is too high.")
else:
print(f"Congratulations! You guessed it! The number was {secret_number}.")
break # Exit the loop because the game is overNetra Koirala
Computer Science Educator
Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.
LinkedIn ProfileRelated Posts
Loading related posts…
Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.
Featured Post
Grade 10 Computer Science: Specification Grid & Model Questions
Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...
