Skip to content

Python Data Types

Understanding Data Types in Python

Data types in Python are a fundamental concept that plays a crucial role in defining the nature of variables and how they behave during operations. Python is a dynamically-typed language, which means that the interpreter can determine the type of a variable at runtime. This flexibility allows for more concise and expressive code. Let’s delve into the essential data types in Python.

Numeric Data Types

Integers

intint

Integers represent whole numbers without any fractional part. They can be positive or negative.

datatype.py
age = 25
population = -1000
datatype.py
age = 25
population = -1000

or you can use the int()int() function to convert a string to an integer.

datatype.py
age = int(25)
population = int(-1000)
datatype.py
age = int(25)
population = int(-1000)

Floating-Point Numbers

floatfloat

Floating-point numbers are used to represent real numbers with a decimal point.

datatype.py
pi = 3.14
temperature = -15.5
datatype.py
pi = 3.14
temperature = -15.5

or you can use the float()float() function to convert a string to a floating-point number.

datatype.py
pi = float(3.14)
temperature = float(-15.5)
datatype.py
pi = float(3.14)
temperature = float(-15.5)

Complex Numbers

complexcomplex

Complex numbers have both a real and an imaginary part.

datatype.py
z = 3 + 4j
datatype.py
z = 3 + 4j

or you can use the complex()complex() function to create a complex number.

datatype.py
z = complex(3, 4)
datatype.py
z = complex(3, 4)

Sequence Types

Strings

strstr

Strings are sequences of characters and are enclosed in single or double quotes.

datatype.py
name = "John"
message = 'Hello, Python!'
datatype.py
name = "John"
message = 'Hello, Python!'

or you can use the str()str() function to convert a number to a string.

datatype.py
name = str("John")
message = str('Hello, Python!')
datatype.py
name = str("John")
message = str('Hello, Python!')

Lists

listlist

Lists are ordered, mutable sequences that can contain elements of different data types.

datatype.py
fruits = ['apple', 'banana', 'orange']
mixed_list = [1, 'two', 3.0, [4, 5]]
datatype.py
fruits = ['apple', 'banana', 'orange']
mixed_list = [1, 'two', 3.0, [4, 5]]

or you can use the list()list() function to convert a tuple to a list.

datatype.py
fruits = list(('apple', 'banana', 'orange'))
mixed_list = list((1, 'two', 3.0, [4, 5]))
datatype.py
fruits = list(('apple', 'banana', 'orange'))
mixed_list = list((1, 'two', 3.0, [4, 5]))

Tuples

tupletuple

Tuples are ordered, immutable sequences. Once created, their elements cannot be changed.

datatype.py
coordinates = (3, 4)
RGB_color = (255, 0, 0)
datatype.py
coordinates = (3, 4)
RGB_color = (255, 0, 0)

or you can use the tuple()tuple() function to convert a list to a tuple.

datatype.py
coordinates = tuple([3, 4])
RGB_color = tuple([255, 0, 0])
datatype.py
coordinates = tuple([3, 4])
RGB_color = tuple([255, 0, 0])

Set Types

Sets

setset

Sets are unordered collections of unique elements.

datatype.py
unique_numbers = {1, 2, 3, 4, 5}
datatype.py
unique_numbers = {1, 2, 3, 4, 5}

or you can use the set()set() function to create a set.

datatype.py
unique_numbers = set([1, 2, 3, 4, 5])
datatype.py
unique_numbers = set([1, 2, 3, 4, 5])

Mapping Type

Dictionary

dictdict

Dictionaries are unordered collections of key-value pairs.

datatype.py
person = {'name': 'Alice', 'age': 30, 'city': 'Wonderland'}
datatype.py
person = {'name': 'Alice', 'age': 30, 'city': 'Wonderland'}

or you can use the dict()dict() function to create a dictionary.

datatype.py
person = dict({'name': 'Alice', 'age': 30, 'city': 'Wonderland'})
datatype.py
person = dict({'name': 'Alice', 'age': 30, 'city': 'Wonderland'})

Boolean Type

Boolean

boolbool

Boolean values represent truth or falsehood and are used in logical operations.

datatype.py
is_raining = True
has_pet = False
datatype.py
is_raining = True
has_pet = False

or you can use the bool()bool() function to convert a number to a boolean.

datatype.py
is_raining = bool(1)
has_pet = bool(0)
datatype.py
is_raining = bool(1)
has_pet = bool(0)

Special Types

None Type

NoneTypeNoneType

The NoneNone type represents the absence of a value or a null value.

datatype.py
result = None
datatype.py
result = None

Type Conversion

Python allows you to convert between different data types using built-in functions like int()int(), float()float(), str()str(), etc.

datatype.py
num_str = "42"
num_int = int(num_str)
datatype.py
num_str = "42"
num_int = int(num_str)

Checking Data Types

You can check the data type of a variable using the type()type() function.

datatype.py
age = 25
print(type(age))  # <class 'int'>
datatype.py
age = 25
print(type(age))  # <class 'int'>

Operations on Data Types

Different data types support various operations. For example, you can concatenate strings, perform arithmetic operations on numbers, and use logical operators with booleans.

datatype.py
greeting = "Hello, "
name = "Alice"
full_greeting = greeting + name  # Concatenation of strings
datatype.py
greeting = "Hello, "
name = "Alice"
full_greeting = greeting + name  # Concatenation of strings

Data Type Table

There are many more data types in Python, but the ones listed above are the most commonly used ones. The following table summarizes the data types in Python.

Data Type        DescriptionExample
intintIntegerage = 25age = 25
floatfloatFloating-point numberpi = 3.14pi = 3.14
complexcomplexComplex numberz = 3 + 4jz = 3 + 4j
strstrStringname = "Alice"name = "Alice"
listlistListfruits = ['apple', 'banana', 'orange']fruits = ['apple', 'banana', 'orange']
tupletupleTuplecoordinates = (3, 4)coordinates = (3, 4)
setsetSetunique_numbers = {1, 2, 3, 4, 5}unique_numbers = {1, 2, 3, 4, 5}
frozensetfrozensetFrozen setunique_numbers = frozenset({1, 2, 3, 4, 5})unique_numbers = frozenset({1, 2, 3, 4, 5})
dictdictDictionaryperson = {'name': 'Alice', 'age': 30, 'city': 'Wonderland'}person = {'name': 'Alice', 'age': 30, 'city': 'Wonderland'}
boolboolBooleanis_raining = Trueis_raining = True
NoneTypeNoneTypeNoneresult = Noneresult = None
rangerangeRangenumbers = range(1, 10)numbers = range(1, 10)
bytesbytesBytesdata = b'Hello, Python!'data = b'Hello, Python!'
bytearraybytearrayByte arraydata = bytearray(10)data = bytearray(10)
memoryviewmemoryviewMemory viewdata = memoryview(bytes(10))data = memoryview(bytes(10))

Conclusion

Understanding Python’s data types is fundamental to writing effective and efficient code. Python’s flexibility in handling various data types makes it suitable for a wide range of applications, from simple scripts to complex data analysis and machine learning tasks. As you continue your journey in Python programming, a solid grasp of data types will empower you to manipulate and process data effectively.

Explore more Python concepts and practical examples in our tutorials on Python Central Hub!

Was this page helpful?

Let us know how we did