Python has the capability to perform calculations involving addition, subtraction, multiplication, and division. These basic arithmetic operations are some of the simplest types of commands that Python is able to process.
# Addition
3 + 6
# Multiplication
6 * 7
# Division
5 / 2
# Division
7 / 3
Do you notice anything unusual about the output of the next line?
8 / 2
Attempting to divide by zero will result in an error.
42 / 0
Python uses the symbol ** to indicate the operation of exponentiation.
# Exponentiation
3 ** 4
Python recognizes the standard order of operations.
4 + 5 * 6
Parentheses can be used to form more complex arithmetic expressions.
# Parentheses and Order of operations
5 * (3 + 8 / 2)
Variables are an important aspect of any programming language. A variable can be thought of as a container or storage location for a piece of information. Every variable has two features:
We define variables using the assignment operator "=".
# Define variables x and y
x = 4
y = 9
We can call the variable x
to check its value.
x
Now let's check the value of y
.
y
As we have seen, it is possible to have multiple lines of code within a single Jupyter cell. However, if we have multiple lines that all generate some sort of output, only the last of these lines will have its output displayed.
x
y
To display the value of more than one variable in a single Jupyter notebook cell, we need to use the print()
function.
print(x)
print(y)
We can perform arithmetic operations using variables that are storing numbers.
print(x + y) # Sum of x and y
print(x * y) # Product of x and y
Notice using variables in arithmetic operations does not affect the values of those variables.
print(x)
print(y)
print(2 * x)
print(x - y)
print(x)
print(y)
Calculate the product of x
and y
, and store the result in a new variable, z
.
z = x * y
Print the value of z
.
z
In general, variables are not static objects. While the name of a variable is fixed, the contents of that variable can be changed or overwritten. We illustrate this idea in the next two code cells.
In the next cell, we:
a = 37
b = a
print(a)
print(b)
We now change the value of b
to 18 and then print a
and b
again.
b = 18
print(a)
print(b)
Notice that the value of b
changed, but the value of a
was unaffected. Although b
was set equal to a
when it was created, it is a separate variable.
In the example above, we overwrote the value of the variable b
with a new value that was completely unrelated to its original value. It is also possible to reassign the value of a variable in such a way that its new value depends upon its previous value.
# Define a variable s with initial value of 7
s = 7
print(s)
# Double the value of s, storing the result back into s.
s = s * 2
print(s)
# Increment the value of s by 3.
s = s + 3
print(s)
# Square the value of s.
s = s ** 2
print(s)
The process of incrementing, or increasing, a variable by a certain amount is one with many practical applications in computer science and data science. Python, like many other programming languages, comes equipped with a shorthand means of performing this task.
The operator "+=" is used to perform variable incrementation, as shown below.
# Define i to have an initial value of 5.
i = 5
print(i)
# Increment i by 1.
i += 1
print(i)
# Increment i by 3.
i += 3
print(i)
Python variable names can include numbers, letters, and underscores, but cannot contain any other characters or spaces. Variable names must always start with a letter.
# These are valid variable names.
var1 = 3.14159
this_is_a_valid_name = 2.71828
t0tally_val1d = 1.414
# Variable must start with a letter.
1var = 3.14159
# Variable names cannot contain spaces.
bad name = 2.71828
# Underscores are the only non-alphanumeric characters allowed in a name.
thi$_is_inv@lid = 1.414
Python variable names are case sensitive. Variable names that use different cominations of uppercase and lowercase letters, but are otherwise identical will refer to different variables.
# var, Var, and vAr all refer to different variables.
var = 12
Var = 45
vAr = 139
print(var)
print(Var)
print(vAr)
# The variable VAR is currently undefined.
print(VAR)
Every variable in Python has a "type". The type of a variable determines the ways in which we can use the variable.
Python has four numerical variable types: int
, long
, float
, and complex
. For now, we will focus on int
and float
.
int
variable is used to represent an integer. float
variable is used to represent a decimal number. We will discuss the importance of variable types in more depth later. For now, we will show how to determine a variable's type using the type()
function.
x = 7
type(x)
y = 3.7
type(y)
We can coerce an integer value to be stored as a float
by adding ".0" to the end of the number.
z = 14.0
type(z)
The sum of an int and a float is always stored as a float.
print(x)
print(y)
u = x + y
print(u)
print(type(u))
print(x)
print(z)
v = x + z
print(v)
print(type(v))
The ratio of any combination of ints and floats is always stored as a float.
a = 7
b = 5
q = a / b
print(q)
print(type(q))
c = 20
d = 4
p = c / d
print(p)
print(type(p))
It is possible to change the type of a variable in Python. This is not allowed in many other programming languages.
p = 4
print(type(p))
p = 3.999
print(type(p))
q = 7
print(type(q))
q = 7 / 1
print(type(q))