Skip to article frontmatterSkip to article content

Chapter 2 - Python Syntax

Python is the most popular general-purpose programming language. According to a StackOverflow 2024 survey, only Javascript and HTML/CSS are more popular than Python, and Python is the most popular language for those learning to code.

2.0.1 - Programming language structure

In typical four-year Computer Science programs, there are multiple courses dedicated to the critical analysis of programming language design. Each language has advantages and disadvantages that are obvious and not so obvious. Sometimes the choice of language is made for you by your job. However, if you can pick the language, what you choose in 2025 will very likely not be what you choose in 2045. Thus, it is good to know the general structure of programming languages so you can jump between languages without trouble. Because of this, Computer Science programs typically teach courses in C++, Java, or other high-level programming languages due to their popularity in many fields as well as their general object oriented programing paradigm. Depending on the interests of the faculty, you might examine COBOL, perl, Ada, C#, SQL, Fortran, and even assembly.

2.0.2 - Python vs. C++ comparison

When comparing and choosing programming languages, you need to consider many things, including:

1. Hardware requirements - Is the program running on an embedded device with limited RAM? Does it need to run on old laptops?

❌ If response time is crucial and memory is limited, C++ or similar languages may be a better choice.

✅ Python can take advantage of the speed of C++ when it serves as a wrapper for an existing C++ library.

2. Project requirements - Are there budget limitations? How big is the program/team? What risks are involved?

❌ Python uses duck typing, which can make development, testing, and maintenance more difficult compared to C++ or similar languages.

✅ Python has been moving towards better type handling using approaches like type hints.

3. Ease of use - what is the experience level of your team? How quickly do you need to have a proof of concept?

✅ Python generally requires fewer lines of code to do the same thing as more complex languages. Consider the “Hello World!” example

Python

print("Hello World!")

C++

#include <iostream>

int main() {
  std::cout << "Hello, World!" << std::endl;
  return 0;
}

2.0.3 - Python Basics

The computer will process the Python code you write starting from the top of the notebook and downward.

In the following example, you will print out “First” and “Second” using print. Python will process the first line, which tells the computer to print out “First”. Next, it will process the second line, which tells the computer to print out “Second”. New text will appear below the code block which signifies the code has been successfully processed (also referred to as executed). Try making small changes to the code below to see what happens.

print("First")
print("Second")
First
Second

Defining variables in Python is very similar to setting variables in an algebra problem.

However, there are some rules that will help you to understand and properly use variables.

2.0.4 - Variable rules

1. When a variable is defined, the variable will represent whatever value it has been assigned until:

a. You restart, shut down, or log out of the notebook (or your IDE)

b. You redefine the variable (either implicitly or explicitly)

2. When a variable is defined, everything to the right of an equal sign (=) is evaluated first.

a. A variable can be assigned to the result of a math operation that includes itself. The value does not change until the right side of the equation is evaluated.

Examples of these rules will be explored in more detail in the next section.

2.0.5 - Summary of Chapter 2

While not all of this will make sense at first, the best way to learn Python is to practice. The following sections will go into great detail about defining variables and the general types of values (data types) that can be used while defining variables.