Lesson 1 - Programming in Python

Laying the Foundation for Data Analytics

This is your starting point for learning Python as a powerful tool for data analytics. Whether you are new to programming or already have some experience, this course and its lessons will help you understand how Python can assist in exploring and understanding data.

As you develop your Python skills, you’ll see how it can be used to manage, process, and analyze large amounts of information. You’ll also discover how artificial intelligence (AI) enhances data ecosystems, making it easier to spot trends and uncover insights.

By the end of this lesson, you will be able to:

  • Write a simple computer program in Python.
  • Understand fundamental concepts like variables and data types.
  • Use essential operations, including arithmetic and working with text.

By the end of this course, you will know how to:

  • Store values in variables and retrieve them later.
  • Work with numbers and text data effectively.
  • Load a large dataset into Python and explore what it contains.
  • Use conditional logic to answer questions and guide decisions.

No special background is needed—let’s get started!


Communicating with the Computer

To analyze data efficiently, you need a way to tell the computer what to do. We do this by writing instructions, also known as programming. To program a computer, we use a programming language. Here, we’ll focus on Python, a language widely used in data analytics.

Let’s try a simple task: tell Python to add two numbers, for example, 14 and 5. In your own coding environment, type:

print(14 + 5)

When you run this code, Python will display 19.

Run the code in VS Code
Running the code in VS Code
Try writing a similar line with different numbers and run it. This is your first step in directing the computer’s actions.


Showing Multiple Outputs with print()

You’re not limited to a single calculation. You can write multiple lines of code and display each result on its own line using the print() function.

For example:

print(14 + 5)
print(10 - 2)

When you run these instructions, you’ll see:

19
8

Both results appear because we used print() twice. Try writing two separate print() lines yourself. For one, perform an addition. For the other, try a subtraction. Run them and observe the two results.


Understanding Python’s Syntax

Each print() instruction should be on its own line. If you try to place two print() functions on the same line, you’ll get a syntax error. This happens because Python follows strict rules about how code is written, similar to how we follow grammar rules in spoken languages.

Learning to read and fix errors is an important skill. Sometimes, tools like AI-based code assistants can help identify and correct syntax errors before you run your code.

Give it a try: write three separate lines of code, each using print(). For instance:

print(20 + 10 + 5)
print(3)
print(-1)

Run the code and check the output.


What is a Program?

When you write instructions in Python, those instructions together form code. Each line is called a line of code. A set of lines that work together to produce a result is known as a computer program.

A program can be just one line or hundreds. When you run the code, the instructions (input) are processed, and the computer shows you the outcome (output).

If you don’t use print(), the result might not appear on the screen. For example:

2 + 3
print(6 + 4)

Only 6 + 4 will be shown (as 10), because we told Python to print it.

Try making a short three-line program where each line uses print(). For example:

  • On one line, show the result of 30 + 10.
  • On another, show a single number, like 30.
  • On the last line, show a negative number, like -30.

Run your code and confirm that all three results appear.


Adding Comments to Your Code

The computer reads and runs code line by line. It ignores blank lines, which you can use for clarity. It also ignores anything that follows a # on the same line. Such text is known as a comment, and it helps you describe what your code does without affecting how it runs.

For example:

# print(8 + 2)
print(10 - 9)

The print(8 + 2) line is ignored because it comes after #.

Comments are useful for explaining tricky parts of the code or noting the purpose of a section. Try removing the # symbols in the following lines to make them run:

# print(30 + 10)
# print(30)
# print(-30)

After uncommenting them, run your code and see what happens.


Working with Arithmetic Operations

So far, we have used addition and subtraction. Python can also handle multiplication, division, and exponentiation.

  • Multiplication uses *:

    print(5 * 4)

    Output: 20

  • Division uses /:

    print(12 / 4)

    Output: 3.0

  • Exponentiation uses **:

    print(3 ** 2)

    Output: 9 (because 3 squared is 9).

Python follows standard math rules: parentheses first, then exponents, then multiplication and division, and finally addition and subtraction.

For example:

print(6 + 2 * 10)
print((6 + 2) * 10)

Run the code in VS Code
Running the code in VS Code
The first line results in 26 because multiplication happens before addition. The second gives 80 because (6 + 2) is calculated first.

These basic operations are fundamental for data analytics. They let you clean, transform, and summarize data, making it easier to analyze patterns and trends.

Try writing three lines of code using print():

  • One line for multiplication (e.g., 12 * 7)
  • One line for division (e.g., 40 / 8)
  • One line for exponentiation (e.g., 2 ** 5)

Run your code and check the results.


Looking Ahead

In this first lesson, you learned how to run basic Python instructions, display outputs, and use simple arithmetic operations. You also learned about the importance of following syntax rules and how comments can help explain code.

As you continue, you will build on these basics:

  • Learn how to store data in variables for easier access.
  • Work with numbers and text.
  • Load larger datasets into Python and explore them.
  • Apply conditional logic to answer questions and guide your analysis.

With these skills, you will be better equipped to handle data and draw meaningful insights, making your journey in data analytics smoother and more productive.