Punjab Board · New syllabus 2026

10th Class Computer Chapter 3 Notes

Introduction to Python Programming

Definition

The basic programming process typically includes: understanding/analyzing the problem, designing a solution (e.g. an algorithm or flowchart), writing the code, testing it, and fixing (debugging) any errors found — simply guessing code without first understanding the problem is not a valid step of this process.


When installing Python, it's important to check the 'Add Python to PATH' option during setup, so Python can be run directly from the command line (by typing `python`) without needing to type its full installation folder path every time.

Definition

Three main rules for naming variables in Python: a variable name must start with a letter or underscore (not a digit), can contain only letters, digits, and underscores (no spaces or special symbols), and cannot be one of Python's reserved keywords.


Reserved keywords are words Python itself uses for its own syntax and so cannot be used as variable names — three examples are `if`, `for`, and `class`.


Python is case-sensitive, so the variable names `age` and `Age` are NOT the same — Python treats them as two completely separate, independent variables.

Definition

Python's basic data types: integers (`int` — whole numbers, e.g. `25`), floats (`float` — numbers with a decimal point, e.g. `3.14`), strings (`str` — text, written in quotes, e.g. `"hello"`), and Booleans (`bool` — only `True` or `False`).


The difference between integer and float: an integer has no fractional/decimal part (e.g. `10`), while a float can represent fractional values (e.g. `10.5`) — even a whole-number value written with a decimal point (like `10.0`) is treated as a float, not an integer.

Definition

The `#` symbol is used for a single-line comment in Python; triple quotes (`'''` or `"""`, used as string literals) are commonly used to write multi-line comments.


Comments are important in programming because they explain what the code does (or why it does it) in plain language for other programmers (or the same programmer, later) reading the code — Python itself ignores comments entirely when running the program, so they have zero effect on how the program behaves.

Definition

The `print()` statement's purpose is to display output (text, numbers, or variable values) to the screen.


The `input()` function pauses the program, waits for the user to type something and press Enter, and returns whatever the user typed — but importantly, `input()` always returns this as a string, even if the user typed numbers.


The result of `input()` must often be converted using `int()` or `float()` before doing math with it, because `input()`'s string result can't be directly added, subtracted, multiplied, or divided the way a true number can — trying to do arithmetic on an un-converted string either causes an error or (for `+`) simply joins text together instead of adding numbers.

Example 1 — reading and using code output

What is the output of: `age = 25` then `print("Age: ", age)`?

  1. The variable `age` is assigned the integer .
  2. `print()` displays the text `"Age: "` followed by the value stored in `age`.

Answer: `Age: 25`

Definition

Arithmetic operators in Python: `+` (addition), `-` (subtraction), `*` (multiplication), `/` (division, always gives a float result), `//` (floor division, rounds down to the nearest whole number), `%` (modulus, gives the remainder), and `**` (exponent/power).

Example 2 — floor division

What is the output of `print(10 // 3)`?

  1. Floor division rounds this down to the nearest whole number.

Answer:

Example 3 — the modulus operator

What is the result of `17 % 5`?

  1. remainder (since , and ).

Answer:

Definition

The difference between `//` (floor division) and `/` (division): `/` always gives the exact, precise result as a float (e.g. `10 / 3` gives `3.3333...`), while `//` discards any fractional part and gives only the rounded-down whole number (e.g. `10 // 3` gives `3`).

Definition

`+=` is a compound assignment operator: `a += b` means 'add `b` to `a`, and store the result back in `a`' (equivalent to `a = a + b`). Similarly, `a -= b` means 'subtract `b` from `a`, and store the result back in `a`' (equivalent to `a = a - b`).


The string concatenation operator (`+`), when used with two strings, joins them together into one combined string, rather than adding numbers — e.g. `"Hello" + "World"` gives `"HelloWorld"`.

Definition

Operator precedence is the order in which Python evaluates different operators when several appear in one expression, similar to the order of operations in maths — e.g. in `2 + 3 * 4`, multiplication (`*`) has higher precedence than addition (`+`), so it's calculated first: , then , giving `14`, not `20`.


In Python, parentheses `()` have the highest precedence of all — anything inside them is always evaluated first, before any other operator is applied.

Definition

Comparison operators (like `>`, `<`, `==`, `!=`) compare two values and return a Boolean (`True` or `False`). Logical operators combine or modify Boolean values; Python's three logical operators are `and`, `or`, and `not`.


The expression `5 > 3 and 2 < 1` evaluates each comparison separately first: `5 > 3` is `True`, and `2 < 1` is `False`; then `and` requires both sides to be `True` for the whole expression to be `True`.

Example 4 — evaluating a combined logical expression

Find the result of `5 > 3 and 2 < 1`.

  1. `5 > 3` evaluates to `True`.
  2. `2 < 1` evaluates to `False`.
  3. `True and False` evaluates to `False`, since `and` needs both sides to be `True`.

Answer: `False`

Definition

The `!=` operator correctly demonstrates the 'not equal to' comparison in Python (e.g. `5 != 3` evaluates to `True`, since and are indeed not equal).

Definition

An `if` statement runs a block of code only when its condition is `True`, and simply does nothing (skips that block) if the condition is `False`. An `if-else` statement adds an alternative: if the condition is `True`, the `if` block runs; if it's `False`, the `else` block runs instead — so an `if-else` always runs exactly one of its two blocks, while a plain `if` might run nothing at all.

Definition

Function used to convert a string into an integer: `int()` (e.g. `int("25")` gives the integer `25`).

Example 5 — greeting the user by name

Write a Python program that takes a name from the user and displays a greeting.

  1. Use `input()` to get the name, store it in a variable.
  2. Use `print()` combined with that variable (via concatenation or by passing it as a second argument) to display a greeting.

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

Example 6 — summing two numbers

Write a Python program that takes two numbers from the user and displays their sum.

  1. Use `input()` twice to collect two values, converting each with `float()` (or `int()`) since `input()` returns strings.
  2. Add the two converted numbers and print the result.

Answer: num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) print("Sum:", num1 + num2)

Example 7 — averaging five numbers

Write a Python program that takes five numbers from the user and displays their average.

  1. Collect five numbers via `input()`, converting each to `float()`.
  2. Add all five together and divide by to get the average, then print it.

Answer: n1 = float(input("Number 1: ")) n2 = float(input("Number 2: ")) n3 = float(input("Number 3: ")) n4 = float(input("Number 4: ")) n5 = float(input("Number 5: ")) average = (n1 + n2 + n3 + n4 + n5) / 5 print("Average:", average)

Quick Recap

  • ●Programming process: understand design code test debug. Check 'Add Python to PATH' during install
  • ●Variable naming: start with a letter/underscore, letters/digits/underscores only, not a reserved keyword (e.g. `if`, `for`, `class`); Python is case-sensitive
  • ●Data types: int, float, str, bool. `#` for single-line comments, triple quotes for multi-line
  • ●`print()` displays output; `input()` returns a string (convert with `int()`/`float()` before math)
  • ●Arithmetic: `+ - * / // % **`; `/` gives exact float, `//` rounds down; `%` gives the remainder
  • ●`+=`/`-=` compound assignment; string `+` concatenates; precedence: `()` highest, then `*` `/` before `+` `-`
  • ●Comparison operators (`> < == !=`) Boolean; logical operators `and`/`or`/`not` combine Booleans
  • ●`if` runs a block only if true; `if-else` always runs exactly one of two blocks

Keep going

More 10th Class Computer notes

All 10th Class Computer chapters