Lesson 3: Beginning Programming with Python

Back to articles
Series: WebDev 101 Part 3

Day 3: Beginning Programming with Python

Today, students moved from writing webpages to writing their first programs. HTML gives a webpage structure, but programming languages allow us to make decisions, repeat tasks, work with data, and solve problems.

For this course, we are using Python. Python is widely used in web development, automation, data science, artificial intelligence, and many other areas of software development. It is also a good language for beginners because its syntax is relatively clean and readable.

Installing Python

We started by installing Python on each Windows computer using the official installer from python.org.

Python needs to be installed before the computer can run Python files. The installation includes the Python interpreter, which reads and executes the instructions we write.

Basic Installation Steps

  1. Open a web browser.
  2. Go to the official Python website.
  3. Download the current Windows installer.
  4. Run the installer.
  5. Select the option to add Python to the system path, if it appears.
  6. Complete the installation.

Once Python was installed, students were ready to write and run Python code.

Installing the Python Extension for VS Code

Next, students installed the Python extension for Visual Studio Code.

The extension helps VS Code understand Python files and provides useful features such as:

  • Syntax highlighting
  • Code suggestions
  • Error detection
  • The ability to run Python files from VS Code
  • Debugging tools for future lessons

This turns VS Code into a more complete environment for working with Python.

Introducing Data Types

Programs work with different kinds of data. These different kinds are called data types.

We discussed several common types that appear in many programming languages:                                                                                                                                              

Type Example Purpose
Character 'A' A single character
String "Hello" Text
Integer 25 A whole number
Float 3.14 A number with a decimal
Array or list ["Alice", "Bob"] Multiple values stored together

Python uses these types without requiring us to declare them in advance. For example:

name = "Alice"
age = 14
height = 1.65

Python understands that name is a string, age is an integer, and height is a float.

We also discussed that Python does not use a separate character type in the same way as languages such as C or C++. In Python, a single character is simply a string with one character.

Working With a List

Students were then introduced to a list. A list stores several values together in one variable.

names = ["Alice", "Bob", "Charlie", "David"]

Instead of creating a separate variable for every name, we can keep the names together in one list.

Our First Loop

A loop allows the computer to repeat an instruction.

We used a for loop to print every name in the list:

names = ["Alice", "Bob", "Charlie", "David"]

for name in names:
    print(name)

The loop visits each item in the list one at a time.

On the first pass, name contains "Alice". On the next pass, it contains "Bob", and the process continues until the list is finished.

This demonstrated one of the main benefits of programming: we can write an instruction once and ask the computer to repeat it.

Making Decisions With an If Statement

Programs often need to make decisions. Python uses an if statement to run code only when a condition is true.

number = 4

if number == 4:
    print("The number is four")

The code inside the if statement only runs when the condition is true.

The Modulo Operator

We then introduced the modulo operator, written as %.

Modulo returns the remainder after division.

5 % 2

Dividing 5 by 2 leaves a remainder of 1.

6 % 2

Dividing 6 by 2 leaves no remainder, so the result is 0.

This gives us a simple way to determine whether a number is even:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for number in numbers:
    if number % 2 == 0:
        print(number)

The condition checks whether the remainder is zero after dividing the number by two. Only even numbers are printed.

Using the range Function

Typing every number into a list can become repetitive. Python provides the range() function to generate a sequence of numbers.

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

This prints the numbers 1 through 10.

The final number, 11, is not included. In Python, the ending value of a range is usually excluded.

We then combined range(), a loop, an if statement, and modulo:

for number in range(1, 11):
    if number % 2 == 0:
        print(number)

This program generates the numbers, checks each one, and prints only the even values.

A First Look at Time Complexity

Near the end of class, we briefly introduced a more advanced computer science topic called time complexity.

Students are not expected to fully understand this yet. The goal was simply to introduce the idea that different solutions may require different amounts of work as the amount of data grows. We do indeed live with huge quantities of data and thus, it's processing needs to be efficient!

Time complexity does not usually measure exact seconds. Instead, it describes how the work grows when the input becomes larger.

Some common examples include:

  • O(1) — constant time
  • O(log n) — logarithmic time
  • O(n) — linear time
  • O(n²) — quadratic time

We looked at a simple two-dimensional graph to see that these growth rates behave differently.

For now, students only need to know that this concept exists. It will become more meaningful later as we work with larger lists, searches, loops, and data structures.

Connecting Python to HTML

So far, students have written HTML by hand and used CSS to style it. Now they are learning programming logic through variables, lists, loops, and conditions.

The next step will be to connect these ideas.

We will begin working with files and use Python to create HTML content. This will demonstrate how a program can generate a webpage instead of requiring a person to manually write every repeated part.

For example, a loop could read a list of names and create one HTML list item for each name:

names = ["Alice", "Bob", "Charlie"]

for name in names:
    print("<li>" + name + "</li>")

This begins to bridge the gap between a logical programming language and a markup language.

HTML describes what a page contains. Python can make decisions, repeat work, and generate that HTML. This is an important step toward understanding how modern web applications create dynamic pages.

Main Takeaways

During Day 3, students:

  • Installed Python on Windows
  • Installed the Python extension for VS Code
  • Learned about strings, integers, floats, characters, and lists
  • Created and looped through a list of names
  • Used an if statement to make a decision
  • Used modulo to identify even numbers
  • Used the range function to generate numbers
  • Received a first introduction to time complexity
  • Discussed how Python will soon be used to create HTML files

The programs were small, but the concepts are fundamental. Loops, conditions, data types, and lists are used throughout nearly every area of software development.