Introduction to Python¶

In this notebook, we will cover some concepts in Python, including:

  1. Print statements
  2. Data types
  3. Expressions in Python
  4. Variable assignment

1. Print Statements¶

The print() function is used to display output in Python. It can be used to show the value of variables, display messages, or both.

You can simply pass a variable or text to print() to display it.

In [ ]:
# Here is an example of using print directly on a string
print("Look! I can print things this way")

# I've used print() to print the values of variables already in this notebook but here is another example.
message = "Or, I can print this way"
print(message)

2. Data Types¶

Each value has a data type. The data type defines what kind of information the value holds and what operations can be performed on it.

Here are the most common data types in Python:

  • Integer (int): Whole numbers, such as 10 or -42.
  • Float (float): Decimal numbers, such as 3.14 or -0.5.
  • String (str): Text, such as "Hello" or "My favorite movie is Fantastic Mr. Fox.".

3. Expressions in Python¶

Here are some examples of how math operations are written in Python.

In [ ]:
# Addition
2+2
Out[ ]:
4
In [ ]:
# Subtraction
3-2
Out[ ]:
1
In [ ]:
# Multiplication
2*2
Out[ ]:
4
In [ ]:
# Exponentiation
2**3
Out[ ]:
8
In [ ]:
# Division
5/2
Out[ ]:
2.5

Variables can be used in math expressions¶

You can use variables to hold values and then use them in place of numbers.

In [14]:
x = 2
y = 3
z = x * y
print(z)
6

Python follows order of operations similar to PEMDAS¶

Note: You can always use parentheses to control or change the order of evaluation.

  1. Parentheses: () — Expressions inside parentheses are evaluated first.
  2. Exponentiation: ** — Exponentiation is evaluated next.
  3. Multiplication and Division— These are evaluated from left to right.
  4. Addition and Subtraction— These are evaluated from left to right.
In [ ]:
# Steps for evaluation listed below
2 + 3 * 4 ** 2 / (1 - 5)
Out[ ]:
-10.0

Steps:

  1. Parentheses: (1 - 5) becomes -4.
  2. Exponentiation: 4 ** 2 becomes 16.
  3. Multiplication and Division (from left to right):
    • First, 3 * 16 = 48.
    • Then, 48 / -4 = -12.
  4. Addition: 2 + (-12) becomes -10. Thus, the final result is -10.

Use parentheses in practice¶

In practice, it is much easier to understand math expressions when you use parentheses becuase you don't have to think about the other rules in order of operations.

In [ ]:
# Same expression as above but with parentheses to show order of evaluation.
2 + ((3 * (4 ** 2)) / (1 - 5))
Out[ ]:
-10.0

4. Variable Assignment¶

A variable in Python is simply a way to store data so you can reference and/or change it later. You can think of a variable as a labeled box that holds a value.

Variable rules:

  • You can assign a variable to a value using the equals sign =. Ex: variable = value.
  • Variable names can't start with a number (for example the variable name 5head won't work, but fivehead will).
  • Variable names also can't have spaces in them (five head won't work, but five_head will).
In [ ]:
# Here I assign a variable I chose to name "x" to hold the integer value 10

x = 10

# I can see the value assigned to x by printing it with the print() function
print(x)
In [ ]:
# Also, a useful tip for jupyter notebooks is that the last variable/value/command you put in a cell will print to output without having to use print().
x

Python runs commands sequentially¶

Commands are run from top to bottom.

In the cell below, the first line my_var = 1 is evaluated first.
Then its value is printed with print(my_var).

This same process is then repeated when I assign my_var = 2 and then print its value again with print(my_var).

In [ ]:
my_var = 1
print(my_var) # This prints 1


my_var = 2
print(my_var) # This prints 2

Variable updating¶

Variables can be overwritten and variables can also reference themselves.

In [ ]:
# Assigning my_var to be 10
my_var = 10
print(my_var)

# Overwriting my_var to be its current value plus 1
my_var = my_var + 1
print(my_var)