How Python Executes Your Code
ExpressionsTo 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.
The mathematical expression above evaluates to the value .
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.
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.
- Evaluate to ,
- Evaluate to ,
- Rewrite the equation to ,
- Evaluate to
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.
Loading
Loading
Python Data Types
What is it?A data type denotes the category of a value. In Python, ALL values have data types. We'll go over a few built-in types. Built-in types are pre-defined data types that are part of the Python programming language itself.
- Text
"Hello World"
is a text type (str
). - Number
475
is an integer type (int
). - Number
1.99
is a float type (float
). - Logical
True
is a boolean type (bool
).
Data types can be categorized into two types - primitive and non-primitive types. Technically speaking, Python only has non-primitive types as everything is an object in Python. For the purpose of this course, you do not have to worry about differentiating between primitive and non-primitive types.
Python Basic Data Types
Strings (str
) are text types. They are always enclosed in single or double quotes.
Integers (int
) and decimals (float
) are numeric types. There is also another numeric type (complex
) that is rarely used.
Booleans (bool
) can only have two possible values - True
or False
. Note that "True"
is a string type since it's enclosed in double quotes. Boolean values should not be enclosed in quotes.
Loading
Loading
Loading
Checking Data Type of a Value
You can use the type()
function to check the data type of a value programmatically.
type(some_value)
Combine print()
and type()
to print out the data type.
# Syntax
print(type(some_value))
# Examples
print(type(1)) # <class 'int'>
print(type(True)) # <class 'bool'>
print(type("A")) # <class 'str'>
Loading
Loading
Loading
Variables
From WikipediaA variable is a storage location (identified by a memory address) paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value.
A variable is a nickname for a stored value that can change.
Why do we use variables?If you have a constantly-changing value that is used in many different places of a computer program, you will likely run into a maintenance issue as you have to manually update those values every time the value changes. If you create a variable and reference that variable instead, you only have to update that variable instead of having to update every occurrence.
Variables also increase the readability if properly used. Assume that you are calculating the after-tax price (at a tax rate of 10%) of an item that is 2 dollars. In Python code, the calculation of the after-tax price can be written as 2 * 1.10
. Although you may understand what the numbers in 2 * 1.10
mean, but other people may have a difficult time understanding it. Rewriting it to before_tax_price * (1 + tax_rate)
improves the readability of your code.
Working with Variables
Creating a VariableTo create a new variable, use the assignment operator (=
).
my_variable = some_value
It is important to note that the =
symbol here is an assignment operator and NOT a symbol for equality.
Loading
Loading
Loading
Loading
Python Variable Naming Rules
Variable names must begin with a letter or an underscore.
You can use letters, numbers, and underscores for the remainder of your variable name.
You can use letters, numbers, and underscores for the remainder of your variable name.
Loading
Loading
Working with Variables, continued
Updating a VariableThe syntax for updating a variable is identical to that of creating a new variable.
# create a new variable
my_city = "Champaign"
# update a variable
my_city = "Urbana"