Numbers, Strings, and Booleans
A kitchen has jars, bottles, and boxes. What you can do with something depends on what's inside.
In Variables and Naming, you stuck name tags on all kinds of values: 88, 36.6, "Ben", True. Python didn't ask what kind each one was, but it always knows. And the kind of value decides what you can do with it: you can multiply two numbers, but not two names.
The kind of a value is called its type. This lesson covers the five types you'll use constantly.
Jars, bottles, and boxes
In a kitchen, flour goes in a jar, milk goes in a bottle, and eggs go in a carton. You pour milk, but you scoop flour, and you count eggs. Try to pour an egg carton, and things go badly.
Python's values work the same way. A whole number can be counted and multiplied. Text can be joined and searched. A true-or-false value can be used to make decisions. Each type comes with its own set of things you can do with it.
The five everyday types
| Type | What it holds | Examples |
|---|---|---|
int | whole numbers | 17, -3, 2026 |
float | numbers with a decimal point | 36.6, 0.5, -2.0 |
str | text (a string of characters) | "Maria", 'hello', "" |
bool | True or False | True, False |
NoneType | "nothing here" | None |
You can ask Python for any value's type with the type function:
print(type(17))
print(type(36.6))
print(type("Maria"))
print(type(True))
print(type(None))<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'NoneType'>The word class is Python's word for a type. You'll learn to make your own in Classes and Objects.
Whole numbers: int
Use int for anything you count: students, points, lives, years.
Python whole numbers have a nice property: they can be as big as you like. Many languages put a limit on whole numbers, and going past it gives nonsense. Python just keeps going:
print(2 ** 100)1267650600228229401496703205376(** means "to the power of." More on operators in Operators and Expressions.)
To make big numbers readable in your code, you can put underscores between the digits. Python ignores them:
population = 8_100_000_000
print(population)8100000000Decimals: float
Any number written with a decimal point is a float, short for floating-point number. Use it for measurements, prices, averages, and percentages:
height = 1.62
average = 88.75
print(height, average)1.62 88.752.0 is a float, even though it's a whole amount, because of the decimal point. 2 is an int.
Floats come with one quirk that surprises everyone. Computers store decimals in binary, and some simple decimals, like 0.1, can't be stored exactly, the same way 1/3 can't be written exactly as a decimal (0.3333...). So:
print(0.1 + 0.2)0.30000000000000004That's not a Python bug. JavaScript, Java, and nearly every other language print the same thing. For grades and measurements, the tiny error doesn't matter; you'll learn to round numbers for display in Formatting Output with f-strings. Real banking software uses a special type called Decimal for money instead.
Text: str
A str holds text, written between quotes. You've been using them since your first program. A string can be a single character, a whole paragraph, or even empty:
name = "Maria"
initial = "M"
empty = ""
print(len(name), len(initial), len(empty))5 1 0len gives back the length of a string: how many characters it has. Unlike some languages, Python doesn't have a separate type for a single character. "M" is just a string of length 1.
Some values look like numbers but are really text. A phone number like "09171234567" or a ZIP code like "02134" should be a string: you'll never add two phone numbers together, and storing one as an int would lose the leading zero.
And note the difference the quotes make:
print(5 + 5)
print("5" + "5")10
55Two ints add up. Two strings join together. Working with Strings is all about what else strings can do.
True or false: bool
A bool (short for Boolean, after the mathematician George Boole) holds one of exactly two values: True or False. They're written with a capital first letter and no quotes:
is_enrolled = True
has_paid = False
print(is_enrolled, has_paid)True FalseYou'll rarely type True or False directly. Much more often, they come from questions your code asks, like "is the score at least 75?":
score = 82
print(score >= 75)TrueBooleans are what your program will use to make decisions in Making Decisions: if, elif, and else.
Nothing at all: None
Sometimes a variable needs to exist, but there's no value for it yet. A student who hasn't taken the test doesn't have a score of 0; they have no score. Python's value for "nothing here" is None:
test_score = None
print(test_score)NoneNone is different from 0, from "", and from False. It means the value is missing, not that it's zero or empty. You'll see None a lot once you start writing functions in Writing Functions.
Mixing types
Python is happy to mix ints and floats in math. The answer is a float, so nothing is lost:
print(3 + 0.5)
print(type(3 + 0.5))3.5
<class 'float'>But it won't guess what you mean when you mix numbers and text:
print("Age: " + 17) # error: TypeError: can only concatenate str (not "int") to strShould that be the text "Age: 17", or should Python try to turn "Age: " into a number? Rather than guess, Python stops and asks you to decide. You'll learn how to convert between types in Type Conversion.
Try it
This program asks Python for the type of several values, including some that might trick you. Predict each line before you check.
Running Python here
Running Python right in the browser is coming to Syntaxia soon. For now, you can read and edit the code here. If you have Python installed on your computer (see Setting Up), save it as main.py and run it with python main.py (or python3 main.py on macOS and Linux).
Check your prediction
<class 'int'>
<class 'float'>
<class 'str'>
<class 'float'>
<class 'bool'>
<class 'str'>
<class 'NoneType'>
13
hahahaQuotes always make a string, even around digits or the word True. Dividing with / always gives a float in Python, even when it comes out even: 10 / 2 is 5.0. len counts every character, including the comma, the space, and the !. And multiplying a string by a number repeats it.
Try it yourself
- Create a variable holding your height in meters and another holding your age, and print the type of each.
- Store a phone number in a variable, first as a number like
9171234567and then as the text"09171234567". Print both. Which one kept the leading zero? - Predict the result of
print("7" * 3)andprint(7 * 3), then check them in the interactive shell.
Check your understanding
Up next
You know the kinds of values Python works with. Now let's do things with them: math, comparisons, and combining true-or-false answers, in Operators and Expressions.
