Python

Python is a clear, readable programming language used for everything from simple scripts to large applications. Its strengths are readable syntax, a rich standard library, and a focus on writing code that is easy to understand and maintain. Write small functions, name variables descriptively, and follow simple style rules (consistent indentation, short functions). This chapter shows basic Python patterns you will use often: printing, variables, input/output, simple functions, lists and loops, and a small working calculator for subtraction (final example). The examples are short, copyable, and practical so you can run them immediately.
Hello world
The classic first program prints text. Run this in a REPL or save and run a .py file.
print("Hello, world!")
Variables and input
Variables hold values; use descriptive names. Convert input strings to numbers before arithmetic using int() or float(). Avoid implicit conversions.
name = "Asha"
age = int(input("Age: "))
Small functions
Keep functions short: one job, clear inputs and outputs. Return values instead of printing from deep inside logic so code is testable.
def add(a, b):
return a + b
print(add(2, 3)) # 5
Lists and loops
Lists store ordered items; loop with for. Use descriptive loop variables and prefer direct iteration over index-based loops when possible.
fruits = ["apple", "banana", "cherry"]
for f in fruits:
print(f)
Final example — calculator for subtraction
Below is a working subtraction calculator saved as a single file. It reads two numbers, computes the result, and prints it. The specialized variable names are used inside the example code block only.
main.py
# main.py
minuend = float(input("Enter minuend: "))
subtrahend = float(input("Enter subtrahend: "))
difference = minuend - subtrahend
print("Difference:", difference)
MCQs
1. Which statement prints text to the console in Python?
(a) console.log("Hi")
(b) echo "Hi"
(c) print("Hi")
(d) System.out.println("Hi")
► (c) print("Hi")
2. What type does input() return by default?
(a) int
(b) float
(c) str
(d) bool
► (c) str
3. Which function converts the string "3.5" to a floating-point number?
(a) int("3.5")
(b) float("3.5")
(c) number("3.5")
(d) parseFloat("3.5")
► (b) float("3.5")
4. How do you define a function that returns the sum of two arguments?
(a) func add(a,b): return a+b
(b) def add(a, b): return a + b
(c) function add(a,b) { return a+b }
(d) let add = (a,b) => a+b
► (b) def add(a, b): return a + b
5. Which list method appends an item to the end?
(a) add()
(b) append()
(c) push()
(d) insert_end()
► (b) append()
6. Which built-in returns the number of items in a sequence?
(a) size()
(b) length()
(c) count()
(d) len()
► (d) len()
7. Which loop iterates directly over items of a list?
(a) for i = 0; i < len(lst); i++
(b) for item in lst:
(c) while (item in lst):
(d) foreach (item in lst)
► (b) for item in lst:
8. Which data type is immutable in Python?
(a) list
(b) dict
(c) tuple
(d) set
► (c) tuple
9. Which exception is raised when dividing by zero?
(a) ValueError
(b) ZeroDivisionError
(c) TypeError
(d) DivisionError
► (b) ZeroDivisionError
10. Which operator tests equality of value in Python?
(a) =
(b) ==
(c) ===
(d) :=
► (b) ==
11. What does the is operator check?
(a) Value equality
(b) Type equality only
(c) Object identity (same object)
(d) Whether variable exists
► (c) Object identity (same object)
12. Which keyword returns a value from a function?
(a) yield
(b) send
(c) return
(d) break
► (c) return
13. Which string method splits a string into parts by whitespace by default?
(a) cut()
(b) split()
(c) explode()
(d) tokenize()
► (b) split()
14. Which expression builds a new list by doubling each x in nums?
(a) [x * 2 for x in nums]
(b) map(lambda x: x*2, nums)
(c) list(map(lambda x: x*2, nums))
(d) All of the above
► (d) All of the above
15. How do you write a single-line comment in Python?
(a) // comment
(b) /* comment */
(c) # comment
(d) -- comment
► (c) # comment
16. What file extension is used for Python source files?
(a) .py
(b) .python
(c) .p
(d) .pt
► (a) .py
17. What does range(5) represent in Python 3?
(a) The numbers 1 through 5
(b) An iterable producing 0,1,2,3,4
(c) A list [0,1,2,3,4] exactly (always)
(d) A generator that yields strings
► (b) An iterable producing 0,1,2,3,4
18. Which is the recommended way to check an object's type for being a list?
(a) type(obj) == "list"
(b) isinstance(obj, list)
(c) obj.__class__ is list
(d) str(type(obj)) contains "list"
► (b) isinstance(obj, list)