Python fundamentals
Numbers, text, and Boolean values
Recognise int, float, str, and bool values and inspect a value with type().
Lesson 8 of 63
Reading: about 2 min
Lesson + practice: about 12 min
Difficulty: Beginner
On this page5 sections
Python values have types. Whole numbers are int values; numbers with a decimal point are float values; text in quotes is a str value; and True or False is a bool value. A Boolean represents a true-or-false state. The value determines its type; a variable name can later be assigned a value of another type.
sessions = 3 # int: whole number
hours = 1.5 # float: decimal number
name = "Sam" # str: text
finished = False # bool: True or False
print(type(sessions))
print(type(name))<class 'int'> <class 'str'>
Text and numbers differ
The number 3 and the text "3" look similar when printed, but have different types. You can calculate with the number. Text can be joined to other text. Adding a number directly to text raises a TypeError.
print(3 + 2)
print("3" + "2")5 32
Represent no value
None is a special value meaning that no value is present. It is different from 0, an empty string, and False. Later, a function can return None to indicate that it did not produce a usable number.
result = None
print(result)
print(type(result))None <class 'NoneType'>
Why this works
A value has a type, while a variable name can later refer to a different value. That distinction explains why 3 + 2 calculates 5 but "3" + "2" joins text into "32". None is the built-in value for an absent result; it is not a number. type() helps inspect a value when an input or calculation behaves unexpectedly.
Practice
Create one int, float, str, and bool value, plus a name set to None. Print each value and its type. Remember that True, False, and None begin with capital letters and are not surrounded by quotes.
Is "3" the same type as 3?
Reveal answer
No. "3" is a str value, while 3 is an int value.
Try another case
What are the types of 4, 4.0, "4", and False?
Check your answer
Reveal answer
They are int, float, str, and bool, respectively.
Your progress
Reveal the check answer or reach the end to complete this lesson.