0 to Infinity Lesson

Python Foundations

Go deeper into logic with conditions, loops, functions, and collections so your Python code becomes structured, reusable, and ready for real problems.

1. Overview

In Week 1, we usually learn printing, input, variables, datatypes, and simple calculations. In Week 2, we start making Python think. We use decisions, repetition, reusable blocks, and collections of data.

Conditions

Conditions help code decide what to do.

Loops

Loops help code repeat work without writing the same line again and again.

Functions

Functions help us package logic into reusable blocks.

Collections

Collections help us store many values together.

Structure

Code becomes clean, readable, and easy to improve.

Reusable thinking

We stop solving only one example and start building reusable solutions.

Final output: a student marks analyzer using conditions, loops, functions, lists, dictionaries, and clean program structure.
Advertisement

2. Week 2 learning path

This week is about moving from simple statements to structured programs.

Conditions
Loops
Functions
Collections
Mini Project
Topic Question it answers Example
Condition Should this code run? if marks >= 40:
Loop How do I repeat this? for mark in marks:
Function How do I reuse this logic? def find_grade(marks):
List How do I store many values? [80, 90, 75]
Dictionary How do I store named values? {"name": "Aarav", "marks": 85}

3. Conditions: if, elif, else

Conditions allow Python to choose between different paths.

marks = 75

if marks >= 40:
    print("Pass")
else:
    print("Fail")

Multiple conditions with elif

marks = 85

if marks >= 90:
    print("Grade A+")
elif marks >= 80:
    print("Grade A")
elif marks >= 70:
    print("Grade B")
elif marks >= 60:
    print("Grade C")
elif marks >= 40:
    print("Pass")
else:
    print("Fail")
Important: Python checks conditions from top to bottom. As soon as one condition becomes true, that block runs and the rest are skipped.

Indentation matters

Python uses indentation to understand which lines belong inside a condition.

age = 18

if age >= 18:
    print("You can vote")
    print("This line is inside the if block")

print("This line is outside the if block")
Wrong indentation is one of the most common beginner errors in Python.

4. Logical operators

Conditions become more powerful when we combine them using logical operators.

Operator Meaning Example
and Both conditions must be true age >= 18 and has_id == True
or At least one condition must be true city == "Varanasi" or city == "Lucknow"
not Reverses the condition not is_absent
age = 20
has_id = True

if age >= 18 and has_id:
    print("Entry allowed")
else:
    print("Entry not allowed")

Example: scholarship decision

marks = 92
attendance = 88

if marks >= 90 and attendance >= 85:
    print("Scholarship approved")
else:
    print("Scholarship not approved")

5. Loops: repeating work

Loops are used when we want to repeat something.

for loop with range

for i in range(5):
    print(i)

Output:

0
1
2
3
4

range start, stop, step

for i in range(1, 11):
    print(i)
for i in range(2, 21, 2):
    print(i)

Loop through a list

marks = [80, 75, 90, 60, 88]

for mark in marks:
    print(mark)

Loop with calculation

marks = [80, 75, 90, 60, 88]

total = 0

for mark in marks:
    total = total + mark

average = total / len(marks)

print("Total:", total)
print("Average:", average)
A loop is not only repetition. A loop is controlled repetition.

6. while loop

A while loop continues as long as a condition is true.

count = 1

while count <= 5:
    print(count)
    count = count + 1

Countdown example

count = 5

while count >= 1:
    print(count)
    count = count - 1

print("Start!")

Input validation example

password = ""

while password != "python123":
    password = input("Enter password: ")

print("Access granted")
A while loop can become infinite if the condition never becomes false.

7. break and continue

break stops a loop completely. continue skips the current round and moves to the next round.

break example

numbers = [10, 20, 30, 40, 50]

for number in numbers:
    if number == 30:
        break
    print(number)

Output:

10
20

continue example

numbers = [10, 20, 30, 40, 50]

for number in numbers:
    if number == 30:
        continue
    print(number)

Output:

10
20
40
50
Use break when the job is finished. Use continue when only the current item should be skipped.

