Understanding Python: How It Works

This article is a complementary resource to the Learn Python Basics course.

 

Understanding Python: How It Works

Python is a programming language used in machine learning (ML), data science, web applications, and software development.

Because Python is used in many industries, it is a great choice for building a strong foundation for a successful career in technology.

Python is also a good choice for beginners because it is easier to learn than many other programming languages.

Consider this code to add two numbers in Python and C++ programming.

Comparison between Python and CPP code
Comparison between Python and CPP code

As you can see, the Python code is easier to understand and read, while the C++ code is more complicated and needs extra steps.


How does Python work?

Much like English or German are languages for communicating with people, think of programming as a language for communicating with computers.

In English, we use words and grammar in certain ways to convey meaning. Similarly, Python uses specific syntax and keywords to instruct a computer on what actions to perform.

Once you understand the basic keywords and how to use them, you can write instructions that enable a computer to solve different problems.

As you might have noticed, the code in Python and C++ is different. Python is simpler and more straightforward, while C++ requires more detailed instructions for the same task.

So, why do Python and C++ require different code to perform the same task (like adding two numbers)?

Computers do not directly understand the code you write.

When you write and run code, Python uses an interpreter to convert your code into machine code that the computer can understand.

In the case of C++, a compiler is used to convert the code into machine code that the computer understands.

These tools (the Python interpreter and the C++ compiler) are designed differently, so the syntax for writing code in these languages varies.

Although the syntax of programming languages is different, the basic concepts are the same. So once you learn one language, it becomes easier to learn another.


Python Interpreter

As mentioned, the Python interpreter translates your Python code into instructions that your computer can understand.

The interpreter works line by line, meaning:

  1. The Python interpreter reads the first line of code.
  2. It translates the code into machine-readable instructions that your computer can execute.
  3. The interpreter then runs the instructions.
  4. It moves on to the next line and repeats the process.

Here's a visual representation of how the Python interpreter works:

Working of Python Interpreter
Working of Python Interpreter