Last Updated:

Python Data Types | Examples

All the information that is entered into the program (each object entered by a variable) has its own class. Under any information object, a certain part of memory is allocated, in which transformations and operations associated with it are stored. It is the type of object that determines which memory area will be used.


Python Data Types

The first and simplest data type is numbers. They are divided into integer, floating-point and complex. They are denoted in Python respectively int, float and complex.


Integers do not have a fractional part, they are used for mathematical operations and the quantitative calculation of something. Floating-point numbers are needed to denote fractional and irrational values. The program uses an approximate value where necessary. They are used in the same way as integers, but for more complex tasks. Complex numbers have the two-part form "x+yj" (actual + imaginary part).


The next data type, string , is denoted str. You can enclose any information in it, limiting it to quotation marks. Here you can enter any words and sentences, a sequence of characters, etc. With a string, you can perform many operations, but the original one will remain the same.


One of the composite data types is a tuple. It can include a variety of values, from numbers to strings that are bounded by parentheses. Tuples work faster than almost the same data type, lists. Unlike tuples, you can change list objects. Each element is sorted by indexes depending on the position in the row, counting them from 0. List items are enclosed in square brackets.


A data type where a single value corresponds to a single definition is called a dictionary (dict). At the same time, the pairs are randomly arranged. Dictionary elements are separated by curly braces. Pairs of values are associated with ":", and the elements themselves are enclosed in quotation marks. All pairs are separated by commas.

The type function lets you know the type of an object. A function run on a specific object will output its data type.


|print(type(milk)) → <class 'str'>
|print(type(45)) → <class 'int'>


Operations in Python


Python has built-in data types and no built-in data types. The first include numbers, strings, lists, tuples, sets, dictionaries, Boolean variables (True, False), binary lists (bytes and byte arrays).

Objects can be transformed using a variety of functions. For example, from a numeric data type represented as an int, make a numeric object of the form float.

|float(a) →5,0


A similar result can be obtained in another way:


|print(float(a))→ 5,0


Also, you can perform the opposite operation: from the float object to make an int object. At the same time, the fractional part will simply disappear, that is, as a result, the number will not be rounded, and the numbers after the decimal point will simply be removed from its value.


|int(a)→ 5

Similar operation:

|print(int(a))→ 5


The Boolean data type can take two values, True and False. It is used in the construction of many algorithms, where there is a branching by actions depending on the logical value of the object. The simplest operation is to determine the fairness of the expression.


|print(bool(a))→ true

Or if the expression is unfair:

|print(bool(a)) →false


All objects are divided into mutable and immutable. Immutable include numbers, strings, tuples. Immutability means the inability to change anything in an object. That is, the original version of the object will not change during any operation. The output will be a new object, in the receipt of which the original one was involved.


|a+b → 5+b


Variable lists, sets and dictionaries, as well as byte arrays. For example, you can replace any item in a list with another. In addition, it is possible to add new objects to the list or remove the current ones.


|a=[5, 4, 8]
|a.append(b) →a=[5, 4, 8, b]


Let's look at possible operations with the string data type. The first and simplest function is to determine the length of a string (len). With its help, you can find out the number of characters contained in a string.


|len(a)→ 5


The next simple operation is stacking the drain.




The contents of the string can be repeated as many times as necessary. To do this, just use the print function.


|print(a * 5)→ stopstopstopstopstop

A string usually consists of several characters. You can find out what any account is using an index that starts with 0. To start counting from the end, the index is assigned "-".

|a[-3]→ ‘h’

A more complex function is the selection of line characters. You can select two indexes from which a portion of the row will be highlighted.

|a [1:-1]→ arsto

Consider the functions of another immutable data type, numbers. They are the same as mathematical operations: addition, subtraction, division, multiplication.

|a+b → 10
|a-b → 6
|a/b → 4
|a*b →16

