Variables and Types
Last updated on 2024-09-03 | Edit this page
Estimated time: 25 minutes
Overview
Questions
- How can I store data in Python?
- What are some types of data that I can work with in Python?
Objectives
- Write Python to assign values to variables.
- Print outputs to a Jupyter notebook.
- Use indexing to manipulate string elements.
- View and convert the data types of Python objects.
Use variables to store values.
Variables are names given to certain values. In Python the
=
symbol assigns a value to a variable. Here, Python
assigns the number 42
to the variable age
and
the name Ahmed
in single quote to a variable
name
.
Naming variables
Variable names:
- cannot start with a digit
- cannot contain spaces, quotation marks, or other punctuation
- may contain an underscore (typically used to separate words in long variable names)
- are case sensitive.
name
andName
would be different variables.
Use print()
to display values.
You can print Python objects to the Jupyter notebook output using the
built-in function, print()
. Inside of the parentheses we
can add the objects that we want print, which are known as the
print()
function’s arguments.
OUTPUT
Ahmed 42
In Jupyter notebooks, you can leave out the print()
function for objects – such as variables – that are on the last line of
a cell. If the final line of Jupyter cell includes the name of a
variable, its value will display in the notebook when you run the
cell.
OUTPUT
42
Format output with f-strings
F-strings provide a concise and readable way to format strings by
embedding Python expressions within them. You can format variables as
text strings in your output using an f-string. To do so, start a string
with f
before the open single (or double) quote. Then add
any replacement fields, such as variable names, between curly braces
{}
. (Note the f string syntax can only be used with Python
3.6 or higher.)
OUTPUT
'Ahmed is 42 years old'
Variables must be created before they are used.
If a variable doesn’t exist yet, or if the name has been misspelled,
Python reports an error called a NameError
.
ERROR
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-c1fbb4e96102> in <module>()
----> 1 print(eye_color)
NameError: name 'eye_color' is not defined
The last line of an error message is usually the most informative. In
this case it tells us that the eye_color
variable is not
defined. NameErrors often refer to variables that haven’t been created
or assigned yet.
Variables can be used in calculations.
We can use variables in calculations as if they were values. We
assigned 42 to age
a few lines ago, so we can reference
that value within a new variable assignment.
OUTPUT
Age equals: 45
Every Python object has a type.
Everything in Python is some type of object and every Python object will be of a specific type. Understanding an object’s type will help you know what you can and can’t do with that object.
You can use the built-in Python function type()
to find
out an object’s type.
OUTPUT
<class 'float'> <class 'int'> <class 'str'> <class 'builtin_function_or_method'>
- 140.2 is an example of a floating point number or
float
. These are fractional numbers. - The value of the
age
variable is 45, which is a whole number, or integer (int
). - The
name
variable refers to the string (str
) of ‘Ahmed’. - The built-in Python function
print()
is also an object with a type, in this case it’s abuiltin_function_or_method
. Built-in functions refer to those that are included in the core Python library.
Types control what operations (or methods) can be performed on objects.
An object’s type determines what the program can do with it.
OUTPUT
2
We get an error if we try to subtract a letter from a string:
ERROR
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-67f5626a1e07> in <module>()
----> 1 'hello' - 'h'
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Use an index to get a single character from a string.
We can reference the specific location of a character (individual letters, numbers, and so on) in a string by using its index position. In Python, each character in a string (first, second, etc.) is given a number, which is called an index. Indexes begin from 0 rather than 1. We can use an index in square brackets to refer to the character at that position.
OUTPUT
A
Use a slice to get multiple characters from a string.
A slice is a part of a string that we can reference using
[start:stop]
, where start
is the index of the
first character we want and stop
is the last character.
Referencing a string slice does not change the contents of the original
string. Instead, the slice returns a copy of the part of the original
string we want.
OUTPUT
Ale
Note that in the example above, library[0:3]
begins with
zero, which refers to the first element in the string, and ends with a
3. When working with slices the end point is interpreted as going up to,
but not including the index number provided. In other words,
the character in the index position of 3 in the string
Alexandria
is x
, so the slice
[0:3]
will go up to but not include that character, and
therefore give us Ale
.
Use the built-in function len
to find the length of a
string.
The len()
function will tell us the length of an item. In
the case of a string, it will tell us how many characters are in the
string.
OUTPUT
5
Variables only change value when something is assigned to them.
Once a Python variable is assigned it will not change value unless
the code is run again. The value of older_age
below does
not get updated when we change the value of age
to
50
, for example:
OUTPUT
Older age is 45 and age is 50
A variable in Python is analogous to a sticky note with a name
written on it: assigning a value to a variable is like putting a sticky
note on a particular value. When we assigned the variable
older_age
, it was like we put a sticky note with the name
older_age
on the value of 45
. Remember,
45
was the result of age + 3
because
age
at that point in the code was equal to 42
.
The older_age
sticky note (variable) was never attached to
(assigned to) another value, so it doesn’t change when the
age
variable is updated to be 50
.
F-string Syntax
Use an f-string to construct output in Python by filling in the blanks with variables and f-string syntax to tell Christina how old she will be in 10 years.
Tip: You can combine variables and mathematical expressions in an f-string in the same way you can in variable assignment. We’ll see more examples of dynamic f-string output as we go through the lesson.
swap = x # x = 1.0 y = 3.0 swap = 1.0
x = y # x = 3.0 y = 3.0 swap = 1.0
y = swap # x = 3.0 y = 1.0 swap = 1.0
These three lines exchange the values in x
and
y
using the swap
variable for temporary
storage. This is a fairly common programming idiom.
Can you slice integers?
If you assign a = 123
, what happens if you try to get
the second digit of a
?
Slicing
We know how to slice using an explicit start and end point:
OUTPUT
'library_name[1:3] is: ib'
But we can also use implicit and negative index values when we define
a slice. Try the following (replacing low
and
high
with index positions of your choosing) to figure out
how these different forms of slicing work:
- What does
library_name[low:]
(without a value after the colon) do? - What does
library_name[:high]
(without a value before the colon) do? - What does
library_name[:]
(just a colon) do? - What does
library_name[number:negative-number]
do?
- It will slice the string, starting at the
low
index and stopping at the end of the string. - It will slice the string, starting at the beginning on the string,
and ending an element before the
high
index. - It will print the entire string.
- It will slice the string, starting the
number
index, and ending a distance of the absolute value ofnegative-number
elements from the end of the string.
Fractions
What type of value is 3.4? How can you find out?
Automatic Type Conversion
What type of value is 3.25 + 4?
Key Points
- Use variables to store values.
- Use
print
to display values. - Format output with f-strings.
- Variables persist between cells.
- Variables must be created before they are used.
- Variables can be used in calculations.
- Use an index to get a single character from a string.
- Use a slice to get a portion of a string.
- Use the built-in function
len
to find the length of a string. - Python is case-sensitive.
- Every object has a type.
- Use the built-in function
type
to find the type of an object. - Types control what operations can be done on objects.
- Variables only change value when something is assigned to them.