8. Functions: reusable blocks of code

A function is a named block of code. It helps us avoid repeating the same logic.

def say_hello():
    print("Hello from Python")

say_hello()
say_hello()

Why functions are important

Without function With function
Same code repeated many times Write once, call many times
Hard to fix Fix in one place
Messy program Structured program
Difficult to test Easy to test function by function
A function is like a small machine. You give input, it performs logic, and it may give output.

9. Parameters and arguments

Parameters allow a function to receive values.

def greet(name):
    print("Hello", name)

greet("Aarav")
greet("Siya")
greet("Mohan")

Function with two parameters

def add(a, b):
    print(a + b)

add(10, 20)
add(5, 7)

Marks example

def check_pass(marks):
    if marks >= 40:
        print("Pass")
    else:
        print("Fail")

check_pass(75)
check_pass(32)
Word Meaning
Parameter Name used inside the function definition
Argument Actual value passed while calling the function

10. return: sending a result back

print() displays a value. return gives a value back to the caller.

def add(a, b):
    return a + b

result = add(10, 20)
print(result)

Grade function

def find_grade(marks):
    if marks >= 90:
        return "A+"
    elif marks >= 80:
        return "A"
    elif marks >= 70:
        return "B"
    elif marks >= 60:
        return "C"
    elif marks >= 40:
        return "Pass"
    else:
        return "Fail"

grade = find_grade(86)
print("Grade:", grade)

Reusable average function

def find_average(numbers):
    total = 0

    for number in numbers:
        total = total + number

    return total / len(numbers)

marks = [80, 90, 70, 85]
average = find_average(marks)

print("Average:", average)
Use return when the result should be used later.

11. Collections: storing many values

A collection stores multiple values. Python has several important collection types.

Collection Example Best use
List [10, 20, 30] Ordered, changeable data
Tuple (10, 20, 30) Ordered, fixed data
Dictionary {"name": "Aarav"} Named values
Set {10, 20, 30} Unique values

12. Lists

A list stores multiple values in order. Lists are changeable.

marks = [80, 75, 90, 60]

print(marks)
print(marks[0])
print(marks[-1])

List methods

marks = [80, 75, 90]

marks.append(88)
print(marks)

marks.remove(75)
print(marks)

marks.sort()
print(marks)

marks.reverse()
print(marks)

Loop through list

names = ["Aarav", "Siya", "Mohan"]

for name in names:
    print("Hello", name)

List comprehension

List comprehension is a short way to create a list.

squares = []

for number in range(1, 6):
    squares.append(number * number)

print(squares)

The same work can be written like this:

squares = [number * number for number in range(1, 6)]

print(squares)

13. Tuples

A tuple is like a list, but it cannot be changed after creation.

point = (10, 20)

print(point[0])
print(point[1])

When to use tuples

def find_min_max(numbers):
    minimum = min(numbers)
    maximum = max(numbers)
    return minimum, maximum

result = find_min_max([10, 50, 20, 5])

print(result)
print("Minimum:", result[0])
print("Maximum:", result[1])

14. Dictionaries

A dictionary stores data as key-value pairs.

student = {
    "name": "Aarav",
    "age": 15,
    "marks": 86
}

print(student["name"])
print(student["marks"])

Add and update values

student = {
    "name": "Aarav",
    "marks": 86
}

student["city"] = "Varanasi"
student["marks"] = 90

print(student)

Loop through dictionary

student = {
    "name": "Aarav",
    "age": 15,
    "marks": 86
}

for key, value in student.items():
    print(key, "=", value)

List of dictionaries

This is very important. Real data often looks like a list of dictionaries.

students = [
    {"name": "Aarav", "marks": 86},
    {"name": "Siya", "marks": 92},
    {"name": "Mohan", "marks": 68}
]

for student in students:
    print(student["name"], student["marks"])

15. Sets

A set stores unique values. Repeated values are automatically removed.

cities = {"Varanasi", "Lucknow", "Varanasi", "Patna"}

print(cities)

Remove duplicates from a list

