Last updated on 2023-04-21 | Edit this page
- 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?
- 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 track the weight of a patient who
weighs 60 kilograms by assigning the value
60 to a variable
From now on, whenever we use
weight_kg, Python will
substitute the value we assigned to it. In layperson’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:
weight0is a valid variable name, whereas
Weightare different variables
Python knows various types of data. Three common ones are:
- integer numbers
- floating point numbers, and
In the example above, variable
weight_kg has an integer
60. If we want to more precisely track the weight
of our patient, we can use a floating point value by executing:
To create a string, we add single or double quotes around some text. To identify and track a patient throughout our study, we can assign each person a unique identifier by storing it in a string:
Once we have data stored with variable names, we can make use of it in calculations. We may want to store our patient’s weight in pounds as well as kilograms:
= 2.2 * weight_kgweight_lb
We might decide to add a prefix to our patient identifier:
= 'inflam_' + patient_idpatient_id
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
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(patient_id, 'weight in kilograms:', weight_kg)
inflam_001 weight in kilograms: 60.3
We can also call a function inside of another function call. 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('weight in pounds:', 2.2 * weight_kg)
weight in pounds: 132.66
The above command, however, did not change the value of
To change the value of the
weight_kg variable, we have
weight_kg a new value using the
= 65.0 weight_kg print('weight in kilograms is now:', weight_kg)
weight in kilograms is now: 65.0
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 subject’s weight in pounds in its own variable:
# There are 2.2 pounds per kilogram = 2.2 * weight_kg weight_lb print('weight in kilograms:', weight_kg, 'and in pounds:', weight_lb)
weight in kilograms: 65.0 and in pounds: 143.0
Everything in a line of code following the ‘#’ symbol is a comment that is ignored by Python. Comments allow programmers to leave explanatory notes for other programmers or their future selves.
Similar to above, the expression
2.2 * weight_kg is
143.0, and then this value is assigned to the
weight_lb (i.e. the sticky note
weight_lb is placed on
143.0). At this point,
each variable is “stuck” to completely distinct and unrelated
Let’s now change
= 100.0 weight_kg print('weight in kilograms is now:', weight_kg, 'and weight in pounds is still:', weight_lb)
weight in kilograms is now: 100.0 and weight in pounds is still: 143.0
weight_lb doesn’t “remember” where its value comes
from, it is not updated when we change
`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
print(type(planet)) print(type(apples)) print(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
# some kind of explanationto add comments to programs.
- Built-in functions are always available to use.