Skip to article frontmatterSkip to article content

Introduction to Python

Welcome to the world of Python! Python is a versatile, high-level programming language known for its readability and simple syntax. This makes it an excellent choice for beginners and a powerful tool for experts. In this chapter, we’ll cover some of the absolute basics to get you started on your programming journey.

Python Logo Cover Image

🐣 What is Python?

Python was created by Guido van Rossum and first released in 1991. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than might be used in languages like C++ or Java.

Python is an interpreted language, which means that you can run each line of code as you write it, making it great for learning and experimenting. It’s used in a wide range of applications, including:

The Zen of Python

The Zen of Python is a collection of guiding principles for writing computer programs in Python. You can access it by typing import this in a Python interpreter. Here are a few key principles:

>>> import this

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Readability counts.
...

🌟 Core Features of Python

📜 A Brief History of Python

🌍 Python Ecosystem

Python’s power comes from its huge ecosystem of libraries and frameworks:


⚖️ Python Compared to Other Languages

🖥️ Python vs. C

☕ Python vs. Java

📊 Python vs. R

🌐 Python vs. JavaScript


💪 Strengths of Python

⚠️ Limitations of Python


🎯 Why Learn Python?


⚙️ How Python Executes Your Code

To understand how the Python interpreter executes your code, we first need to understand expressions in the context of programming.

An expression is a syntatic entity that may be evaluated to determine its value (source). Here is a simple expression.

1+21 + 2

The mathematical expression above evaluates to the value 3.

Expressions are not always as simple as having two operands and a single operator. Sometimes, you may have an expression that contains multiple sub-expressions. Look at this mathematical expression.

(1+2)×(4÷2)(1 + 2) \times (4 \div 2)

We are trained to perform the arithmetic operations in the predefined order of operations (PEMDAS). Here is a step-by-step breakdown of how a human would carry out this evaluation process.

  1. Evaluate (1+2)(1 + 2) to 3,
  2. Evaluate (4÷2)(4 \div 2) to 2,
  3. Rewrite the equation to 3×23 \times 2,
  4. Evaluate 3×23 \times 2 to 6

Python executes your program in a similar fashion. It evaluates your expressions from left to right (except when you’re performing an assignment operation, which we will discuss later). The operator precedence also emulates the mathematical order of operations, except that there are some operators that only exist in Python.

For a summary of the operator precedence in Python, refer to the Operator Precedence section of https://docs.python.org/3/reference/expressions.html.


💬 Python Comments

Python comments are lines in your code that are not executed. They are used to explain and clarify the code for anyone reading it (including your future self). In Python, comments start with the # symbol.

# This is a single-line comment
print("Hello, World!")
print("Hello, World!")  # This is another comment
# This is a multi-line comment
# It spans multiple lines
print("Hello, World!")

⬜ Whitespace and Indentation

Python uses whitespace (spaces and tabs) to define the structure of the code. Indentation is crucial in Python, as it indicates blocks of code that belong together, such as the body of a function or a loop. We will cover functions and loops in later chapters, but here is a simple example to illustrate the concept of indentation.

def greet(name):
    print(f"Hello, {name}!")  # Indented block

greet("Kingfisher") # Output: Hello, Kingfisher!

Incorrect indentation will lead to an IndentationError.

def greet(name):
print(f"Hello, {name}!")  # This will raise an IndentationError

greet("Kingfisher")

Ignored Whitespaces in Python

In Python, whitespaces around operators are ignored (though spacing is encouraged for readability). This means that the following lines of code are equivalent:

x = 5 + 3
x=5+3
x = 5 +3
x = 5 + 3  # Good practice
x=5+3      # Less readable
x = 5 +3   # Less readable

Extra whitespaces inside data structures (like dictionaries, lists, or tuples) are also ignored by the interpreter. This means you can add spaces or line breaks to improve readability without changing how the code runs.

For example, all of the following dictionary definitions are equivalent:

my_dict = { "key1": "value1", "key2": "value2" }
my_dict = {"key1":        "value1",  "key2":"value2"}
my_dict = {
    "key1": "value1",
    "key2": "value2"
}

Python doesn’t care about the spacing between keys, colons, and values — the meaning stays the same.

For best practices on code style, refer to the PEP 8 - Style Guide for Python Code. PEP 8 is the de facto code style guide for Python, and it is widely adopted by the Python community.