Variables and Data Types

Objectives ⟶

  • Learn expressions and evaluations

  • Understand what a data type is

  • Discuss basic data types

  • Understand what a variable is

  • Discuss why variables are useful

How Python Executes Your Code

Expressions

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 33.

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 33,
  2. Evaluate (4÷2)(4 \div 2) to 22,
  3. Rewrite the equation to 3×23 \times 2,
  4. Evaluate 3×23 \times 2 to 66

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.

Multiple Choice Question

Loading

Question
Loading
Loading Options
Loading
Multiple Choice Question

Loading

Question
Loading
Loading Options
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).
Primitive vs Non-primitive

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

Text

Strings (str) are text types. They are always enclosed in single or double quotes.

Numbers

Integers (int) and decimals (float) are numeric types. There is also another numeric type (complex) that is rarely used.

Logical Yes/No

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.

Multiple Choice Question

Loading

Question
Loading
Loading Options
Loading
Multiple Choice Question

Loading

Question
Loading
Loading Options
Loading
Multiple Choice Question

Loading

Question
Loading
Loading Options
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'>
Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Variables

From Wikipedia

A 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.

What it really means

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 Variable

To 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.

Multiple Choice Question

Loading

Question
Loading
Loading Options
Loading
Python Challenge

Loading

Multiple Choice Question

Loading

Question
Loading
Loading Options
Loading
Python Challenge

Loading

Python Variable Naming Rules

First Character

Variable names must begin with a letter or an underscore.

Allowed Characters

You can use letters, numbers, and underscores for the remainder of your variable name.

Case Matters

You can use letters, numbers, and underscores for the remainder of your variable name.

Python Challenge

Loading

Python Challenge

Loading

Working with Variables, continued

Updating a Variable

The 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"
Python Challenge

Loading