THE Data Types on Python (in French « data types ») are essential for representing, processing and using data. Using different types of datayou can store information efficiently, which helps optimize the performance of your application.
Python Data Types: What are they?
Python Data Types are value categorizations to represent different types of data. These types define how information can be stored and manipulated. Python offers a large number of data types, the most basic of which are integers, floats, and strings. More complex types include lists, tuples, dictionaries, and sets. Data types play a central role in Python programming because they allow data to be structured and manipulated.
The purpose of Python Data Types is to organize data according to certain rules to meet the needs of different applications. Each data type has specific functions and properties. For example, lists can store elements in an ordered sequence, while dictionaries use key-value pairs for targeted querying of data. By choosing appropriate data types, your program becomes more flexible and easier to maintain.
The different Data Types on Python
The Python programming language includes several built-in data types. Here is a non-exhaustive list:
-
Numeric Data Types :
int
,float
,complex
. -
Chains :
str
. -
Sequential Data Types :
list
,tuple
,range
. -
Binary types :
bytes
,bytearray
,memoryview
. -
Dictionaries :
dict
. -
Boolean Data Types :
bool
. -
Sets :
set
,frozenset
.
Numeric Data Types
There are several Python numeric data types that are used to work with numbers:
- Integer (
int
) : represents whole numbers without decimal places. - Long (
long
) : is used for integers of unlimited length. Starting with Python 3,long
Andint
have been merged. - Float (
float
) : includes numbers with decimal places. - Complex (
complex
) : includes complex numbers with a real part and an imaginary part, indicated by the suffixj
.
a=3
# variable with float value.
b=3.17
# variable with complex value.
c=50+7j
python
Strings
A string or Python string (str
) reproduces a sequence of characters. You can mark them with single, double or triple quotes.
# Single quotes
str1 = 'Hello World!'
# Double quotes
str2 = "This is a string."
# Triple quotes for multi-line strings
str3 = '''This is a multi-line string.'''
python
The strings are invariable in Python, so you can't modify them after you create them. However, strings support many methods and operations to manipulate, combine, and parse them. You can store the results in variables and thus obtain new strings.
Examples of operations on character strings:
- Length of a string:
len(str)
. - Cutting :
str[start:end]
. - Concatenate strings:
str1 + str2
.
Sequential Data Types
Sequential data types in Python are data structures that store a ordered collection of elements. They allow you to access elements based on their position in the sequence. There are several types of Python sequential data:
-
Lists (
list
) : Python lists are mutable sequential data types which represent an ordered collection of items. You can edit, add, and delete items in a list. Lists are created by brackets and contain items of different data types.
my_list = [1, 2, 3, 'Hello', 'World']
python
-
Tuple (
tuple
) : tuples are invariable sequential data types which, like lists, indicate an ordered collection of elements. Unlike lists, tuples cannot be modified later. Tuples are enclosed in parentheses.
my_tuple = (4, 5, 6, 'Python')
python
-
Tidy (
range
) : This is a special Python data type that is used to generate a sequence of numbers. It is used in particular for loops and iterations. The data typerange
creates a sequence of integers in a certain range. The objectrange
generates the sequence of numbers on demand and does not store it in memory as a complete list. This improves efficiency, for example for large sequences of numbers.
# Range from 0 to 4
my_range = range(4)
for i in my_range:
print(i)
# Output: 0, 1, 2, 3
python
Binary types
-
Bytes (
bytes
) : the data typebytes
represents a invariable sequence of bytes. Bytes can be created with the constructorbytes()
or with the prefixb
.
my_bytes = b'Hello'
python
-
Byte array (
bytearray
) : unlikebytes
,bytearray
is part of the Modifiable Python Data Typeswhich represent a sequence of bytes. This means that you can modify the values after you declare them.
my_bytearray = bytearray(b'Python')
python
Dictionaries
In Python, a dictionary (dict
) is a data structure that stores an unordered collection of elements as a key-value pairs. Unlike lists or tuples, which contain an ordered sequence of elements, a dictionary allows its elements to be accessed via a unique key.
my_dict = {
"name": "Max",
"age": 25,
"city": "Lyon"
}
python
Boolean Data Types
Python Boolean data types represent truth values which can be either true (true
) is false (false
). These data are of critical importance for logical evaluations and decisions within a program.
a = True
b = False
result_1 = (a and b) # returns False
result_2 = (a or b) # returns True
result_3 = (not a) # returns False
python
Sets
A set (set
) is a unordered collection of unique values that does not allow duplicates. You can use it to store multiple elements, where each element is unique.
my_set = {1, 2, 3, 4, 5}
python
A frozenset
is a immutable version of a set. Once created, items cannot be added, deleted, or edited.
my_set = {1, 2, 3, 4, 5}
frozen_set = frozenset(my_set)
python
Web hosting with personal advisor!
Powerful, flexible and high-performance web hosting with email box, personal advisor and domain included 1time year !
Domain
SSL Certificate
24/7 Support