numbers = [10, 20, 10, 30, 20, 40]

unique_numbers = set(numbers)

print(unique_numbers)

Set operations

a = {"Python", "Java", "C"}
b = {"Python", "JavaScript", "C"}

print(a.union(b))
print(a.intersection(b))
print(a.difference(b))
Operation Meaning
union() All unique values from both sets
intersection() Values common to both sets
difference() Values present in one set but not the other

16. Mini Project: Student Marks Analyzer

Now we combine conditions, loops, functions, and collections.

"""
Week 2 Mini Project
Student Marks Analyzer

Concepts used:
1. Conditions
2. Loops
3. Functions
4. Lists
5. Dictionaries
6. Reusable program structure
"""

def find_grade(marks):
    if marks >= 90:
        return "A+"
    elif marks >= 80:
        return "A"
    elif marks >= 70:
        return "B"
    elif marks >= 60:
        return "C"
    elif marks >= 40:
        return "Pass"
    else:
        return "Fail"


def find_average(marks_list):
    total = 0

    for marks in marks_list:
        total = total + marks

    return total / len(marks_list)


def print_student_report(student):
    name = student["name"]
    marks = student["marks"]

    total = sum(marks)
    average = find_average(marks)
    grade = find_grade(average)

    print("-" * 40)
    print("Name:", name)
    print("Marks:", marks)
    print("Total:", total)
    print("Average:", average)
    print("Grade:", grade)


students = [
    {"name": "Aarav", "marks": [80, 85, 90]},
    {"name": "Siya", "marks": [92, 88, 95]},
    {"name": "Mohan", "marks": [60, 68, 72]},
    {"name": "Riya", "marks": [35, 40, 38]},
    {"name": "Kabir", "marks": [75, 79, 82]}
]

print("=" * 40)
print("STUDENT MARKS ANALYZER")
print("=" * 40)

class_total = 0
highest_average = 0
topper_name = ""

for student in students:
    print_student_report(student)

    average = find_average(student["marks"])
    class_total = class_total + average

    if average > highest_average:
        highest_average = average
        topper_name = student["name"]

class_average = class_total / len(students)

print("=" * 40)
print("CLASS REPORT")
print("=" * 40)
print("Class average:", class_average)
print("Topper:", topper_name)
print("Highest average:", highest_average)
This is the real purpose of Week 2: not only learning syntax, but combining syntax into useful programs.

17. Full Week 2 Notebook

Copy this complete notebook into the Python editor and run it.

"""
Week 2: Python Foundations II
Programmer's Picnic by Champak Roy

Topics:
1. Conditions
2. Logical operators
3. for loops
4. while loops
5. break and continue
6. Functions
7. Parameters
8. return
9. Lists
10. Tuples
11. Dictionaries
12. Sets
13. Mini project
"""

print("=" * 60)
print("WEEK 2: PYTHON FOUNDATIONS II")
print("=" * 60)

# ------------------------------------------------------------
# 1. Conditions
# ------------------------------------------------------------

print("\n1. CONDITIONS")

marks = 85

if marks >= 90:
    print("Grade A+")
elif marks >= 80:
    print("Grade A")
elif marks >= 70:
    print("Grade B")
elif marks >= 60:
    print("Grade C")
elif marks >= 40:
    print("Pass")
else:
    print("Fail")

# ------------------------------------------------------------
# 2. Logical operators
# ------------------------------------------------------------

print("\n2. LOGICAL OPERATORS")

age = 20
has_id = True

if age >= 18 and has_id:
    print("Entry allowed")
else:
    print("Entry not allowed")

# ------------------------------------------------------------
# 3. for loop
# ------------------------------------------------------------

print("\n3. FOR LOOP")

for number in range(1, 6):
    print(number)

# ------------------------------------------------------------
# 4. Loop through list
# ------------------------------------------------------------

print("\n4. LOOP THROUGH LIST")

marks_list = [80, 75, 90, 60, 88]

total = 0

for mark in marks_list:
    total = total + mark

average = total / len(marks_list)

