Python Tutorial · Python Basics · Lesson 6 of 42
Python Data Types
The built-in types: str, int, float, bool, list, tuple, dict, set and None.
Built-in data types
| Category | Types |
|---|---|
| Text | str |
| Numeric | int, float, complex |
| Sequence | list, tuple, range |
| Mapping | dict |
| Set | set, frozenset |
| Boolean | bool |
| None | NoneType |
Use type() to see the type of any value:
print(type("hello"))
print(type(42))
print(type(3.14))
print(type(True))
print(type([1, 2]))
print(type({"a": 1}))
print(type(None))
Output
<class 'str'> <class 'int'> <class 'float'> <class 'bool'> <class 'list'> <class 'dict'> <class 'NoneType'>
Mutable vs immutable
- Immutable values can't change after creation:
int,float,str,tuple,bool. Operations create new objects. - Mutable values can change in place:
list,dict,set.
s = "hi"
t = s.upper() # returns a NEW string
print(s, t)
nums = [3, 1, 2]
nums.sort() # changes the list in place
print(nums)
Output
hi HI [1, 2, 3]
Knowing which types are mutable explains many bugs — and many interview questions.
Test yourself
Which of these types is mutable?
strtuplelistint
Show answer
Answer: list