These functions can be complicated, for example, by inferring division without residue (//) or the remainder of division (%), exponentiation (**).

|a//b → 8
|a%b → 5
|a**b → 289

The addition of complex numbers looks different. To perform this operation, the actual parts and imaginary parts are added separately.

|a+b → 8+3d

A lot more functions can be performed with the data type lists. You can change their composition by adding new elements (append function), connecting to another list (extend), replacing variables (insert), removing existing ones (remove).

|a=[2, ‘stop’, 7,3, 3, ‘juice’]
|b=[4, 3,7, ‘fresh’]
|a.extend(b) → [2, ‘stop’, 7,3, 3, ‘juice’, 4, 3,7, ‘fresh’]

In order to replace one element with another, you need to specify the index of the value to be replaced and the object that you want to add to the list. If you want to delete an item, it is important to specify its value.

|a=[2, ‘stop’, 7,3, 3, ‘juice’]
|a.insert(1, b) → [2, 4, 7,3, 3, ‘juice’]
|a.remove(‘juice’)→[2, 4, 7,3, 3]

The list can be completely cleared using the clear function. There is also a function to compare two lists (==). In this case, it is necessary to use if (if) and else (more).

|a=[2, ‘stop’, 7,3, 3, ‘juice’]
|f=[2, ‘stop’, 7,3, 3, ‘juice’]
|if(a==f) → 1
|else → -1

Many list functions are repeated with string and number functions. For example, the function of repeating a string is similar in record to repeating a list. Also, lists and strings have a similar function of separating characters (list).A string can be divided into words (split).

|a=[2, ‘stop’, 3, ‘juice’]
|list(a) → [2, ‘s’, ‘t’, ‘o’, ‘p’, 3, ‘j’, ‘u’, ‘i’, ‘c’, ‘e’]
|list(f) → [‘c’, ‘a’, ‘r’, ‘s’, ‘t’, ‘o’, ‘p’]

With the data type, dictionaries are a bit more complicated. First, you need to create them. There are several ways to do this. Let's consider the simplest of them. The first stage is the creation of the dictionary itself.The next is to assign pairs of meanings (dependencies) to the dictionary.

|a={ }
|a={ ‘heart’ : 0, ‘hand’ : 1, ‘head’ : 2}

The second way to create a dictionary is done using the dict function.

|a=dict( ‘heart’=0, ‘hand’=1, ‘head’=2) →
|a={ ‘heart’ : 0, ‘hand’ : 1, ‘head’ : 2}

Other methods are used in a more in-depth study of Python, and new users are unlikely to need it.

Just like a list, a dictionary can be deleted by the clear function. Specific functions relate to working with dependencies. For example, if you want to get pairs of dictionary objects, use the items function, or use the keys to get only the keys.

In order to remove the key and get the value, the pop function is used, after which you must specify the key and add default. You can modify a dictionary to add new values from another dictionary by using the update function.Use the popitem function to retrieve and delete a pair.

|a={ ‘heart’ : 0, ‘hand’ : 1, ‘head’ : 2}
|a.items() → [(‘heart’, 0), (‘hand’=1), (‘head’=2)]

Motorcades are almost the same as dictionaries, but immutable. You can create them in the same way, but using the tuple function.

|a → ('d ',)

With tuples, you can perform many operations that are fair to the list, but do not change it.
One of the universal data types is sets. They store variables in a random order, without repeating characters.

|a= [1, 5, 3, 6, 1]
|set(a)→{ 1, 5, 3, 6 }

The functions of the set are similar to those of other data types. For example, len, comparison (==) is used to count symbols. There are also specific properties, such as combining two sets (union( )) or copying one (copy). The function of highlighting variables that belong to only one of the presented sets (a^b) is also highlighted.

One of the previously described data types is bool. It includes two values: true and false. False refers to any empty data types and zeros, or immutable none, false with a single value. Truth – all data that is not related to zero and constant is true.

|bool(7) → true
|bool(0) → false


Python as a programming language


As mentioned earlier, all data is divided into types. This is necessary for the proper operation of the system, that is, to perform operations, the program must recognize the type. The Python system determines the type of variable itself, which, although it can lead to errors, reduces the time of operation. In Python, dynamic typing works, that is, one variable can acquire several values of different types.

Python is a simple language where there is no need to memorize the sequences of operations for a long time. Literally in one or two lines you can perform the necessary action. There are many built-in functions of the system, which simplifies the work with this language.

Python is a fairly popular and simple programming language. It can be used in a variety of tasks: from conducting ordinary mathematical operations to creating software. If desired, you can write your own applications in Python, modify files and data.

This language can be used on a wide variety of platforms, and its simple operation names are similar to English and algebraic notation. So, Python is a promising and convenient language, multifunctional and easy to use.