print("Marks:", marks_list)
print("Total:", total)
print("Average:", average)

# ------------------------------------------------------------
# 5. while loop
# ------------------------------------------------------------

print("\n5. WHILE LOOP")

count = 1

while count <= 5:
    print("Count:", count)
    count = count + 1

# ------------------------------------------------------------
# 6. break and continue
# ------------------------------------------------------------

print("\n6. BREAK AND CONTINUE")

numbers = [10, 20, 30, 40, 50]

print("Using break:")

for number in numbers:
    if number == 30:
        break
    print(number)

print("Using continue:")

for number in numbers:
    if number == 30:
        continue
    print(number)

# ------------------------------------------------------------
# 7. Functions
# ------------------------------------------------------------

print("\n7. FUNCTIONS")

def greet(name):
    print("Hello", name)

greet("Aarav")
greet("Siya")

# ------------------------------------------------------------
# 8. return
# ------------------------------------------------------------

print("\n8. RETURN")

def add(a, b):
    return a + b

result = add(10, 20)
print("Addition result:", result)

# ------------------------------------------------------------
# 9. Grade function
# ------------------------------------------------------------

print("\n9. GRADE FUNCTION")

def find_grade(marks):
    if marks >= 90:
        return "A+"
    elif marks >= 80:
        return "A"
    elif marks >= 70:
        return "B"
    elif marks >= 60:
        return "C"
    elif marks >= 40:
        return "Pass"
    else:
        return "Fail"

print("Grade for 86:", find_grade(86))
print("Grade for 35:", find_grade(35))

# ------------------------------------------------------------
# 10. Lists
# ------------------------------------------------------------

print("\n10. LISTS")

students = ["Aarav", "Siya", "Mohan"]

students.append("Riya")

for student in students:
    print(student)

# ------------------------------------------------------------
# 11. Tuples
# ------------------------------------------------------------

print("\n11. TUPLES")

point = (10, 20)

print("X:", point[0])
print("Y:", point[1])

# ------------------------------------------------------------
# 12. Dictionaries
# ------------------------------------------------------------

print("\n12. DICTIONARIES")

student = {
    "name": "Aarav",
    "age": 15,
    "marks": 86
}

for key, value in student.items():
    print(key, "=", value)

# ------------------------------------------------------------
# 13. Sets
# ------------------------------------------------------------

print("\n13. SETS")

cities = ["Varanasi", "Lucknow", "Varanasi", "Patna", "Lucknow"]

unique_cities = set(cities)

print("Original cities:", cities)
print("Unique cities:", unique_cities)

# ------------------------------------------------------------
# 14. Mini project
# ------------------------------------------------------------

print("\n14. MINI PROJECT: STUDENT MARKS ANALYZER")

def find_average(marks):
    total = 0

    for mark in marks:
        total = total + mark

    return total / len(marks)


def print_student_report(student):
    name = student["name"]
    marks = student["marks"]

    total = sum(marks)
    average = find_average(marks)
    grade = find_grade(average)

    print("-" * 40)
    print("Name:", name)
    print("Marks:", marks)
    print("Total:", total)
    print("Average:", average)
    print("Grade:", grade)


students_data = [
    {"name": "Aarav", "marks": [80, 85, 90]},
    {"name": "Siya", "marks": [92, 88, 95]},
    {"name": "Mohan", "marks": [60, 68, 72]},
    {"name": "Riya", "marks": [35, 40, 38]},
    {"name": "Kabir", "marks": [75, 79, 82]}
]

class_total = 0
highest_average = 0
topper_name = ""

for student in students_data:
    print_student_report(student)

    average = find_average(student["marks"])
    class_total = class_total + average

    if average > highest_average:
        highest_average = average
        topper_name = student["name"]

class_average = class_total / len(students_data)

print("=" * 40)
print("CLASS REPORT")
print("=" * 40)
print("Class average:", class_average)
print("Topper:", topper_name)
print("Highest average:", highest_average)

print("\n" + "=" * 60)
print("WEEK 2 NOTEBOOK COMPLETE")
print("=" * 60)

