Courses / Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course
3.2 Python Basics: Input/Output, Variables, Data Types and Operators
Unit 3 – Programming Fundamentals · 80 min read · 12 MCQs to check yourself
Python full course for beginners (Programming with Mosh)
1. Your first program
print("Assalam-o-Alaikum, Pakistan!")
print("CS", "XI", sep="-", end=" ")
print("FBISE")
Output:
Assalam-o-Alaikum, Pakistan!
CS-XI FBISE
print() displays output; sep sets the separator between items (default space) and end sets what comes after (default new line). A # starts a comment, ignored by Python.
2. Variables and data types
A variable is a named memory location that stores a value. Python creates it when you first assign: age = 17. Python is dynamically typed – the type comes from the value.
Naming rules: start with a letter or underscore; only letters, digits and _; no spaces; case-sensitive (Total ≠ total); not a keyword (if, for, class …). Good names: student_name, total_marks.
| Type | Meaning | Example | type() shows |
|---|---|---|---|
| int | Whole number | marks = 85 |
<class 'int'> |
| float | Decimal number | gpa = 3.75 |
<class 'float'> |
| str | Text in quotes | city = "Lahore" |
<class 'str'> |
| bool | True / False | passed = True |
<class 'bool'> |
| list | Ordered, changeable collection | [10, 20, 30] |
<class 'list'> |
x = ["apple", "banana", "mango"]
print(type(x))
x = "Hello World"
print(type(x))
x = 20.5
print(type(x))
Output:
<class 'list'>
<class 'str'>
<class 'float'>
3. Input and type conversion
input() always returns a string. Convert it with int(), float() or str().
name = input("Enter your name: ") # input: Ayesha | 17
age = int(input("Enter your age: "))
print("Hello", name + "!", "Next year you will be", age + 1)
Output:
Hello Ayesha! Next year you will be 18
(The prompts appear on screen too; only the result is shown here.) f-strings format output neatly: print(f"{name} scored {marks}/100").
4. Operators
| Category | Operators | Example (a = 17, b = 5) | Result |
|---|---|---|---|
| Arithmetic | + - * / |
a / b |
3.4 |
| Floor division | // |
a // b |
3 |
| Modulus (remainder) | % |
a % b |
2 |
| Exponent | ** |
b ** 2 |
25 |
| Comparison | == != > < >= <= |
a > b |
True |
| Logical | and or not |
a > 10 and b > 10 |
False |
| Assignment | = += -= *= /= |
a += 1 |
a becomes 18 |
| Membership | in, not in |
"a" in "cat" |
True |
Operator precedence (highest first): () → ** → unary - → * / // % (left to right) → + - → comparisons → not → and → or.
Model paper: evaluate (2 + 3 ** 2 + 4 // 2 * 5 % 3 - 1) * 2
3 ** 2= 94 // 2= 2, then2 * 5= 10, then10 % 3= 1 (same level, left to right)- inside brackets: 2 + 9 + 1 − 1 = 11
11 * 2= 22
print((2 + 3 ** 2 + 4 // 2 * 5 % 3 - 1) * 2)
print(7 / 2, 7 // 2, 7 % 2, 2 ** 3 ** 2)
Output:
22
3.5 3 1 512
Note: ** is evaluated right to left, so 2 ** 3 ** 2 = 2 ** 9 = 512.
5. Common beginner errors (model paper)
| Incorrect code | Problem | Corrected code |
|---|---|---|
num1 = 10 · num2 = "5" · result = num1 + num2 |
TypeError – cannot add int and str | num2 = 5 or result = num1 + int(num2) |
fruits = ["Islamabad", "Karachi", "Lahore"] · print(fruits[3]) |
IndexError – indexes are 0, 1, 2 | print(fruits[2]) |
num1 = 10 · num2 = 0 · print(num1 / num2) |
ZeroDivisionError | check if num2 != 0: before dividing |
Short and long questions
1. Evaluate the following expression using the correct order of operations in Python: (2 + 3 2 + 4 // 2 5 % 3 - 1) 2
Model answer
Step 1: 3 ** 2 = 9. Step 2: 4 // 2 = 2 → 2 * 5 = 10 → 10 % 3 = 1 (same precedence, left to right). Step 3: 2 + 9 + 1 − 1 = 11. Step 4: 11 * 2 = 22.
2. What will be displayed after executing the following statements? x = ["apple", "banana", "mango"]; print(type(x)); x = "Hello World"; print(type(x)); x = 20.5; print(type(x))
Model answer
<class 'list'>
<class 'str'>
<class 'float'>
3. Correct the following Python code segments by removing errors: (i) num1 = 10; num2 = "5"; result = num1 + num2; print(result) (ii) fruits = ["Islamabad", "Karachi", "Lahore"]; print(fruits[3]) (iii) num1 = 10; num2 = 0; result = num1 / num2; print(result)
Model answer
(i) TypeError (int + str): num2 = 5 (or result = num1 + int(num2)) → prints 15.
(ii) IndexError: valid indexes are 0–2: print(fruits[2]) → Lahore.
(iii) ZeroDivisionError: use a non-zero divisor, e.g. num2 = 2, or check if num2 != 0: print(num1 / num2) else: print("Cannot divide by zero").
4. Explain operators in Python: arithmetic, comparison, logical and assignment, with examples and precedence.
Model answer
Arithmetic: + − * / (true division) // (floor division) % (remainder) ** (power): 17 // 5 = 3, 17 % 5 = 2, 2 ** 3 = 8. Comparison (relational): == != > < >= <= give True/False: 5 > 3 → True. Logical: and (both true), or (at least one true), not (reverses): (5 > 3) and (2 > 4) → False. Assignment: = += −= *= /=: x += 2 means x = x + 2. Membership: in, not in. Precedence: () → ** → * / // % → + − → comparisons → not → and → or. Example: (2 + 3 ** 2 + 4 // 2 * 5 % 3 − 1) * 2 = (2 + 9 + 1 − 1) * 2 = 22.
This lesson ends with a quiz of 12 MCQs. Open the lesson on PrepOasis to take it and save your progress.
Previous: 3.1 Why Programming Matters: Programs, IPO and Real-World Solutions · Next: 3.3 Control Structures: Sequence, Selection and Repetition
All lessons in Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course