Week 1: Introduction to Algorithms & Python Basics

4280 words
21 min read
2026-09-12T00:00:00.000Z
Python Week 1: the first filter for runtime behavior
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**.

Visual strip

Relevant recall pieces for this note

Open gallery

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:
Problem→Algorithm→Program→Output\boxed{ \text{Problem} \rightarrow \text{Algorithm} \rightarrow \text{Program} \rightarrow \text{Output} }
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:
text
1. 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

text
read two numbers
add them
divide by 2
display result

Python program

python
a = float(input())
b = float(input())

average = (a + b) / 2

print(average)
So:
algorithm≠program\boxed{ \text{algorithm}\neq\text{program} }
but a program can implement an algorithm.

3. Problem-solving pipeline

A useful model:
text
Problem
  ↓
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:
Input→Processing→Output\boxed{ \text{Input} \rightarrow \text{Processing} \rightarrow \text{Output} }
Example:
Convert Celsius to Fahrenheit.

Input

CC

Processing

F=95C+32F=\frac95C+32

Output

FF

5. Python implementation

python
celsius = 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:
text
make the number nicer
Better:
text
round 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:
python
x = 5
y = x + 3
z = y * 2
print(z)
Execution:
x=5x=5
then:
y=8y=8
then:
z=16z=16
then output:
16\boxed{16}

10. Program state

At any moment, variables hold values.
Example:
python
x = 10
x = x + 5
x = x * 2
Trace:
Statementx
x = 1010
x = x + 515
x = x * 230
Final:
x=30\boxed{x=30}

11. Assignment is not algebraic equality

Python:
python
x = x + 1
does not mean the mathematical equation:
x=x+1.x=x+1.
It means:
  1. evaluate the right side using current x
  2. store the result back into x
If:
x=5,x=5,
then:
python
x = x + 1
produces:
x=6.x=6.

12. Right-hand side first

For:
python
x = x * 2 + 3
Python first evaluates:
python
x * 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:
python
age = 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:
python
age
student_name
score2
Invalid:
python
2score
Descriptive names generally make algorithms easier to understand.

15. Literals

A literal directly represents a value.
Examples:
python
10
3.14
"hello"
True
False
These correspond to different types of data.

16. Basic numeric types

Two basic numerical categories:

Integer

python
10
-4
0

Floating-point number

python
3.5
-0.25
10.0
Python commonly calls them:
text
int
float

17. Strings

Text values are represented as strings.
python
name = "Jay"
or:
python
message = 'Hello'
A string is not automatically interpreted as a number.
For example:
python
"10"
and:
python
10
are different values.

18. Boolean values

Python Boolean literals are:
python
True
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:
python
2 + 3
x * 5
(a + b) / 2
If:
python
a = 4
b = 6
then:
python
(a + b) / 2
evaluates to:
5.5.

20. Basic arithmetic operators

Common Python operators:
text
+   addition
-   subtraction
*   multiplication
/   division
Example:
python
a = 10
b = 4

print(a + b)
print(a - b)
print(a * b)
print(a / b)
Outputs conceptually:
text
14
6
40
2.5

21. Floor division

Python:
python
a // b
performs floor division.
Example:
python
7 // 2
gives:
3.3.
while:
python
7 / 2
gives:
3.5.3.5.

22. Remainder / modulo

Python:
python
a % b
gives the remainder.
Example:
python
17 % 5
gives:
2.2.
Because:
17=3(5)+2.17=3(5)+2.

23. Exponentiation

Python uses:
python
**
for powers.
Example:
python
2 ** 5
gives:
32.32.
Do not use:
python
^
for exponentiation in Python.

24. Operator precedence

Expressions follow precedence rules.
Example:
python
2 + 3 * 4
is:
2+(3⋅4)=142+(3\cdot4)=14
not:
(2+3)⋅4=20.(2+3)\cdot4=20.
Use parentheses when the intended grouping matters.

25. Parentheses

Example:
python
average = (a + b) / 2
Without parentheses:
python
a + b / 2
means:
a+b2a+\frac b2
which is a different algorithm.

26. print()

Python's:
python
print(...)
produces output.
Example:
python
x = 10
print(x)
outputs:
text
10

27. Expressions inside print

You can directly print an expression:
python
print(5 + 7)
Output:
text
12
or store first:
python
answer = 5 + 7
print(answer)
Both can produce the same visible result.