18. Python Editor

Run the Week 2 notebook here. Change marks, names, conditions, and functions. Observe how the output changes.

Embedded Python Editor Week 2 Python Foundations II

19. Practice tasks

1 Conditions

Take marks from the user and print whether the student passed or failed.

2 Grade calculator

Write an if elif else ladder to print grade from marks.

3 for loop

Print the multiplication table of 7 using a for loop.

4 while loop

Print numbers from 10 to 1 using a while loop.

5 Function

Write a function called square(number) that returns the square of a number.

6 List

Create a list of 5 marks and calculate total and average using a loop.

7 Dictionary

Create a student dictionary with name, age, city, and marks. Print all values.

8 Set

Create a list with duplicate city names and print only unique cities using a set.

Show solved practice answers
# 1. Conditions

marks = int(input("Enter marks: "))

if marks >= 40:
    print("Pass")
else:
    print("Fail")


# 2. Grade calculator

marks = int(input("Enter marks: "))

if marks >= 90:
    print("A+")
elif marks >= 80:
    print("A")
elif marks >= 70:
    print("B")
elif marks >= 60:
    print("C")
elif marks >= 40:
    print("Pass")
else:
    print("Fail")


# 3. Multiplication table

for i in range(1, 11):
    print("7 x", i, "=", 7 * i)


# 4. while loop countdown

count = 10

while count >= 1:
    print(count)
    count = count - 1


# 5. Function

def square(number):
    return number * number

print(square(5))


# 6. List total and average

marks = [80, 90, 70, 85, 95]

total = 0

for mark in marks:
    total = total + mark

average = total / len(marks)

print("Total:", total)
print("Average:", average)


# 7. Dictionary

student = {
    "name": "Aarav",
    "age": 15,
    "city": "Varanasi",
    "marks": 86
}

for key, value in student.items():
    print(key, "=", value)


# 8. Set

cities = ["Varanasi", "Lucknow", "Varanasi", "Patna", "Lucknow"]

unique_cities = set(cities)

print(unique_cities)

20. Final challenge

Create a library book tracker using Week 2 concepts.

Your program should use:

Show starter code
books = [
    {"title": "Python Basics", "author": "A", "price": 300, "available": True},
    {"title": "Data Science", "author": "B", "price": 650, "available": False},
    {"title": "Machine Learning", "author": "C", "price": 800, "available": True},
    {"title": "Web Development", "author": "D", "price": 450, "available": True}
]

# Write your functions below
Show solution
books = [
    {"title": "Python Basics", "author": "A", "price": 300, "available": True},
    {"title": "Data Science", "author": "B", "price": 650, "available": False},
    {"title": "Machine Learning", "author": "C", "price": 800, "available": True},
    {"title": "Web Development", "author": "D", "price": 450, "available": True}
]

def print_all_books(books):
    for book in books:
        print("-" * 40)
        print("Title:", book["title"])
        print("Author:", book["author"])
        print("Price:", book["price"])

        if book["available"]:
            print("Status: Available")
        else:
            print("Status: Not available")


def print_expensive_books(books, limit):
    print("Books above", limit)

    for book in books:
        if book["price"] > limit:
            print(book["title"], "-", book["price"])


def count_available_books(books):
    count = 0

    for book in books:
        if book["available"]:
            count = count + 1

    return count


print_all_books(books)
print_expensive_books(books, 500)

available_count = count_available_books(books)

print("Available books:", available_count)

21. What you now know

You can make decisions

You can use if, elif, else, and logical operators.

You can repeat work

You can use for loops, while loops, break, and continue.

You can write reusable code

You can create functions, pass parameters, and return results.

You can store collections

You understand lists, tuples, dictionaries, and sets.

You can combine ideas

You can build programs using conditions, loops, functions, and data together.

You are ready for data work

These ideas prepare you for arrays, Pandas, data cleaning, and Machine Learning.

Week 2 changes the way you write Python. Your code is no longer only a sequence of lines. It becomes logical, reusable, and structured.
Top