There are different types of data that we store in variables. These different types of data are known as data types. For example,
x = 5
y = 'Python'
Here, x
is an integer data type and y
is a string
data type.
Different Python Data Types
Data Types | Example |
---|---|
Integer | x = 7 |
Float | x = 7.5 |
String | x = 'Programiz' |
Boolean | x = False |
Complex | x = 5j |
List | x =['Python', 'C', 'Java'] |
Tuple | x = ('Python', 'C', 'Java') |
Set | x = {'Python', 'C', 'Java') |
Dictionary | x = {'name' : 'Tim', 'age' : 56} |
Since everything is an object in Python programming, data types are classes and variables are instances (object) of those classes.
For x = 5,
x
is an instance of class
int
.
Note: We use type()
method to identify the
data type of the variable. For example,
x = 5
print(type(x))
# Output: <class 'int'>
Commonly Used Python Data Types
Some of the commonly used data types in Python are:
-
Python Numbers (
int
,float
) - Python Strings
str
Int
The integer data type int
represents positive or negative whole
numbers(i.e. numbers without fraction or decimal). For example,
# positive number
x = 45
print(type(x)) # <class 'int'>
# negative number
x = -3
print(type(x)) # <class 'int'>
Float
The floating point float
type represents decimal or fractional
numbers. For example,
x = 5.5
print(type(x)) # <class 'float'>
x = 0.002
print(type(x)) #<class 'float'>
String
The string data type (str
) represents alphanumeric characters.
For example,
x = 'Car'
print(type(x)) # <class 'str'>
x = 'abc12'
print(type(x)) # <class 'str'>
x = 'Python Programming'
print(type(x)) # <class 'str'>
Other Python Data Types
Other data types that are commonly used in Python are:
Lists
The list type allows us to store multiple values. The elements of a list are
inside square []
brackets. For example,
my_list = [2, 'python', 4.5]
To learn more about list, visit Python List.
Tuples
Tuple is also used to store elements of multiple data types similar to lists.
One important difference between a tuple and a list is that lists are mutable
(changeable), whereas tuples are immutable (unchangeable). The tuple is
created using ()
parenthesis. For example,
my_tuple = ('USA', 'jake', 54)
To learn more about list, visit Python Tuple.
Sets
Set is an unordered collection of items. Also, a set cannot contain duplicate
elements. The elements of set are inside curly braces {}
. For
example,
my_set = {1, 3, 'jimmy'}
To learn more about list, visit Python Set.
Dictionary
The dictionary is an ordered collection that stores elements in key/value pairs. For example,
my_dict = {'name' : 'Jake' , 'age' : 15}
The elements of a dictionary are accessed using the keys. To learn more about list, visit Python Dictionary.
Booleans
The boolean data type represents one of two values: either
True
or False
. For example,
x = True
print(type(x)) # <class 'bool'>
To learn more, visit Python Booleans
None
In Python, None
means no value (lack of value) For example,
x = None