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.
| Type | Python name | Example | What it is for |
|---|---|---|---|
| Whole number | int | 91 | Counts, years |
| Decimal number | float | 0.25 | Measurements |
| Text | str | "Example Station" | Names, labels |
| True or false | bool | False | Yes/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.
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
Discussion
Ask. Answer. Build together.
Loading…