int, float, str) (~20 min)đ§ Activities spaced throughout the session
Any Python interpreter can be used as a calculator:
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 weight_kg:
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:
This means that, for example:
weight0 is a valid variable name, whereas 0weight is notweight and Weight are different variablesPython knows various types of data. Three common ones are:
In the example above, variable weight_kg has an integer value of 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:
We might decide to add a prefix to our patient identifier:
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 function:
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 print, we use the parentheses to tell the function what value we want to display. We will learn more about how functions work and how to create our own in later episodes.
We can display multiple things at once using only one print call:
đ§Ș Try changing the values in parentheses.
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:
Use type() when you are unsure what kind of value a variable holds. This can be used to investigate subtle bugs.
We can also do arithmetic with variables right inside the print function:
đ§Ș Try changing the arithmetic formula.
Tip
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.
The above command, however, did not change the value of weight_kg:
To change the value of the weight_kg variable, we have to assign weight_kg a new value using the equals = sign:
đ§Ș Try changing the value of
weight_kg.
Use the built-in function help to get help for a function. Every built-in function has extensive documentation that can also be found online.
This help message (the functionâs âdocstringâ) includes a usage statement, a list of parameters accepted by the function, and their default values if they have them.
Tip
help() and documentation are useful when syntax or parameters are unclear.
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.
Tip
Assignment links a name to a value stored in memory.
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:
weight_lb stores a value that was computed at a moment in time. It does not keep a link to the expression 2.2 * weight_kg. This is a common pitfall worth emphasizing. Letâs now change weight_kg:
Tip
Reassigning one variable does not automatically recalculate other variables.
Before running this code, try to predict the values of mass and age when each line is run.
On each line, the right-hand side is evaluated first, then the variable is updated.
Run one line at a time and track each variable.
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
Python can assign multiple variables in one line. This may be useful for swapping values without introducing an extra variable.
A list lets you store many related values in one variable. Use lists when order matters and values belong together. For example, daily temperatures, sample IDs, or repeated measurements from one experiment.
We create a list by putting values inside square brackets and separating the values with commas:
We can access elements of a list using indices. Indices are positions of elements in the list. In Python indices start at 0, so the first element has an index of 0 and the fourth has an index of 3.
đĄ Negative indices count from the end of a sequence.
Programming languages like Fortran, MATLAB and R start counting at 1 because thatâs what human beings have done for thousands of years. Languages in the C family (including C++, Java, Perl, and Python) count from 0 because it represents an offset from the first value in the array (the second value is offset by one index from the first value). This is closer to the way that computers represent arrays (if you are interested in the historical reasons behind counting indices from zero, you can read Mike Hoyeâs blog post).
Lists are mutable, so single elements can be edited. The change happens directly in the existing list object.
Strings are immutable in Python. You can replace a whole string with a new one, but you cannot change individual characters of an existing string in place. This distinction between mutable and immutable types appears throughout Python.
Caution
This raises a TypeError because strings do not support item assignment.
Mutable types can have surprising side effects when multiple variables refer to the same object.
Note
Both variables changed because they refer to the same list in memory.
Create an explicit copy when you need independent lists.
This is often the safest default when learners are still building confidence with references because code which modifies data in place can be more difficult to understand. However it is often more efficient to modify large data structures in place than to create a modified copy for every change.
Note
Mutability is a key idea for debugging surprising state changes.
A list can contain any Python variables, including other lists.
For example, you could represent the products on the shelves of a small grocery shop as a nested list called veg:

To store the contents of the shelf in a nested list, you write it this way:
You can reference each row on the shelf as a separate list. For example, veg[2] represents the bottom row.
Use two indices for nested lists. Think of the first index as selecting a row and the second as selecting a position inside that row.

Tip
Pattern: nested_list[row][column].
Lists can contain values of different data types.
Caution
This flexibility is useful, but if not intentional, can make debugging harder.
Common methods to modify lists in place include append, pop, reverse, and sort. Use append to add one item, pop to remove and return an item, reverse to flip the order, and sort to sort the list in place.
If two names refer to one list, method calls through either name change that shared list.
Tip
Copy first whenever one list should change and the other should stay unchanged.
start:stopA section of a list or string is called a slice. To slice a list or string, we use a start index and a stop index. The start index is included, but the stop index is excluded. This "include start, exclude stop" rule is consistent across strings, lists, and other data types that can be sliced.
If you want to take a slice from the beginning of a sequence, you can omit the first index in the range.
And similarly, you can omit the ending index in the range to take a slice to the very end of the sequence:
Tip
Use [:stop] for âfrom the beginningâ and [start:] for âto the endâ.
element[:4]?element[4:]?element[:]?element[-1]?element[-2]?element[1:-1] does.element[1:-1] creates a substring from index 1 up to (not including) the final index, effectively removing the first and last letters from âoxygenâ
step for non-continuous slicesUse start:stop:step to sample at regular intervals. The third value step controls the spacing between selected elements.
We can keep the same step but set the start index later to select a different pattern.
For numbers, the + and * operators perform arithmetic. However, for lists and strings, they perform different operations: + concatenates and * repeats.
Tip
The same operator symbol can implement different behavior for different data types.
list(existing_list) when you need an independent copy.start:stop:step) to select continuous and patterned subsets efficiently.+ and * are overloaded and behave by type.đ Readable, testable code comes from checking assumptions often and keeping state changes explicit.
print(type(value)) and len(...) to inspect data as you go.This module builds the foundations of Python: variables, core data types, indexing, slicing, and basic operators. By the end, learners can trace how values move through code and write small, readable snippets with confidence.
The concepts in this module connect directly to practical data handling and exploration in Python.
| Submodule | Python Connection | Why It Matters |
|---|---|---|
| Variables and Assignment | Simple statements | Correct assignment is essential for tracking program state. |
| Built-in Data Types | Built-in Types | Choosing the right type improves correctness and readability. |
| Indexing and Slicing | Sequence Types | Slicing is a core pattern for selecting and reshaping data. |
Attribution
This lesson is derived from materials developed by the Software Carpentry project.
The original content is licensed under the Creative Commons Attribution 4.0 International (CC BY 4.0) license: https://github.com/swcarpentry/python-novice-inflammation/blob/main/LICENSE.md