Types, Variables, and Basic Operations

Learning Outcomes

  • Assign values to variables.
  • Explain what a list is.
  • Create and index lists of simple values.
  • Change the values of individual elements
  • Append values to an existing list
  • Reorder and slice list elements
  • Create and manipulate nested lists

Questions

  • 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?
  • How can I get help while learning to program?
  • How can I store many values together?

Structure & Agenda

  1. Variables and data types (int, float, str) (~20 min)
  2. Built-in functions and basic debugging (~20 min)
  3. Lists, mutability, and nested data (~20 min)
  4. Slicing patterns and operator behavior (~20 min)

🔧 Activities spaced throughout the session

Variables and Data Types

Any Python interpreter can be used as a calculator:

This is great but not very interesting.

Variables

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.

Naming Variables

In Python, variable names:

  • can include letters, digits, and underscores
  • cannot start with a digit
  • are case sensitive.

This means that, for example:

  • weight0 is a valid variable name, whereas 0weight is not
  • weight and Weight are different variables

Types of Data

Python knows various types of data. Three common ones are:

  • integer numbers
  • floating point numbers, and
  • strings.

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:

Using Variables

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:

Built-in 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 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.

Challenge: Inspecting Types

What are the data types of the following variables?

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.

Getting Help

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.

NoteVariables 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.

Value of 65.0 with weight_kg label stuck on it

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:

Value of 65.0 with weight_kg label stuck on it, and value of 143.0 with weight_lb label stuck on it

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:

Value of 100.0 with label weight_kg stuck on it, and value of 143.0 with label weight_lb stuck on it

Tip

Reassigning one variable does not automatically recalculate other variables.

Challenge: Predict Variable Values

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

Multiple Assignments

Python can assign multiple variables in one line. This may be useful for swapping values without introducing an extra variable.

Lists

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:

Read List Elements by Position

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).

Update List Elements In Place

Lists are mutable, so single elements can be edited. The change happens directly in the existing list object.

Why Strings Behave Differently

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.

Mutability and Side Effects

  • Strings and numbers are immutable.
  • Lists are mutable.

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.

Copy Before Editing

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.

Nested Lists

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:

veg is represented as a shelf full of produce. There are three rows of vegetables on the shelf, and each row contains baskets of vegetables.

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.

Nested list index map where first index selects row and second index selects basket in that row.

Tip

Pattern: nested_list[row][column].

Lists Can Hold Mixed Types

Lists can contain values of different data types.

Caution

This flexibility is useful, but if not intentional, can make debugging harder.

Modifying Lists in Place

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.

Method Calls Follow the Same Reference Rules

If two names refer to one list, method calls through either name change that shared list.

Make an Independent Copy First

Tip

Copy first whenever one list should change and the other should stay unchanged.

Slicing

Slice Sequences with start:stop

A 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.

Implied Bounds

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”.

Challenge: Slicing strings

  • What is the value of element[:4]?
  • What about element[4:]?
  • Or element[:]?
  • What is element[-1]?
  • What is element[-2]?
  • Explain what 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’

Challenge: Slicing strings from the end

How can we rewrite the slice for getting the last three characters of element, so that it works even if we assign a different string to element?

Test your solution with the following strings: carpentry, clone, hi.

Challenge: Slicing lists from the end

Select the last 2 rows of the list. Aim for a solution that still works if the list length changes.

Add a step for non-continuous slices

Use 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.

Challenge: Every other character

Create a slice that returns every second character:

::2 is a concise way to take every second value across the full sequence.

Operator overloading

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.

Challenge: Operator Overloading

Which output matches counts * 2?

  1. [2, 4, 6, 8, 10, 2, 4, 6, 8, 10]
  2. [4, 8, 12, 16, 20]
  3. [[2, 4, 6, 8, 10], [2, 4, 6, 8, 10]]
  4. [2, 4, 6, 8, 10, 4, 8, 12, 16, 20]

counts * 2 replicates elements of the list and concatenates them together:

It is equivalent to counts + counts::

Further Information

📚 Keypoints

  • Assignment binds names to values; reassignment updates a name, not every derived variable.
  • Lists are ordered and mutable; strings are ordered and immutable.
  • Shared references to mutable values can cause unintended side effects.
  • Use list(existing_list) when you need an independent copy.
  • Use slices (start:stop:step) to select continuous and patterned subsets efficiently.
  • Operators like + and * are overloaded and behave by type.

🔑 Readable, testable code comes from checking assumptions often and keeping state changes explicit.

🔦 Hints

  • Predict outputs before you run code, then explain any mismatch.
  • Use print(type(value)) and len(...) to inspect data as you go.
  • Prefer short, descriptive variable names that reflect real meaning.

Module Summary

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.

Additional Learning

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.
NoteAttribution

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