2 Programming Foundations for Data Analysis

Learn how Python variables, collections, conditionals, loops, and functions work together to make data analysis steps repeatable and easier to inspect.

Variables and values

A is a name associated with a value. In Python, = assigns a value; unlike in mathematics, it does not mean “is equal to.” Meaningful names make calculations easier to understand and let programs reuse results.

Python determines a value’s type at runtime. Common built-in types are integers (int), decimal numbers (float), text (str), and truth values (bool). Values with different types may need conversion before they can be used together; for example, float("12.4") converts numeric text to a decimal number.

Choose clear, descriptive names such as daily_sales instead of vague names such as x. Names are case-sensitive, so count and Count refer to different names.

Collections

A stores multiple values. Choose a type according to how the data needs to be organized and used:

  • A is an ordered, changeable sequence, suitable for observations or measurements.

  • A is an ordered, unchangeable sequence, useful for a fixed group of related values.

  • A stores key–value pairs, useful for associating labels with values.

  • A holds distinct values, useful when duplicates are not needed.

Python lists use square brackets, and their indexes start at zero. Lists can be changed in place, while tuples cannot. Dictionaries use keys to retrieve associated values. If a key might be absent, get() can return a default instead of raising a KeyError.

Conditionals

A runs different code depending on whether a test is true or false. Use if for the first test, elif for additional possibilities, and else for the remaining case. The indented block under each condition is the code controlled by it.

Conditions commonly use comparisons such as ==, !=, <, and >=. Use and, or, and not to combine or reverse logical tests. A condition can flag values for review, but an unusual value should not be assumed to be wrong without context.

Loops

A repeats an operation. A for processes items in a in turn, while range() can generate a sequence of integers. A while repeats as long as its condition remains true. In data analysis, for loops are often useful for applying a consistent check to each observation.

On each pass, a such as reading takes the next item. Avoid changing a while iterating over that same ; creating a new is often clearer and safer.

Functions

A is a named, reusable block of code. Define one with def, provide parameters for its inputs, and use return to send a result back to the caller. Functions reduce repetition and make analysis steps easier to test and understand.

In this example, values is a parameter. When the is called with readings, that is the input. The empty- check prevents division by zero; None indicates that no mean is available for an empty . A can also call other functions, helping build a larger analysis from small, understandable steps.

Combining the building blocks

This example combines variables, a , a , a , and a . It flags negative readings, retains nonnegative readings, and calculates the mean of the retained values.

Separating the calculation into a makes it reusable, and the filtering rule is explicit. In real analyses, document the rule and preserve or record excluded values so results can be interpreted and checked. Together, variables give values meaningful names; collections organize them; conditionals select actions based on tests; loops repeat actions across data; and functions package reusable operations with inputs and outputs. These building blocks help make analysis steps repeatable, inspectable, and improvable.