28. input()

Python's:
python
input()
reads user input.
Example:
python
name = input()
If user enters:
text
Jay
then conceptually:
python
name = "Jay"

29. Important: input() gives text

A common beginner issue:
python
x = input()
typically gives a string.
If user types:
text
10
then x behaves conceptually like:
python
"10"
not integer:
python
10
So numerical input commonly needs conversion.

30. Converting input to integer

python
x = int(input())
If input is:
text
10
then:
python
x
becomes integer:
10.10.

31. Converting to float

python
x = float(input())
allows values such as:
text
3.5
to become numerical floating-point values.

32. Example: add two inputs

Wrong for numerical addition:
python
a = input()
b = input()
print(a + b)
If input is:
text
10
20
string concatenation may produce:
text
1020
rather than:
30.30.
Correct numerical intent:
python
a = int(input())
b = int(input())
print(a + b)

33. Type conversion

Common conversions:
python
int(...)
float(...)
str(...)
Examples:
python
int("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:
text
1. Read length
2. Read width
3. area = length × width
4. Output area
Python:
python
length = float(input())
width = float(input())

area = length * width

print(area)

35. Trace

Input:
text
5
3
State:
Steplengthwidtharea
Read length5——
Read width53—
Compute5315
Output:
15\boxed{15}

36. Algorithm example — seconds to minutes

Suppose:
Convert total seconds into whole minutes and leftover seconds.
For:
135135
seconds:
135=2(60)+15.135=2(60)+15.
So output:
text
2 minutes
15 seconds

37. Python

python
total_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 nn divided by dd:
n=qd+rn=qd+r
where:
q=n//dq=n//d
and:
r=n%d.r=n\%d.
This pattern appears frequently in programming problems.

39. Example — swap two values

Suppose:
python
a = 5
b = 9
Goal:
text
a = 9
b = 5
One algorithm uses a temporary variable:
python
temp = a
a = b
b = temp

40. Swap trace

Initial:
a=5,b=9.a=5,\quad b=9.
After:
python
temp = a
temp=5.temp=5.
After:
python
a = b
a=9.a=9.
After:
python
b = temp
b=5.b=5.
Final:
a=9, b=5\boxed{a=9,\ b=5}

41. Why a temporary variable works

If you did:
python
a = b
b = a
starting from:
a=5,b=9,a=5,\quad b=9,
after first line:
a=9,b=9.a=9,\quad b=9.
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:
python
a, 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:
python
x = 4
y = 3
x = x + y
y = x - y
trace:
Start:
x=4, y=3.x=4,\ y=3.
After:
python
x = x + y
x=7, y=3.x=7,\ y=3.
Then:
python
y = x - y
y=4.y=4.
Final:
x=7, y=4\boxed{x=7,\ y=4}

44. Trace tables

Use them whenever assignments become confusing.
Statementxy
x = 44—
y = 343
x = x + y73
y = x - y74
Do not calculate future lines using old state accidentally.

45. Code is executed, not interpreted as intention

Suppose:
python
x = 5
y = 10
x = y
y = x
A human may think:
They wanted to swap.
But Python performs exactly the written assignments.
Trace:
x=5, y=10x=5,\ y=10
then:
x=10, y=10x=10,\ y=10
then still:
x=10, y=10.x=10,\ y=10.
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:
python
average = 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:
percentage=obtainedtotal×100.percentage= \frac{obtained}{total}\times100.
Python:
python
obtained = float(input())
total = float(input())

percentage = obtained / total * 100

print(percentage)

49. Preconditions

The previous algorithm assumes:
total≠0.total\neq0.
This is a precondition: something that must be true for the algorithm's operation to make sense.
If:
total=0,total=0,
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:
python
print("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:
python
x = 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:
python
area = 2 * length + width
when the task was rectangle area:
length×width.length\times width.
These can be particularly tricky because the program may not crash.

54. Three broad error categories

text
Syntax 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:
python
x = 100
y = 0.18
z = x + x * y
with:
python
price = 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:
text
1. 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

python
price = 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:
(a+b)/2(a+b)/2
and:
a/2+b/2a/2+b/2
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:
text
10 steps
Algorithm B uses:
text
1,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:
python
radius = 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:
text
15
then printing:
text
The 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:
python
print("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:
python
a = int(input())
b = int(input())
print(a + b)
rather than:
python
a = int(input("Enter first number: "))
unless prompts are explicitly part of the required interface.

66. Type matters

Consider:
python
a = "2"
b = "3"
Then:
python
a + b
produces string combination:
text
"23"
not:
5.5.
But:
python
int(a) + int(b)
gives:
5.5.
Always track both:
value + datatype\boxed{ \text{value + datatype} }

67. = vs ==

For later weeks, keep this distinction ready:
python
=
is assignment.
python
==
is equality comparison.
Example:
python
x = 5
stores 5.
Whereas:
python
x == 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:
python
x + 5

Statement

Performs an action:
python
x = x + 5
or:
python
print(x)
This distinction helps when reading programs.

69. Algorithm tracing workflow

For a simple Week 1 program:
  1. Write all input values.
  2. Record their types.
  3. Execute statements from top to bottom.
  4. Evaluate right-hand sides first.
  5. Update variable state after assignments.
  6. Record output only when print() executes.
  7. Do not infer unstated behaviour.

70. Worked trace

Program:
python
a = int(input())
b = int(input())

c = a + b
a = c * 2
b = a - b

print(a)
print(b)
Input:
text
3
5
Trace:
Initial:
a=3,b=5.a=3,\quad b=5.
Then:
c=3+5=8.c=3+5=8.
Then:
a=8⋅2=16.a=8\cdot2=16.
Then:
b=16−5=11.b=16-5=11.
Output:
text
16
11

Common mistake: mental simultaneous execution
Python normally executes sequential statements one at a time.
Given:
python
x = 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
python
age = input()
next_age = age + 1
mixes string input with integer arithmetic.
The intended numerical algorithm usually needs:
python
age = int(input())
next_age = age + 1

Common mistake: incorrect formula translation
Mathematics:
a+bc+d\frac{a+b}{c+d}
Correct Python:
python
(a + b) / (c + d)
Not:
python
a + b / c + d
Parentheses matter.

Common mistake: integer division confusion
For:
python
5 // 2
result is not:
2.5.2.5.
It is floor division.
Be deliberate about choosing:
python
/
versus:
python
//

Common mistake:
% interpreted as percentage
In Python:
python
%
is the modulo/remainder operator.
It does not mean "percentage."
To calculate 20% of xx:
python
0.20 * x
or:
python
20 / 100 * x

Common mistake:
^ for powers
Python:
python
2 ** 3
gives:
8.8.
Do not assume mathematical-looking:
python
2 ^ 3
means exponentiation.

Common mistake: outputting intermediate state
Suppose:
python
x = 5
y = x + 3
x = y * 2
print(x)
Only final x is printed:
16.16.
Variable assignments do not automatically produce visible output.

Common mistake: assuming variable name controls type
This:
python
number = "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:
text
INPUT:
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

rr

Output

CC

Relation

C=2πrC=2\pi r

Algorithm

text
read r
compute 2πr
print result

Python

python
radius = 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:
text
CT:
understand algorithm manually

Python:
encode algorithm precisely
and let the computer execute it

83. Course progression from here

The official Python sequence is:
text
Week 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:
RHS evaluated first→assignment\boxed{ \text{RHS evaluated first} \rightarrow \text{assignment} }
and:
trace the code, don’t guess its intention\boxed{ \text{trace the code, don't guess its intention} }

85. What you should actually master

By the end of Python Week 1, you should be comfortable with:
  1. Explaining what an algorithm is.
  2. Distinguishing an algorithm from its Python implementation.
  3. Identifying input, processing and output.
  4. Breaking simple problems into precise steps.
  5. Reading Python sequentially.
  6. Tracking variable state manually.
  7. Understanding assignment.
  8. Using basic numerical/string values.
  9. Evaluating arithmetic expressions.
  10. Using /, //, %, and ** correctly.
  11. Using parentheses correctly.
  12. Reading input with input().
  13. Converting input with int() and float().
  14. Producing output with print().
  15. Translating mathematical formulas into Python.
  16. Tracing a program line by line.
  17. Distinguishing syntax, runtime and logical errors.
  18. Testing normal and edge cases.
  19. Following exact input/output specifications.
  20. 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, producing 2.5 (<class 'float'>). For integer division, use //.
  • Week 1 Diagnostic: What does int('10') + 5 evaluate to? :: Answer: 15 — The string '10' is explicitly cast to integer 10, and 10 + 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

Active Recall Session

Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.