Python #1 Fundamentals
Think logically.
Code creatively.
Interactive study material for CS25C02 – Computer Programming: Python. Learn the fundamentals through explanations, worked examples, short activities, and a live Python runner in every topic.
Unit I learning path
3. Python Modes
Understand interactive mode, scripts, comments, indentation, and errors.
Study topic →1. Problem solving and problem analysis
Problem solving means understanding a task and developing a clear sequence of steps to produce the required result. A problem analysis chart records the inputs, processing, and outputs before coding.
Worked example: Area of a rectangle
| Input | Processing | Output |
|---|---|---|
| Length and breadth | Area = length × breadth | Area of rectangle |
Method: identify the known values, choose the formula, calculate the result, and display it.
Output will appear here.
2. Algorithms, flowcharts, and pseudocode
An algorithm is a finite sequence of unambiguous steps that solves a problem. A flowchart uses symbols and arrows to show the steps visually. Pseudocode describes the logic in plain, structured language without strict programming syntax.
| Flowchart symbol | Purpose |
|---|---|
| Oval | Start / End |
| Parallelogram | Input / Output |
| Rectangle | Processing / calculation |
| Diamond | Decision, such as Yes / No |
| Arrow | Direction of flow |
Pseudocode: larger of two numbers
START
INPUT a, b
IF a > b THEN
DISPLAY a
ELSE
DISPLAY b
END IF
STOPOutput will appear here.
3. Interactive mode, script mode, comments, indentation, and errors
Interactive mode: type an expression at the Python prompt and see the result immediately. Script mode: save statements in a .py file and run the file.
- Comments: start with
#; Python ignores them during execution. - Indentation: spaces at the start of a line define a code block. Four spaces are the common convention.
- Syntax error: code breaks Python's grammar.
- Runtime error: an error occurs while the program is running.
- Logic error: the program runs but produces the wrong answer.
Output will appear here.
4. Variables, reserved words, and data types
A variable is a name that refers to a value. Python determines a value's type at runtime. Use descriptive names such as student_name and total_marks.
Identifier rules
- Start with a letter or underscore; do not start with a digit.
- Use letters, digits, and underscores only.
- Names are case-sensitive:
scoreandScorediffer. - Do not use reserved keywords such as
if,for,class, orreturn.
| Type | Example | Meaning |
|---|---|---|
int | age = 19 | Integer |
float | height = 1.72 | Decimal number |
str | name = "Jamal" | Text |
bool | passed = True | True or False |
NoneType | value = None | No value assigned |
Output will appear here.
5. Arithmetic operators and expressions
| Operator | Operation | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 2 | 9 |
- | Subtraction | 7 - 2 | 5 |
* | Multiplication | 7 * 2 | 14 |
/ | True division | 7 / 2 | 3.5 |
// | Floor division | 7 // 2 | 3 |
% | Remainder | 7 % 2 | 1 |
** | Exponentiation | 7 ** 2 | 49 |
Parentheses can make the intended order clear. In general, exponentiation is evaluated before multiplication and division, which are evaluated before addition and subtraction.
Output will appear here.
6. Built-in functions and importing from packages
Python provides built-in functions that are ready to use. A module is a file containing Python definitions and statements. Import a module to use additional functionality.
| Function | Example | Purpose |
|---|---|---|
print() | print("Hello") | Display output |
type() | type(4.2) | Inspect a value's type |
len() | len("Python") | Count items |
sum() | sum([2, 3, 4]) | Add values |
max() / min() | max([2, 9, 4]) | Find largest / smallest |
Example import: import math makes names such as math.sqrt() and math.pi available.
Output will appear here.
Practical activities
Complete these exercises by editing the relevant code lab. Check each task after you have run and understood your solution.
🧩 Quick self-check quiz
Choose one answer for each question, then check your score.
Recommended references
- Allen B. Downey, Think Python: How to Think Like a Computer Scientist, 2nd edition, 2016.
- Karl Beecher, Computational Thinking: A Beginner's Guide to Problem Solving and Programming, 2017.
- Official Python Tutorial
- Pyodide Quickstart Documentation
Comments
Post a Comment