Skip to content

Variables & Data Types

1 min read2 lessons in topic

Learning objective

By the end of this lesson you will be able to use numbers, strings and booleans in Python and check their types.

Python figures out the type of a value automatically. You still need to know the types to write correct code.

Numbers

age = 21            # int
height = 1.72       # float
print(age + 5)      # 26
print(height * 2)   # 3.44

Strings

greeting = "Hello"
name = "Ama"
print(greeting + ", " + name)  # Hello, Ama
print(len(greeting))            # 5

Booleans & comparisons

is_student = True
print(21 > 18)      # True
print(21 == 18)     # False

Checking types

print(type(21))     # <class 'int'>
print(type(3.14))   # <class 'float'>
print(type("x"))    # <class 'str'>
  • int — whole numbers
  • float — numbers with decimals
  • str — text
  • bool — True or False

Code playground

Run and edit the starter code — experiment freely.

python · runnable

Output appears here when you press Run.

Practice exercise

Store your age, your height and your name. Print each value together with its type.

Check your understanding

1. What does type(3.14) return?

2. Which operator checks if two values are equal?

Self-graded — answer then check below.