# Creating a variable
my_number = 16Variables
This lesson explains how variables work in Python, including core data types, naming rules, assignment, and how to store and print values for use in larger analyses.
Python variables, Python data types, Variable naming
Approximate time: 30 minutes
Learning Objectives
In this lesson, we will:
- Assign values to variables
- Describe the different data types available
Overview of lesson
Variables are one of the most fundamental units in many programming languages. Variables can be thought of as a label for an object. You can then retrieve this object for later use. Some helpful uses for variables are if:
- The output of something processed needs to be stored for later use
- A value is used multiple times in an analysis
- Your code is being read by others
In many cases, you will be creating variables in many different programming languages for all three of these reasons. Let’s go ahead and dive into how to create variable within Python.
Assigning Variables
In Python, in order to assign a value to an object we need to use =. For example, you might have a number or word that you think is important that you’d like to store to use at a later time like this:
When creating variables in Python, there are some rules and guidelines that you should follow:
Rules
- Python variables are case sensitive, so
my_numberandMy_numberare different variables - Variables names must only consist of letters, numbers and underscores
- Must begin with a letter or an underscore, it cannot start with a number. Starting a variable with an underscore has some special properties that are beyond the scope of this workshop
- It cannot be a reserved keyword like
fororif
Guidelines
- Make variable names meaningful. Technically, there is nothing wrong with a variable named
s5ro_l67, but that is likely not very descriptive to yourself and others. - Use camel case (
MyVariableName) or underscores (my_variable_name) to denote multi-word variable names - Be consistent in your style
There is an official Python style guide that has much more detailed guidelines.
If we would like to see the the value of a variable we have created, we can use the the print() function:
# Print my_number
print(my_number)16
You can add text around a variable within the print() function:
# Added text within the print function
print('I want to remember', my_number, 'for later use.')I want to remember 16 for later use.
print() function
You can see that when we add text around a variable, it automatically adds spaces on either side of that variable inside the print() function.
Let’s go ahead and create a second number that we are interested in:
# Creating a second variable called your_number
your_number = 8.5Printing variables
We can use the print() function to see the values of these variables:
# Multiple variable within a print function
print('I want to remember', my_number, 'and', your_number)I want to remember 16 and 8.5
You can carry out basic arithmetic using variables. For example, if we wanted to add my_number and your_number, we could do that with:
# Add our numbers
print(my_number + your_number)24.5
Additionally, we can assign the output of that summation to a new variable:
# Add our numbers
our_sum = my_number + your_number
# Print the sum of our numbers
print(our_sum)24.5
Python uses white-space to define scope, but when assigning values to a variable, Python has no set rules how to do this. The assignments below are all treated the same within Python:
# No whitespace on either side of =
x=3
# Print the value of x
print(x)3
# One space on either side of =
x = 3
# Print the value of x
print(x)3
# Three spaces on either side of =
x = 3
# Print the value of x
print(x)3
Re-assigning variable
You can also assign a new value to a variable. Let’s update my_number to be a new number:
print(my_number)
my_number = 22
print(my_number)16
22
# Print the sum of our numbers
print(our_sum)
# Add our numbers
our_sum = my_number + your_number
# Print the sum of our numbers
print(our_sum)24.5
30.5
- Create two new variables called
a, that has the value of 5, andb, that has the value of 3. - Subtract
bfromaand assign it to the new variable calleddifference - Using the variables
a,banddifference, print out the sentence that looks like:
The difference of 5 and 3 is 2
Types of Variables
Now that we have a basic understanding of what a variable is, let’s discuss the different data types can hold a single value. Some of these most common data types are:
| Type | Description | Example |
|---|---|---|
| int | Integers (whole numbers) | 7, -3, 42 |
| float | Floating point numbers (numbers with decimals) | 3.14, 0.5 |
| bool | Booleans (True/False values) | True, False |
| str | Strings (text) | “hello”, “3” |
We can determine what type of data type a variable is by using the type() function.
# Data type of my_number
print(type(my_number))<class 'int'>
When we have functions nested inside of one another, the innermost function is evaluated first and the output from it is passed to the next outer function until you reach the outermost function. In the example above, my_number is evaluated by the type() function and the output of the type() function is then passed onto the print() function.
Integers and Floats
Integers are whole numbers and floats are numbers with decimals. Both of these data types can be used in mathematical operations together. For example, we can add together an integer and a float:
# Add integer and float together
5 + 3.148.14
Strings
Strings are a data type that can hold a sequence of characters. Strings are denoted by wrapping the string in either single- or double-quotes and either is acceptable.
# Create a string variable
my_word = 'sequence'
print(type(my_word))<class 'str'>
In order to demonstrate that Python is indifferent between single- and double-quotes, we can also create a string variable with double-quotes and we can see that Python still returns that this variable is of the string data type:
# Create a second string variable
my_other_word = "chromosome"
# Print the data type for a double-quoted string
print(type(my_other_word))<class 'str'>
If you were to place a variable name within quotes, it will be evaluated as a string rather than as a variable. For example:
# Printing a variable name within quotes
print('my_number is a string, but', my_number, 'is a variable.')my_number is a string, but 22 is a variable.
Furthermore, if you were to place a numeric value within quotes, then Python will interpret that number as a string rather than as a number.
# Number in quotes is a string
number_string = '12'
# Print the data type of number_string
print(type(number_string))<class 'str'>
And if you were to attempt to add together a string and a numeric value, integer or floating point, Python will return an error message letting you know that adding together numbers and strings is not permitted. This is even true if the string is a number like number_string:
# Trying to add strings and numbers
my_number + number_string--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[21], line 2 1 # Trying to add strings and numbers ----> 2 my_number + number_string TypeError: unsupported operand type(s) for +: 'int' and 'str'
Booleans
Booleans are another data type and the denotes True/False values.
# Create a boolean variable
my_boolean = True
print(type(my_boolean))<class 'bool'>
Changing the Data Type
Let’s consider the case where you have perhaps classified the data type of variable incorrectly. In our case, number_string is a string with the number 16. We can see that:
# Data type of number_string
print(type(number_string))<class 'str'>
But we can tell Python to interpret this string as a number if we would like to, by using either the int() or float() functions.
# Change the data type of number_string to integer
print(type(int(number_string)))<class 'int'>
Sometimes, 0 and 1 denote False and True, respectively, in the world of computers, but if we evaluate the type for 0 or 1, we will know that by themselves they are integers:
# Retrieve the data type of 1
print(type(1))<class 'int'>
Thus, if we wanted to have 0 or 1 evaluated as booleans rather than integers, then we can use the bool() function.
# Retrieve the data type for 1 as a boolean
print(type(bool(1)))<class 'bool'>
Interestingly, because booleans are 0 and 1 under the hood, they can be added together and the end result is a integer data type.
# Adding together two Trues and a False
True + True + False
# Data type for adding together two Trues and a False
print(type(True + True + False))<class 'int'>
- How would you determine the data type on
your_numberand what data type is it?
Variable Inspector
If we right click on some empty space within the Jupyter Lab interface, we can open up the Variable Inspector. This is a helpful tool to see all of the variables that you have created and their data types.
Once we open up the Variable Inspector, a new tab will open in our notebook. From this tab, we can see all of the variables that we have created, their data types and their values. This is a useful resource to quickly check what variables you have created and what they contain, especially if you have created many variables and are not sure what they all are.