In Python we have several built-in data types. The most common types of data that you will find are numbers, strings and booleans.
Key points
Python is dynamically typed, meaning you don’t need to declare the type of a variable explicitly.
You can use the type() function to check the data type of a variable.
Data types determine what operations can be performed on the data.
You can convert between different data types using built-in functions like int(), float() and str()
Numeric Types
Description
Type
Example
integers or whole numbers
int
a = int(12)
floating-point numbers
float
b = float(7.3)
a =12b =7.3print("A:", a, type(a))print("B:", b, type(b))
A: 12 <class 'int'>
B: 7.3 <class 'float'>
Text Types
Description
Type
Example
textual data (strings)
str
a = “Jean Golding”
name ="Jean Golding"print("Name:", name, type(name))
Name: Jean Golding <class 'str'>
Note that
It’s important that when writing numbers in your scripts, you do not put quotation marks around them, otherwise they will be recognized by the Python interpreter as strings. There is a difference between 3.14159 and “3.14159”, the first is a number and the second is just a pair of characters.
pi =3.14159print("pi:", pi, type(pi))
pi: 3.14159 <class 'float'>
pi ="3.14159"print("pi:", pi, type(pi))
pi: 3.14159 <class 'str'>
Boolean Types
Description
Type
Example
boolean values (True or False)
bool
a = True
a =23b =2c = a != bprint("C:", c, type(c))
C: True <class 'bool'>
Exercise
Without using Python, can you tell what is the data type of these variables?
x =32number_of_participants ="1017"Friday =Truey =float(1)a =10<8