lesson
35 min

Variables and Data Types

Store numbers, text, and yes/no values in Python, and learn why the type of a value matters.

Objective

By the end of this lesson, you can create variables, name the four basic data types, and do math with them in Python.

Why Python

Python is a programming language that reads close to plain English. Most machine learning tools are built for it. You do not need to install anything for this module. The code boxes on this page run real Python in your browser. The first run downloads Python, which takes a moment and uses some data.

Variables

A variable is a name that holds a value. You make one with an equals sign.

station = "Example Station"
high_temp_f = 91
rain_inches = 0.25
had_storm = False

Read the equals sign as "gets." high_temp_f gets 91.

The four basic types

Every value has a type. The type decides what you can do with it.

TypePython nameExampleWhat it is for
Whole numberint91Counts, years
Decimal numberfloat0.25Measurements
Textstr"Example Station"Names, labels
True or falseboolFalseYes/no facts

Types matter because "91" + "1" gives "911" (text glued together), but 91 + 1 gives 92. Data files often store numbers as text by mistake. You will fix that in Module 4.

Doing math

Python does math with +, -, *, and /. Here is a real formula: converting Fahrenheit to Celsius.

Run the code. Then change high_temp_f to a temperature from your own area and run it again. round(x, 1) keeps one decimal place.
Python 3
Ctrl+Enter to run | Tab to indent | Python runs in your browser via Pyodide

Notice the types. 91 is an int. After division, the answer is a float.

A national example

Judgment check

Variables can hold anything, including private details. In this course, never put your real name, address, school, or phone number in code. Use made-up examples. Code gets shared, pasted, and saved in places you do not control.

Quiz: Types check

  1. 01What type is the value 0.25?

    Choose one.

  2. 02What does "12" + "3" give in Python?

    Choose one.

  3. 03Which is safe to put in practice code for this course?

    Choose one.

Ask. Answer. Build together.