Python Tutorial: Data Types
Every value in Python has a type. Learn them all — strings, integers, floats, booleans, None, and how to convert between them.
Different kinds of data
A name is not the same as an age. An age is not the same as a price. A yes/no answer is not the same as either.
Python uses types to tell different kinds of data apart. Every value has a type, and the type determines what you can do with it.
Twelve steps. Every type, every conversion, every gotcha.
What you'll learn in this Python data types tutorial
This interactive Python tutorial has 12 hands-on exercises. Estimated time: 20 minutes.
- Not all data is the same — In Chapter 2, you stored your name and your age in variables. But did you notice? Your name had quotes around it and you…
- str — anything you can read — A `str` (short for 'string') holds text. Your name is a string. An email is a string. A tweet is a string.
- int — whole numbers — An `int` is a whole number. No decimal point. Positive, negative, or zero.
- float — when you need more precision — A `float` is a number with a decimal point: `3.14`, `99.99`, `-0.5`.
- bool — the yes/no type — A `bool` can only be one of two values: `True` or `False`.
- None — the 'nothing here yet' type — Sometimes you need to say 'this variable exists, but has no value yet'. That is what `None` is for.
- Python figures out the type for you — Here is something wild: you never have to tell Python what type a variable is. Python infers it from the value.
- The sneaky "42" vs 42 trap — This is the most common type mistake beginners make. Seriously, everyone does this at least once.
- int(), float(), str() — convert between types — You can convert between types:
- str() — join text with numbers — You cannot glue a string and a number together directly:
- isinstance() — double-check a type — `isinstance(value, type)` returns `True` if the value is of that type — `False` otherwise.
- All five types — your turn — Final challenge! No starter code.
Python Data Types concepts covered
- Different kinds of data