OverviewTeaching: 20 min
Exercises: 10 minQuestions
What basic data types can I work with in Python?
How can I create a new variable in Python?
How do I use a function?
Can I change the value associated with a variable after I create it?Objectives
Assign values to variables.
Any Python interpreter can be used as a calculator:
3 + 5 * 4
This is great but not very interesting.
To do anything useful with data, we need to assign its value to a variable.
In Python, we can assign a value to a
variable, using the equals sign
For example we can capture the gross domestic power per capita for a country by
assinging the value ? to a variable gdpPercapUSD:
gdpPercapUSD = 2449
From now on, whenever we use
gdpPercapUSD, Python will substitute the value we assigned to
it. In layman’s terms, a variable is a name for a value.
In Python, variable names:
- can include letters, digits, and underscores
- cannot start with a digit
- are case sensitive.
This means that, for example:
gdpPercap0is a valid variable name, whereas
GDP_percapare different variables
Types of data
Python knows various types of data. Three common ones are:
- integer numbers
- floating point numbers, and
In the example above, variable
gdpPercapUSD has an integer value of
If we want to capture the gdp per capita of the country more precisely
we can use a floating point value by executing:
gdpPercapUSD = 2449.008185
To create a string, we add single or double quotes around some text. To keep track of the year we are working with we can store the year in a string:
year = "1952"
Using Variables in Python
Once we have data stored with variable names, we can make use of it in calculations. We may want to store the country’s gdp percapita in US dollars and in British pounds:
gdpPercapGBP = gdpPercapUSD * 0.779063
We could add the year to the label
columnlabel = 'gdpPercap_' + year
Built-in Python functions
To carry out common tasks with data and variables in Python,
the language provides us with several built-in functions.
To display information to the screen, we use the
print(gdpPercapUSD) print(gdpPercapGBP) print(columnlabel)
2249.008185 1752.119063630655 gdpPercap_1952
When we want to make use of a function, referred to as calling the function,
we follow its name by parentheses. The parentheses are important:
if you leave them off, the function doesn’t actually run!
Sometimes you will include values or variables inside the parentheses for the function to use.
In the case of
We can display multiple things at once using only one
print(columnlabel , 'for Algeria:' , gdpPercapUSD , '(USD)')
gdpPercap_1952 for Algeria: 2249.008185 (USD)
We can also call a function inside of another
For example, Python has a built-in function called
type that tells you a value’s data type:
<class 'float'> <class 'str'>
Moreover, we can do arithmetic with variables right inside the
print('gdpPercap in GBP:', gdpPercapUSD * 0.779063)
gdpPercap in GBP: 1752.119063630655
The above command, however, did not change the value of
To change the value of the
gdpPercapUSD variable, we have to
gdpPercapUSD a new value using the equals
gdpPercapUSD = 3520.610273 print('The gdp per capita for Angola in USD is:', gdpPercapUSD)
The gdp per capita for Angola in USD is: 3520.610273
Variables as Sticky Notes
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 that sticky note on a particular value.
Using this analogy, we can investigate how assigning a value to one variable does not change values of other, seemingly related, variables. For example, let’s store the, country Angola’s, gdp per capita GPB in its own variable:
# gdp per capita for Angola gdpPercapGBP = gdpPercapUSD * 0.779090 print('gdp per capita in GBP', gdpPercapGBP, 'and in USD:', gdpPercapUSD)
gdp per capita in GBP 2742.8722575915695 and in USD: 3520.610273
Similar to above, the expression
gdpPercapUSD * 0.779090is evaluated to
2742.8722575915695, and then this value is assigned to the variable
gdpPercapGBP(i.e. the sticky note
gdpPercapGBPis placed on
2742.8722575915695). At this point, each variable is “stuck” to completely distinct and unrelated values.
Let’s now change
gdpPercapUSD = 851.241141 print('gdp percapita in USD is now:', gdpPercapUSD, 'and gdp per capita in GBP is still: ', gdpPercapGBP)
gdp percapita in USD is now: 851.241141 and gdp per capita in GBP is still: 2742.8722575915695
gdpPercapGBPdoesn’t “remember” where its value comes from, it is not updated when we change
Check Your Understanding
What values do the variables
agehave after each of the following statements? Test your answer by executing the lines.
mass = 47.5 age = 122 mass = mass * 2.0 age = age - 20
`mass` holds a value of 47.5, `age` does not exist `mass` still holds a value of 47.5, `age` holds a value of 122 `mass` now has a value of 95.0, `age`'s value is still 122 `mass` still has a value of 95.0, `age` now holds 102
Sorting Out References
Python allows you to assign multiple values to multiple variables in one line by separating the variables and values with commas. What does the following program print out?
first, second = 'Grace', 'Hopper' third, fourth = second, first print(third, fourth)
Seeing Data Types
What are the data types of the following variables?
planet = 'Earth' apples = 5 distance = 10.5
type(planet) type(apples) type(distance)
<class 'str'> <class 'int'> <class 'float'>
Basic data types in Python include integers, strings, and floating-point numbers.
variable = valueto assign a value to a variable in order to record it in memory.
Variables are created on demand whenever a value is assigned to them.
print(something)to display the value of
Built-in functions are always available to use.