Week 1: Introduction to Algorithms & Python Basics
4280 words
21 min read
2026-09-12T00:00:00.000Z
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
Python execution model, variables, dynamic typing, primitive types, arithmetic operators, and standard I/O. # Programming in Python — Week 1: Introduction to Algorithms & Python Basics The official IITM Python syllabus lists **Week 1: Introduction to algorithms**.

Programming in Python — Week 1: Introduction to Algorithms & Python Basics
The official IITM Python syllabus lists Week 1: Introduction to algorithms. The course itself is meant to introduce Python with a basic problem-solving and algorithmic flavour, while later weeks move into conditionals, iteration, collections, files, modules, and Pandas/NumPy.
Because the source only gives the Week 1 heading rather than detailed subtopics, the explanations below are standard supporting material for “Introduction to algorithms”, not additional syllabus claims.
TL;DR
Before writing Python, think:
An algorithm is a precise, finite sequence of steps for solving a problem.
Python is then used to express those steps in executable form.
The main Week 1 ideas to internalize are:
- problem → input → processing → output
- algorithm vs program
- variables and expressions as algorithmic state
- assignment
- sequential execution
- tracing code manually
- input/output
- correctness
- edge cases
- basic Python syntax needed to express simple algorithms
1. What is an algorithm?
An algorithm is a well-defined sequence of steps for solving a problem.
Example problem:
Given two numbers, calculate their average.
Algorithm:
text1. Read a 2. Read b 3. Compute (a + b) / 2 4. Output the result
The algorithm describes what must happen.
It is independent of Python.
2. Algorithm vs program
An algorithm is the logical procedure.
A program is an implementation of that procedure in a programming language.
Algorithm
textread two numbers add them divide by 2 display result
Python program
pythona = float(input()) b = float(input()) average = (a + b) / 2 print(average)
So:
but a program can implement an algorithm.
3. Problem-solving pipeline
A useful model:
textProblem ↓ Understand requirements ↓ Identify input/output ↓ Design algorithm ↓ Translate into Python ↓ Run ↓ Test ↓ Correct/debug if necessary
Do not start typing code before understanding what the problem actually asks.
4. Input–Process–Output model
Many introductory problems fit:
Example:
Convert Celsius to Fahrenheit.
Input
CProcessing
F=59C+32Output
F5. Python implementation
pythoncelsius = float(input()) fahrenheit = (9 / 5) * celsius + 32 print(fahrenheit)
The code mirrors the algorithm.
6. Why algorithms come before syntax
Consider two programmers:
Programmer A
Knows Python syntax but has no solution.
Programmer B
Has a correct algorithm but needs to translate it into Python.
Programmer B is usually much closer to solving the problem.
Syntax tells Python how to express instructions.
The algorithm tells us which instructions are needed.
7. Properties of a useful algorithm
A good introductory algorithm should generally have:
Clear inputs
What information does it receive?
Clear outputs
What should it produce?
Precise steps
Instructions should not be ambiguous.
Termination
It should eventually finish.
Correctness
It should produce the intended result for valid inputs.
8. Precision
Humans understand vague instructions surprisingly well.
Computers do not.
Bad:
textmake the number nicer
Better:
textround the number to two decimal places
Programming forces us to convert vague intentions into exact operations.
9. Sequential execution
The simplest Python programs execute statements from top to bottom.
Example:
pythonx = 5 y = x + 3 z = y * 2 print(z)
Execution:
then:
then:
then output:
10. Program state
At any moment, variables hold values.
Example:
pythonx = 10 x = x + 5 x = x * 2
Trace:
| Statement | x |
|---|---|
x = 10 | 10 |
x = x + 5 | 15 |
x = x * 2 | 30 |
Final:
11. Assignment is not algebraic equality
Python:
pythonx = x + 1
does not mean the mathematical equation:
It means:
- evaluate the right side using current
x - store the result back into
x
If:
then:
pythonx = x + 1
produces:
12. Right-hand side first
For:
pythonx = x * 2 + 3
Python first evaluates:
pythonx * 2 + 3
using the old value of
x.Only afterward is the result assigned to
x.This is essential when manually tracing code.
13. Variables
A variable is a name referring to a value.
Example:
pythonage = 20 name = "A" temperature = 31.5
Variables let programs retain state.
14. Variable names
Python variable names can contain:
- letters
- digits
- underscore
_
but cannot begin with a digit.
Valid:
pythonage student_name score2
Invalid:
python2score
Descriptive names generally make algorithms easier to understand.
15. Literals
A literal directly represents a value.
Examples:
python10 3.14 "hello" True False
These correspond to different types of data.
16. Basic numeric types
Two basic numerical categories:
Integer
python10 -4 0
Floating-point number
python3.5 -0.25 10.0
Python commonly calls them:
textint float
17. Strings
Text values are represented as strings.
pythonname = "Jay"
or:
pythonmessage = 'Hello'
A string is not automatically interpreted as a number.
For example:
python"10"
and:
python10
are different values.
18. Boolean values
Python Boolean literals are:
pythonTrue False
These become particularly important in Weeks 2–3 when the course introduces conditionals. The official syllabus places Conditionals in Weeks 2 and 3.
19. Expressions
An expression computes a value.
Examples:
python2 + 3 x * 5 (a + b) / 2
If:
pythona = 4 b = 6
then:
python(a + b) / 2
evaluates to:
20. Basic arithmetic operators
Common Python operators:
text+ addition - subtraction * multiplication / division
Example:
pythona = 10 b = 4 print(a + b) print(a - b) print(a * b) print(a / b)
Outputs conceptually:
text14 6 40 2.5
21. Floor division
Python:
pythona // b
performs floor division.
Example:
python7 // 2
gives:
while:
python7 / 2
gives:
22. Remainder / modulo
Python:
pythona % b
gives the remainder.
Example:
python17 % 5
gives:
Because:
23. Exponentiation
Python uses:
python**
for powers.
Example:
python2 ** 5
gives:
Do not use:
python^
for exponentiation in Python.
24. Operator precedence
Expressions follow precedence rules.
Example:
python2 + 3 * 4
is:
not:
Use parentheses when the intended grouping matters.
25. Parentheses
Example:
pythonaverage = (a + b) / 2
Without parentheses:
pythona + b / 2
means:
which is a different algorithm.
26. print()
Python's:
pythonprint(...)
produces output.
Example:
pythonx = 10 print(x)
outputs:
text10
27. Expressions inside print
You can directly print an expression:
pythonprint(5 + 7)
Output:
text12
or store first:
pythonanswer = 5 + 7 print(answer)
Both can produce the same visible result.
28. input()
Python's:
pythoninput()
reads user input.
Example:
pythonname = input()
If user enters:
textJay
then conceptually:
pythonname = "Jay"
29. Important: input() gives text
A common beginner issue:
pythonx = input()
typically gives a string.
If user types:
text10
then
x behaves conceptually like:python"10"
not integer:
python10
So numerical input commonly needs conversion.
30. Converting input to integer
pythonx = int(input())
If input is:
text10
then:
pythonx
becomes integer:
31. Converting to float
pythonx = float(input())
allows values such as:
text3.5
to become numerical floating-point values.
32. Example: add two inputs
Wrong for numerical addition:
pythona = input() b = input() print(a + b)
If input is:
text10 20
string concatenation may produce:
text1020
rather than:
Correct numerical intent:
pythona = int(input()) b = int(input()) print(a + b)
33. Type conversion
Common conversions:
pythonint(...) float(...) str(...)
Examples:
pythonint("12") float("3.5") str(100)
These convert compatible values between representations.
34. Algorithm example — rectangle area
Problem:
Given length and width, find area.
Algorithm:
text1. Read length 2. Read width 3. area = length × width 4. Output area
Python:
pythonlength = float(input()) width = float(input()) area = length * width print(area)
35. Trace
Input:
text5 3
State:
| Step | length | width | area |
|---|---|---|---|
| Read length | 5 | — | — |
| Read width | 5 | 3 | — |
| Compute | 5 | 3 | 15 |
Output:
36. Algorithm example — seconds to minutes
Suppose:
Convert total seconds into whole minutes and leftover seconds.
For:
seconds:
So output:
text2 minutes 15 seconds
37. Python
pythontotal_seconds = int(input()) minutes = total_seconds // 60 seconds = total_seconds % 60 print(minutes) print(seconds)
This shows why floor division and modulo are useful algorithmically.
38. Quotient–remainder pattern
For positive integer n divided by d:
where:
and:
This pattern appears frequently in programming problems.
39. Example — swap two values
Suppose:
pythona = 5 b = 9
Goal:
texta = 9 b = 5
One algorithm uses a temporary variable:
pythontemp = a a = b b = temp
40. Swap trace
Initial:
After:
temp=5.pythontemp = a
After:
a=9.pythona = b
After:
b=5.pythonb = temp
Final:
41. Why a temporary variable works
If you did:
pythona = b b = a
starting from:
after first line:
The old value 5 has already been lost.
This is a state-update problem, not a syntax problem.
42. Python simultaneous assignment
Python also permits:
pythona, b = b, a
But algorithmically, it is still useful to understand why swapping requires preserving both original values.
43. Dry run / manual tracing
The Python course follows CT, so manually executing small programs remains important.
For:
pythonx = 4 y = 3 x = x + y y = x - y
trace:
Start:
After:
x=7, y=3.pythonx = x + y
Then:
y=4.pythony = x - y
Final:
44. Trace tables
Use them whenever assignments become confusing.
| Statement | x | y |
|---|---|---|
x = 4 | 4 | — |
y = 3 | 4 | 3 |
x = x + y | 7 | 3 |
y = x - y | 7 | 4 |
Do not calculate future lines using old state accidentally.
45. Code is executed, not interpreted as intention
Suppose:
pythonx = 5 y = 10 x = y y = x
A human may think:
They wanted to swap.
But Python performs exactly the written assignments.
Trace:
then:
then still:
Always solve what the program does, not what it appears to intend.
46. Correctness
An algorithm is correct when it produces the required output for every input within its intended domain.
Testing one example is not a proof of general correctness.
Example:
pythonaverage = a + b / 2
may accidentally produce the expected answer for some chosen inputs but is not the correct general formula for the average.
47. Test cases
Useful test categories include:
- ordinary cases
- zero
- negative values if allowed
- boundary values
- very small/large values
- values where operands are equal
Testing should try to expose mistakes.
48. Example: percentage
Problem:
Given obtained marks and total marks, calculate percentage.
Formula:
Python:
pythonobtained = float(input()) total = float(input()) percentage = obtained / total * 100 print(percentage)
49. Preconditions
The previous algorithm assumes:
This is a precondition: something that must be true for the algorithm's operation to make sense.
If:
division is invalid.
Later conditionals let us handle such cases explicitly.
50. Edge cases
An edge case lies at an unusual or boundary part of the allowed input.
For numerical algorithms:
- 0
- negative values
- maximum/minimum allowed values
- equal operands
Thinking about edge cases is part of algorithm design.
51. Syntax error
A syntax error means Python cannot parse the written program.
Example:
pythonprint("Hello"
Missing closing parenthesis.
The problem is the form of the program.
52. Runtime error
A program may be syntactically valid but fail while executing.
Example:
pythonx = 10 / 0
The syntax is valid.
But division by zero fails during execution.
53. Logical error
A program runs but gives the wrong result.
Example:
pythonarea = 2 * length + width
when the task was rectangle area:
These can be particularly tricky because the program may not crash.
54. Three broad error categories
textSyntax error ↓ program cannot be properly parsed Runtime error ↓ program fails during execution Logical error ↓ program runs but produces incorrect behaviour
Debugging means identifying and fixing such problems.
55. Comments
Python comments begin with:
python#
Example:
python## Calculate rectangle area area = length * width
Comments are for humans reading the code.
They are not executed as program instructions.
56. Good comments
Useful:
python## Convert total seconds into complete minutes minutes = total_seconds // 60
Less useful:
python## Add one to x x = x + 1
when the line already clearly says exactly that.
Comments should usually explain why or intent, not mechanically repeat obvious syntax.
57. Readability
The official course explicitly aims to teach readable code and debugging, alongside solving programming problems.
Compare:
pythonx = 100 y = 0.18 z = x + x * y
with:
pythonprice = 100 tax_rate = 0.18 final_price = price + price * tax_rate
Both may compute the same value, but the second exposes the algorithm more clearly.
58. Algorithmic decomposition
Suppose task:
Calculate final bill including discount and tax.
Do not treat it as one giant expression immediately.
Break it down:
text1. Read original price 2. Compute discount amount 3. Compute discounted price 4. Compute tax 5. Compute final price 6. Display result
Then translate each step.
59. Python version
pythonprice = float(input()) discount_rate = float(input()) tax_rate = float(input()) discount = price * discount_rate discounted_price = price - discount tax = discounted_price * tax_rate final_price = discounted_price + tax print(final_price)
Intermediate variables can make the algorithm easier to inspect and debug.
60. Equivalent algorithms
A problem may have more than one correct algorithm.
For example:
and:
are mathematically equivalent over ordinary real arithmetic.
Different algorithms can produce the same required result.
Later, you may compare them in terms of:
- clarity
- efficiency
- numerical behaviour
- memory
61. Algorithm efficiency intuition
Suppose two algorithms both solve the same problem.
Algorithm A uses:
text10 steps
Algorithm B uses:
text1,000,000 steps
Correctness alone does not capture the whole story.
Algorithm design also cares about computational resources.
Detailed algorithmic complexity is beyond the Week 1 syllabus heading, but the basic idea is worth retaining.
62. Input specification
Before coding, identify exactly what input means.
For example:
Input contains radius in centimetres.
Then:
pythonradius = float(input())
Do not silently assume:
- diameter
- metres
- integer only
Many programming mistakes begin with misunderstanding the input specification.
63. Output specification
Likewise, understand exactly what must be printed.
If expected output is:
text15
then printing:
textThe answer is 15
may be considered wrong by an automated grader.
Programming problems often care about exact output format.
64. Automated grading mindset
For coding assignments, assume the program will be tested on inputs you did not personally choose.
Therefore avoid:
pythonprint("Enter a number:")
unless the problem explicitly requests that prompt.
Usually graders expect only specified output.
65. Example
If task says:
Read two integers and print their sum.
Prefer:
pythona = int(input()) b = int(input()) print(a + b)
rather than:
pythona = int(input("Enter first number: "))
unless prompts are explicitly part of the required interface.
66. Type matters
Consider:
pythona = "2" b = "3"
Then:
pythona + b
produces string combination:
text"23"
not:
But:
pythonint(a) + int(b)
gives:
Always track both:
67. = vs ==
For later weeks, keep this distinction ready:
python=
is assignment.
python==
is equality comparison.
Example:
pythonx = 5
stores 5.
Whereas:
pythonx == 5
asks whether
x equals 5 and evaluates to a Boolean.Conditionals arrive officially in Week 2.
68. Statement vs expression
Expression
Produces a value:
pythonx + 5
Statement
Performs an action:
pythonx = x + 5
or:
pythonprint(x)
This distinction helps when reading programs.
69. Algorithm tracing workflow
For a simple Week 1 program:
- Write all input values.
- Record their types.
- Execute statements from top to bottom.
- Evaluate right-hand sides first.
- Update variable state after assignments.
- Record output only when
print()executes. - Do not infer unstated behaviour.
70. Worked trace
Program:
pythona = int(input()) b = int(input()) c = a + b a = c * 2 b = a - b print(a) print(b)
Input:
text3 5
Trace:
Initial:
Then:
Then:
Then:
Output:
text16 11
Common mistake: mental simultaneous execution
Python normally executes sequential statements one at a time.
Given:
pythonx = y y = x
the second statement sees the new
x.Do not mentally evaluate both from the original state unless Python syntax explicitly performs simultaneous assignment.
Common mistake: forgetting conversion
pythonage = input() next_age = age + 1
mixes string input with integer arithmetic.
The intended numerical algorithm usually needs:
pythonage = int(input()) next_age = age + 1
Common mistake: incorrect formula translation
Mathematics:
Correct Python:
python(a + b) / (c + d)
Not:
pythona + b / c + d
Parentheses matter.
Common mistake: integer division confusion
For:
python5 // 2
result is not:
It is floor division.
Be deliberate about choosing:
python/
versus:
python//
Common mistake:
% interpreted as percentageIn Python:
python%
is the modulo/remainder operator.
It does not mean "percentage."
To calculate 20% of x:
python0.20 * x
or:
python20 / 100 * x
Common mistake:
^ for powersPython:
python2 ** 3
gives:
Do not assume mathematical-looking:
python2 ^ 3
means exponentiation.
Common mistake: outputting intermediate state
Suppose:
pythonx = 5 y = x + 3 x = y * 2 print(x)
Only final
x is printed:Variable assignments do not automatically produce visible output.
Common mistake: assuming variable name controls type
This:
pythonnumber = "hello"
is syntactically possible.
The name
number does not force the value to be numeric.Type comes from the stored value, not the English meaning of the variable name.
Common mistake: changing the problem
If the question asks:
Print the product.
Do not additionally:
- round it
- convert it
- print explanations
- reject values
unless required.
Solve the specified problem.
80. Small problem-solving template
When you see a new problem, write:
textINPUT: What values are given? OUTPUT: What exactly must be produced? RELATION: How does output depend on input? ALGORITHM: What sequence of steps computes it? TRACE: Does it work on a simple example? CODE: Translate into Python.
This is a reliable Week 1 workflow.
81. Example using the template
Problem:
Read radius and print circumference.
Input
rOutput
CRelation
C=2πrAlgorithm
textread r compute 2πr print result
Python
pythonradius = float(input()) pi = 3.14159 circumference = 2 * pi * radius print(circumference)
82. Relationship to Computational Thinking
Your CT course introduced algorithmic ideas without depending heavily on a programming language. The Python course is explicitly the first formal programming course, and its goal is to turn that problem-solving foundation into Python programs.
So mentally:
textCT: understand algorithm manually Python: encode algorithm precisely and let the computer execute it
83. Course progression from here
The official Python sequence is:
textWeek 1 → Introduction to algorithms Week 2 → Conditionals Week 3 → Conditionals continued Week 4 → Iterations and Ranges Week 5 → Iterations and Ranges continued Week 6 → Basic Collections Week 7 → Basic Collections continued Week 8 → Basic Collections continued Week 9 → File Operations Week 10 → File Operations continued Week 11 → Module system Week 12 → Basic Pandas and NumPy processing
84. Week 1 compact cheat sheet
python## assignment x = 10 ## input as text x = input() ## numeric input x = int(input()) x = float(input()) ## output print(x) ## arithmetic a + b a - b a * b a / b a // b a % b a ** b
Remember:
and:
85. What you should actually master
By the end of Python Week 1, you should be comfortable with:
- Explaining what an algorithm is.
- Distinguishing an algorithm from its Python implementation.
- Identifying input, processing and output.
- Breaking simple problems into precise steps.
- Reading Python sequentially.
- Tracking variable state manually.
- Understanding assignment.
- Using basic numerical/string values.
- Evaluating arithmetic expressions.
- Using
/,//,%, and**correctly. - Using parentheses correctly.
- Reading input with
input(). - Converting input with
int()andfloat(). - Producing output with
print(). - Translating mathematical formulas into Python.
- Tracing a program line by line.
- Distinguishing syntax, runtime and logical errors.
- Testing normal and edge cases.
- Following exact input/output specifications.
- Writing simple readable programs.
If these are comfortable, Programming in Python Week 1 is under control.
Quick Recall & Flashcards
Test your active recall before proceeding to practice problems. Format:
Question :: Answer.- Week 1 Diagnostic: What is the output of
type(5 / 2)in Python 3? :: Answer: B — In Python 3, the/operator always performs true float division, producing2.5(<class 'float'>). For integer division, use//. - Week 1 Diagnostic: What does
int('10') + 5evaluate to? :: Answer: 15 — The string'10'is explicitly cast to integer10, and10 + 5 = 15.
Practice & Self-Check
Attempt these diagnostic questions to verify your operational understanding.
1
Question 1
What is the output of
type(5 / 2) in Python 3?MCQ
3 Unit Assessment
A. <class 'int'>
B. <class 'float'>
C. <class 'double'>
D. <class 'number'>
2
Question 2
What does
int('10') + 5 evaluate to?NAT
3 Unit Assessment