< Prev
Next >

# Python - Data Types

In Python, each value has a specific data type and each data type falls into a specific type category. Some of the most general used specific type categories are:
• Numbers
• Sequences
• Mapping Sets
Each of these type categories represents the various built-in data types belonging to it. Let us take a close look at these type categories and their associated built-in data types.

• ## Type Category - Numbers

• Ther four most used built-in data types that belong to the Numbers type category are:

• int - it represents signed and unsigned integer values up to any length, it is defined by the int class.

• float - it represents the floating-point numbers with its accuracy up to 15 decimal places, it is defined by the float class.

• complex - it represents the complex numbers, it is defined by the complex class.

• bool - booleans are a subtype of integers, and the bool data type represents the boolean values i.e. True or False, it is defined by the bool class.

Type Category Data Type Description

Numbers

int Used to represent integer numbers

Numbers

float Used to represent floating-point numbers

Numbers

complex Used to represent complex numbers

Numbers

bool Used to represent boolean values - True or False

Note: Apparently, we can use the type() function, to know the class type of a specific value or isinstance() function, to know if an object belongs to a specific class.

An example of values under the Number type category
``````#Python - An example of values under the Number type category

# Creating an integer object of type int
x = 9
print(x, 'is of type', type(x))

# Creating an floating-point object of type float
y = 19.99
print(y, 'is of type', type(y))

# Creating an complex number object of type complex
z = 9+2j
print(z, 'is of type', type(z))

# Creating an boolean object of type bool
b = True
print(b, 'is of type', type(b))``````

## Output

``````9 is of type <class 'int'>
19.99 is of type <class 'float'>
(9+2j) is of type <class 'complex'>
True is of type <class 'bool'>``````

• ## Type Category - Sequences

• The three most used built-in data types that belong to the Sequences type category are:

• str - it represents Unicode string i.e. a sequence of characters, defined by the class, str. To create a string object in Python, we can enclose a string literal i.e. a string of characters, within delimiters, such as -
1. A pair of single quotes.
2. A pair of double quotes.
3. A pair of triple single quotes.
4. A pair of triple-double quotes.

Note: A string literal enclosed within single or double quotes can only span across a single line, whereas, a string literal enclosed within triple single or triple double quotes can span across multiple lines.

• list - it represents a mutable ordered sequence of items, declared within a pair of square brackets [ ], defined by the class, list.

• tuple - it represents an immutable ordered sequence of items, declared within a pair of parentheses ( ), defined by the class, tuple.

Type Category Data Type Description

Sequences

str Used to represent strings

Sequences

list Used to represent a mutable ordered sequence of items

Sequences

tuple Used to represent an immutable ordered sequence of items

An example of values under Sequences type category
``````#Python - An example of values under Sequences type category

# Creating an string object of type str
s = 'Hello World!'
print(s, 'is of type', type(s))

# Creating an list object of type list
misc =[19 ,-9, 9.99, 'Have a perfect day!']
print(misc, 'is of type', type(misc))

# Creating a tuple object of type tuple
customer =('Edward', 27, 178.5, 'M')
print(customer, 'is of type', type(customer))
``````

## Output

``````Hello World! is of type <class 'str'>
[19, -9, 9.99, 'Have a perfect day!'] is of type <class 'list'>
('Edward', 27, 178.5, 'M') is of type <class 'tuple'>``````

• ## Type Category - Mapping Sets

• The two most used built-in data types that belong to the Mapping Sets type category are:

• set - it represents an unordered sequence of items which are declared within a pair of curly braces { }. It is defined by the class, set.

• dict - it represents a dictionary of an ordered sequence of items stored in the form of key:value pairs, where key and value are objects which are declared within a pair of curly braces { }. It is defined by the class, dict.

• Type Category Data Type Description

Mapping Sets

dict
It is used to represent sequence of items stored in the form of key:value pairs, where key and value are objects.

Mapping Sets

set
It is used to represents an unordered sequence of items.

An example of values under Mapping Set type category
``````#Python - An example of values under Mapping Set type category

# Creating an dict object with key-value pairs of type dict
dict1 = {
4:'Fourth',
3:'Third',
'two':'Second',
'one':'First',
5:'Fifth'
}
print(dict1, 'is of type', type(dict1))

# Creating an set object of type set
set_ex ={'Never give up', 19.2, -29, 'A'}
print(set_ex, 'is of type', type(set_ex))``````

## Output

``````{4: 'Fourth', 3: 'Third', 'two': 'Second', 'one': 'First', 5: 'Fifth'} is of type <class 'dict'>
{'A', 19.2, 'Never give up', -29} is of type <class 'set'><class 'tuple'>``````

< Prev
Next >
< The First Python Program in IDLE
Python keywords and identifiers >