A Quick Tour of Variables and Data Types in Python
This tutorial is the second in a series on introduction to programming using the Python language. These tutorials take a practical coding-based approach, and the best way to learn the material is to execute the code and experiment with the examples.
Storing information using variables
Computers are useful for two purposes: storing information and performing operations on stored information. While working with a programming language such as Python, informations is stored in variables. You can think of variables are containers for storing data. The data stored within a variable is called it's value. It's really easy to create variables in Python.
my_favorite_color = "black"
my_favorite_color
A variable is created using an assignment statement, which begins with the variable's name, followed by the assignment operator =
(different from the equality comparision operator ==
), followed by the value to be stored within the variable.
You can also values to multiple variables in a single statement by separating the variable names and values with commas.
color1, color2, color3 = "red", "green", "blue"
color1
color2
color3
You can assign the same value to multiple variables by chaining multiple assignment operations within a single statement.
color4 = color5 = color6 = "magenta"
color4
color5
color6
You can change the value stored within a variable simply by assigning a new value to it using another assignment statement. Be careful while reassgining variables: when you assign a new value to the variable, the old value is lost and no longer accessible.
my_favorite_color = "red"
my_favorite_color
While assigning a new value to a variable, you can also use the previous value of the variable to determine the new value.
counter = 10
counter = counter + 1
counter
The pattern var = var op something
(where op
is an arithmetic operator like +
, -
, *
, /
) is very commonly used, so Python provides a shorthand syntax for it.
counter = 10
# Same as `counter = counter + 4`
counter += 4
counter
Variable names can be short (a
, x
, y
etc.) or descriptive ( my_favorite_color
, profit_margin
, the_3_musketeers
etc.). Howerver, you must follow these rules while naming Python variables:
- A variable's name must start with a letter or the underscore character
_
. It cannot start with a number. - A variable name can only contain lowercase or uppercase letters, digits or underscores (
a
-z
,A
-Z
,0
-9
and_
). - Variable names are case-sensitive i.e. a_variable, A_Variable and A_VARIABLE are all different variables.
Here are some valid variable names:
a_variable = 23
is_today_Saturday = False
my_favorite_car = "Delorean"
the_3_musketeers = ["Athos", "Porthos", "Aramis"]
Let's also try creating some variables with invalid names. Python prints a syntax error if your variable's name is invalid.
Syntax:The syntax of a programming language refers to the rules which govern what a valid instruction or statement in the language should look like. If a statement does not follow these rules, Python stops execution and informs you that there is a syntax error. You can think of syntax as the rules of grammar for a programming language.
a variable = 23
is_today_$aturday = False
my-favorite-car = "Delorean"
3_musketeers = ["Athos", "Porthos", "Aramis"]
a_variable
type(a_variable)
is_today_Saturday
type(is_today_Saturday)
my_favorite_car
type(my_favorite_car)
the_3_musketeers
type(the_3_musketeers)
Python has several built-in data types for storing different types of information in variables. Following are at some commonly used data types:
- Integer
- Float
- Boolean
- None
- String
- List
- Tuple
- Dictionary
Integer, float, boolean, None and string are primitive data types because they represent a single value. Other data types like list, tuple and dictionary are often called data structures or containers because they hold multiple pieces of data together.
current_year = 2021
current_year
type(current_year)
Unlike some other programming languages, integers in Python can be arbirarily large (or small). There's no lowest or highest value for integers, and there's just one int
type (as opposed to short
, int
, long
, long long
, unsigned int
etc. in C/C++/Java).
a_large_negative_number = -23374038374832934334234317348343
a_large_negative_number
type(a_large_negative_number)
pi = 3.141592653589793238
pi
type(pi)
Note that a whole number is treated as a float if it is written with a decimal point, even though the decimal portion of the number is zero.
a_number = 3.0
a_number
type(a_number)
another_number = 4.
another_number
type(another_number)
Floating point numbers can also be written using the scientific notation with an "e" to indicate the power of 10.
one_hundredth = 1e-2
one_hundredth
type(one_hundredth)
avogadro_number = 6.02214076e23
avogadro_number
type(avogadro_number)
Floats can be converted into integers and vice versa using the float
and int
functions. The operation of coverting one type of value into another is called casting.
float(current_year)
float(a_large_negative_number)
int(pi)
int(avogadro_number)
While performing arithmetic operations, integers are automatically converted to floats if any of the operands is a float. Also, the division operator /
always returns a float, even if both operands are integers. Use the //
operator if you want the result of division to be an int
.
type(45 * 3.0)
type(45 * 3)
type(10/3)
type(10/2)
type(10//2)
is_today_Sunday = True
is_today_Sunday
type(is_today_Saturday)
Booleans are generally returned as the result of a comparision operation (e.g. ==
, >=
etc.).
cost_of_ice_bag = 1.25
is_ice_bag_expensive = cost_of_ice_bag >= 10
is_ice_bag_expensive
type(is_ice_bag_expensive)
Booleans are automatically converted to int
s when used in arithmetic operations. True
is converted to 1
and False
is converted to 0
.
5 + False
3. + True
Any value in Python can be converted to a Boolean using the bool
function.
Only the following values evaluate to False
(they are often called falsy values):
- The value
False
itself - The integer
0
- The float
0.0
- The empty value
None
- The empty text
""
- The empty list
[]
- The empty tuple
()
- The empty dictionary
{}
- The emtpy set
set()
- The empty range
range(0)
Everything else evaluates to True
(a value that evalutes to True
is often called a truthy value).
bool(False)
bool(0)
bool(0.0)
bool(None)
bool("")
bool([])
bool(())
bool({})
bool(set())
bool(range(0))
bool(True), bool(1), bool(2.0), bool("hello"), bool([1,2]), bool((2,3)), bool(range(10))
nothing = None
type(nothing)
today = "Friday"
today
type(today)
You can use single quotes inside a string written with double quotes, and vice versa.
my_favorite_movie = "One Flew over the Cuckoo's Nest"
my_favorite_movie
my_favorite_pun = 'Thanks for explaining the word "many" to me, it means a lot.'
my_favorite_pun
To use the a double quote within a string written with double quotes, escape the inner quotes by prefixing them with the \
character.
another_pun = "The first time I got a universal remote control, I thought to myself \"This changes everything\"."
another_pun
Strings created using single or double quotes must begin and end on the same line. To create multiline strings, use three single quotes '''
or three double quotes """
to begin and end the string. Line breaks are represented using the newline character \n
.
yet_another_pun = '''Son: "Dad, can you tell me what a solar eclipse is?"
Dad: "No sun."'''
yet_another_pun
Multiline strings are best displayed using the print
function.
print(yet_another_pun)
a_music_pun = """
Two windmills are standing in a field and one asks the other,
"What kind of music do you like?"
The other says,
"I'm a big metal fan."
"""
print(a_music_pun)
You can check the length of a string using the len
function.
len(my_favorite_movie)
Note that special characters like \n
and escaped characters like \"
count as a single character, even though they are written and sometimes printed as 2 characters.
multiline_string = """a
b"""
multiline_string
len(multiline_string)
A string can be converted into a list of characters using list
function.
list(multiline_string)
Strings also support several list operations, which are discussed in the next section. We'll look at a couple of examples here.
You can access individual characters within a string using the []
indexing notation. Note the character indices go from 0
to n-1
, where n
is the length of the string.
today = "Saturday"
today[0]
today[3]
today[7]
You can access a part of a string using by providing a start:end
range instead of a single index in []
.
today[5:8]
You can also check whether a string contains a some text using the in
operator.
'day' in today
'Sun' in today
Two or more strings can be joined or concatenated using the +
operator. Be careful while concatenating strings, sometimes you may need to add a space character " "
between words.
full_name = "Derek O'Brien"
greeting = "Hello"
greeting + full_name
greeting + " " + full_name + "!" # additional space
String in Python have many built-in methods that can be used to manipulate them. Let's try out some common string methods.
Methods:Methods are functions associated with data types, and are accessed using the
.
notatation e.g.variable_name.method()
or"a string".method()
. Methods are a powerful technique for associating common operations with values of specific data types. The.lower()
,.upper()
and.capitalize()
methods are used to change the case of the characters.
today.lower()
"saturday".upper()
"monday".capitalize() # changes first character to uppercase
The .replace
method is used to replace a part of the string with another string. It takes the portion to be replaced and the replacement text as inputs or arguments.
another_day = today.replace("Satur", "Wednes")
another_day
Note that a new string is returned, and the original string is not modified.
today
The .split
method can be used to split a string into a list of strings based using the character(s) provided.
"Sun,Mon,Tue,Wed,Thu,Fri,Sat".split(",")
The .strip method is used to remove whitespace characters from the beginning and end of a string.
a_long_line = " This is a long line with some space before, after, and some space in the middle.. "
a_long_line_stripped = a_long_line.strip()
a_long_line_stripped
The .format
method is used to combine values of other data types e.g. integers, floats, booleans, lists etc. with strings. It is often used to create output messages for display.
# Input variables
cost_of_ice_bag = 1.25
profit_margin = .2
number_of_bags = 500
# Template for output message
output_template = """If a grocery store sells ice bags at $ {} per bag, with a profit margin of {} %,
then the total profit it makes by selling {} ice bags is $ {}."""
print(output_template)
# Inserting values into the string
total_profit = cost_of_ice_bag * profit_margin * number_of_bags
output_message = output_template.format(cost_of_ice_bag, profit_margin*100, number_of_bags, total_profit)
print(output_message)
Notice how the placeholders {}
in the output_template
string are replaced with the arguments provided to the .format
method.
It is also possible use the string concatenation operator +
to combine strings with other values, however, those values must first be converted to strings using the str
function.
"If a grocery store sells ice bags at $ " + cost_of_ice_bag + ", with a profit margin of " + profit_margin
"If a grocery store sells ice bags at $ " + str(cost_of_ice_bag) + ", with a profit margin of " + str(profit_margin)
In fact, the str
can be used to convert a value of any data type into a string.
str(23)
str(23.432)
str(True)
the_3_musketeers = ["Athos", "Porthos", "Aramis"]
str(the_3_musketeers)
Note that all string methods returns new values, and DO NOT change the existing string. You can find a full list of string methods here: https://www.w3schools.com/python/python_ref_string.asp.
Strings also support the comparision operators ==
and !=
for checking whether two strings are equal
first_name = "John"
first_name == "Doe"
first_name == "John"
first_name != "Jane"
We've looked at the primitive data types in Python, and we're now ready to explore non-primitive data structures or containers.
fruits = ['apple', 'banana', 'cherry']
fruits
type(fruits)
Let's try creating a list containing values of different data types, including another list.
a_list = [23, 'hello', None, 3.14, fruits, 3 <= 5]
a_list
empty_list = []
empty_list
To determine the number of values in a list, use the len
function. In general, the len
function can be used to determine of values in several other data types.
len(fruits)
print("Number of fruits:", len(fruits))
len(a_list)
len(empty_list)
You can access the elements of a list using the the index of the element, starting from the index 0.
fruits[0]
fruits[1]
fruits[2]
If you try to access an index equal to or higher than the length of the list, Python returns an IndexError
.
fruits[3]
fruits[4]
fruits[-1]
fruits[-2]
fruits[-3]
fruits[-4]
You can also access a range of values from the list. The result is itself a list. Let us look at some examples.
a_list = [23, 'hello', None, 3.14, fruits, 3 <= 5]
a_list
len(a_list)
a_list[2:5]
Note that the start index (2
in the above example) of the range is included in the list, but the end index (5
in the above example) is not included. So, the result has 3 values (indices 2
, 3
and 4
).
Here are some experiments you should try out (use the empty cells below):
- Try setting one or both indices of the range are larger than the size of the list e.g.
a_list[2:10]
- Try setting the start index of the range to be larger than the end index of the range e.g.
list_a[2:10]
- Try leaving out the start or end index of a range e.g.
a_list[2:]
ora_list[:5]
- Try using negative indices for the range e.g.
a_list[-2:-5]
ora_list[-5:-2]
(can you explain the results?)
The flexible and interactive nature of Jupyter notebooks makes them a great tool for learning and experimentation. Most questions that arise while you are learning Python for the first time can be resolved by simply typing the code into a cell and executing it. Let your curiosity run wild, and discover what Python is capable of, and what it isn't!
You can also change the value at a specific index within a list using the assignment operation.
fruits
fruits[1] = 'blueberry'
fruits
A new value can be added to the end of a list using the append
method.
fruits.append('dates')
fruits
A new value can also be inserted a specific index using the insert
method.
fruits.insert(1, 'banana')
fruits
You can remove a value from the list using the remove
method.
fruits.remove('blueberry')
fruits
What happens if a list has multiple instances of the value passed to .remove
? Try it out.
To remove an element from a specific index, use the pop
method. The method also returns the removed element.
fruits
fruits.pop(1)
fruits
If no index is provided, the pop
method removes the last element of the list.
fruits.pop()
fruits
You can test whether a list contains a value using the in
operator.
'pineapple' in fruits
'cherry' in fruits
To combine two or more lists, use the +
operator. This operation is also called concatenation.
fruits
more_fruits = fruits + ['pineapple', 'tomato', 'guava'] + ['dates', 'banana']
more_fruits
To create a copy of a list, use the copy
method. Modifying the copied list does not affect the original list.
more_fruits_copy = more_fruits.copy()
more_fruits_copy
# Modify the copy
more_fruits_copy.remove('pineapple')
more_fruits_copy.pop()
more_fruits_copy
# Original list remains unchanged
more_fruits
Note that you cannot create a copy of a list by simply creating a new variable using the assignment operator =
. The new variable will point to the same list, and any modifications performed using one variable will affect the other.
more_fruits
more_fruits_not_a_copy = more_fruits
more_fruits_not_a_copy.remove('pineapple')
more_fruits_not_a_copy.pop()
more_fruits_not_a_copy
more_fruits
Just like strings, there are several in-built methods to manipulate a list. Unlike strings, however, most list methods modify the original list, rather than returning a new one. Check out some common list operations here: https://www.w3schools.com/python/python_ref_list.asp
Following are some exercises you can try out with list methods (use the blank code cells below):
- Reverse the order of elements in a list
- Add the elements of one list to the end of another list
- Sort a list of strings in alphabetical order
- Sort a list of numbers in decreasing order
Tuple
A tuple is an ordered collection of values, similar to a list, however it is not possible to add, remove or modify values in a tuple. A tuple is created by enclosing values within parantheses (
and )
, separated by commas.
Any data structure that cannot be modified after creation is called immutable. You can think of tuples as immutable lists.
Let's try some experiments with tuples.
fruits = ('apple', 'cherry', 'dates')
# check no. of elements
len(fruits)
# get an element (positive index)
fruits[0]
# get an element (negative index)
fruits[-2]
# check if it contains an element
'dates' in fruits
# try to change an element
fruits[0] = 'avocado'
# try to append an element
fruits.append('blueberry')
# try to remove an element
fruits.remove('apple')
You can also skip the parantheses (
and )
while creating a tuple. Python automatically converts comma-separated values into a tuple.
the_3_musketeers = 'Athos', 'Porthos', 'Aramis'
the_3_musketeers
You can also create a tuple with just one element, if you include a comma after the element. Just wrapping it with parantheses (
and )
won't create a tuple.
single_element_tuple = 4,
single_element_tuple
another_single_element_tuple = (4,)
another_single_element_tuple
not_a_tuple = (4)
not_a_tuple
Tuples are often used to create multiple variables with a single statement.
point = (3, 4)
point_x, point_y = point
point_x
point_y
You can convert a list into a tuple using the tuple
function, and vice versa using the list
function
tuple(['one', 'two', 'three'])
list(('Athos', 'Porthos', 'Aramis'))
Tuples have just 2 built-in methods: count
and index
. Can you figure out what they do? While look could look for documentation and examples online, there's an easier way to check the documentation of a method, using the help
function.
a_tuple = 23, "hello", False, None, 23, 37, "hello"
help(a_tuple.count)
Within a Jupyter notebook, you can also start a code cell with ?
and type the name of a function or method. When you execute this cell, you will see the documentation for the function/method in a pop-up window.
?a_tuple.index
Try using count
and index
with a_tuple
in the code cells below.
Dictionary
A dictionary is an unordered collection of items. Each item stored in a dictionary has a key and value. Keys are used to retrieve values from the dictionary. Dictionaries have the type dict
.
Dictionaries are often used to store many pieces of information e.g. details about a person, in a single variable. Dictionaries are created by enclosing key-value pairs within curly brackets {
and }
.
person1 = {
'name': 'John Doe',
'sex': 'Male',
'age': 32,
'married': True
}
person1
Dictionaries can also be created using the dict
function.
person2 = dict(name='Jane Judy', sex='Female', age=28, married=False)
person2
type(person1)
Keys can be used to access values using square brackets [
and ]
.
person1['name']
person1['married']
person2['name']
If a key isn't present in the dictionary, then a KeyError
is returned.
person1['address']
The get
method can also be used to access the value associated with a key.
person2.get("name")
The get
method also accepts a default value which is returned if the key is not present in the dictionary.
person2.get("address", "Unknown")
You can check whether a key is present in a dictionary using the in
operator.
'name' in person1
'address' in person1
You can change the value associated with a key using the assignment operator.
person2['married']
person2['married'] = True
person2['married']
The assignment operator can also be used to add new key-value pairs to the dictonary.
person1
person1['address'] = '1, Penny Lane'
person1
To remove a key and the associated value from a dictionary, use the pop
method.
person1.pop('address')
person1
Dictonaries also provide methods to view the list of keys, values or key-value pairs inside it.
person1.keys()
person1.values()
person1.items()
person1.items()[1]
The result of the keys
, values
or items
look like lists but don't seem to support the indexing operator []
for retrieving elements.
Can you figure out how to access an element at a specific index from these results? Try it below. Hint: Use the list
function
Dictionaries provide many other methods. You can learn more about them here: https://www.w3schools.com/python/python_ref_dictionary.asp
Here are some experiments you can try out with dictionaries (use the empty cells below):
- What happens if you use the same key multiple times while creating a dictionary?
- How can you create a copy of a dictionary (modifying the copy should not change the original)?
- Can the value associated with a key itself be a dictionary?
- How can you add the key value pairs from one dictionary into another dictionary? Hint: See the
update
method. - Can the keys of a dictionary be something other than a string e.g. a number, boolean, list etc.?
Further Reading
We've now completed our exploration of variables and common data types in Python. Following are some resources to learn more about data types in Python:
- Python official documentation: https://docs.python.org/3/tutorial/index.html
- Python Tutorial at W3Schools: https://www.w3schools.com/python/
- Practical Python Programming: https://dabeaz-course.github.io/practical-python/Notes/Contents.html
You are now ready to move on to the next tutorial: Branching using conditional statements and loops in Python