Variables

Now that we have the ability to do math, we need a place to store the results.

Immediately printing is convenient for demonstration, but not very useful for programs.

A variable is a programming construct that gives programmers a place to store data between calculations. Variables can be used in the same places literals can within expressions, but also be assigned to using the = operator.

For example:

# uci_bootcamp_2021/examples/basic_variables.py

# Assigning literals to variables.

length = 3
width = 2

# Using variables in expressions, and assigning the result to another variable.
area = length * width

# Using variables in function calls.
print(area)
# 6

Warning: in python= is used as the assignment operator, while== is used as the equality relational operator. It is a common beginner error mistaking these two operators!

# uci_bootcamp_2021/examples/mistaking_equality_for_assigment.py

# NOTE: Do not mistake the equality operator `==` for the assignment operator `=`

x = 42
y = 42
# Demonstrate comparing two variables by equality
if x == y:
    print("x and y are equivalent!")
else:
    print("x and y are not equivalent!")
z = 17

# NOTE: The following line is erroneous; and is included for demonstration purposes ONLY.
# NOTE: Most linters will throw a warning / error here!
if x = z:
    print("x and y are equivalent!")
else:
    print("x and y are not equivalent!")

Variables can hold any value, and be of any type. In fact, a variable may be re-assigned after it is declared, even if the types don't match!

# uci_bootcamp_2021/examples/basic_variables.py

# Demonstrating that variables can be re-assigned to different types.
foo = 42
print(type(foo))
# <class 'int'>
foo = "I like pizza."
print(type(foo))
# <class 'str'>

This can be considered one of Python's strengths: Python is a Dynamically Typed language. One of the major benefits of being dynamically typed is not having to give too much thought to the types a program requires, and how to obtain them programmatically.

In the context of Data Science, this is a massive boost as it greatly reduces the cognative load on the programmer. Allowing the focus on more important things such as ensuring their maths are correct, and manipulating the data into the format required to satisfy the project's constraints.

Dynamic Typing's strength will become more apparent once we start processing data in the second half of this presentation