Python - Data Types

2 minute read

Python is a dynamically-typed language, meaning that the type of a variable is determined at runtime based on the value assigned to it. In this tutorial, we’ll go over the different data types available in Python and how to use them.

Numeric Types

Python supports several numeric types, including integers, floating-point numbers, and complex numbers.

Integers

Integers (int) are whole numbers that can be positive, negative, or zero. They have unlimited precision in Python, meaning they can be as large or as small as your system’s memory can handle.

Here are some examples of integers:

x = 42
y = -10
z = 0

Floating-Point Numbers

Floating-point numbers (float) are numbers with a fractional component. They can be represented using scientific notation with an “e” or “E” to indicate the exponent.

Here are some examples of floating-point numbers:

x = 3.14
y = -0.5
z = 2e3

Complex Numbers

Complex numbers (complex) have a real and imaginary component, both of which are represented as floating-point numbers.

Here are some examples of complex numbers:

x = 3 + 4j
y = -2j
z = complex(1, -1)

Boolean Type

The boolean data type (bool) has only two possible values: True and False. It is often used in conditional statements and loops.

Here are some examples of boolean values:

x = True
y = False

Sequence Types

Python supports several sequence types, including strings, lists, and tuples.

Strings

Strings (str) are sequences of characters. They can be enclosed in single quotes, double quotes, or triple quotes.

Here are some examples of strings:

x = "Hello, World!"
y = 'Python is awesome!'
z = '''This is a multi-line
string.'''

List

Lists are ordered collections of items. They can contain elements of any type and can be modified after creation.

Here are some examples of lists:

x = [1, 2, 3]
y = ['apple', 'banana', 'cherry']
z = [True, 'hello', 3.14]

Tuples

Tuples are similar to lists, but they are immutable, meaning they cannot be modified after creation.

Here are some examples of tuples:

x = (1, 2, 3)
y = ('apple', 'banana', 'cherry')
z = (True, 'hello', 3.14)

Mapping Types

Python also supports mapping types, which are collections of key-value pairs.

Dictionaries

Dictionaries (dict) are unordered collections of key-value pairs. Each key in a dictionary must be unique.

Here are some examples of dictionaries:

x = {'name': 'John', 'age': 30, 'city': 'New York'}
y = {1: 'apple', 2: 'banana', 3: 'cherry'}
z = {'name': 'Jane', 'age': 25, 'city': 'Los Angeles'}

Set Types

Python also supports sets, which are unordered collections of unique elements.

Sets

Sets (set) are collections of unique elements. They are mutable, meaning they can be modified after creation.

Here are some examples of sets:

x = {1, 2, 3}
y = {'apple', 'banana', 'cherry'}
z = {True, 'hello', 3.14}

Conclusion

In conclusion, Python supports a wide range of data types, including numeric types, boolean type, sequence types, mapping types, and set types. Understanding the various data types is essential to writing effective and efficient Python code. By using the appropriate data type for a given task, you can write code that is both easy to read and performant. It’s important to note that Python is a dynamically-typed language, so the type of a variable is determined at runtime based on the value assigned to it. This flexibility allows for more concise and expressive code, but it also means that you need to be mindful of the types of your variables to avoid errors and unexpected behavior.

Updated: