Basic data types in Python 3. Examples of working with collections for beginners
Tags: beginners, programming, python, tutorial
Published: 2022-10-25 07:33:48

##From author
You can find many interesting articles on my website, please visit it [Happy Python Website](https://happypython.ru/)

Automatic translation in the browser will help you to study everything conveniently. 

If the material is useful, I will try to translate all my articles for you


In this article, we will analyze how Python works with variables and what data types can be used in this language.
If we approach the question of Python typing, we can say that it refers to a language with implicitly strong dynamic typing.
Implicit typing is understood so that when declaring a variable, the programmer does not need to specify its type. That is, you do not need to write "int a = 1", as it is written, for example, in C++ and many other languages.

In Pytnon, data types can be divided into built-in and non-built-in, which are used when importing modules.

## Data Types in Python
The main built - in types include…

- None (undefined variable value)
- Boolean Variables (Boolean Type)
- Numbers (Numeric Type)
- int – integer
- float – floating point number
- complex – complex number

###Sequence Type

- list
- tuple
- range 

###Strings (Text Sequence Type )

- str

###Binary Lists (Binary Sequence Types)

- bytes – bytes
- bytearray – arrays of memoryview bytes – special objects for accessing the internal data of an object via protocol buffer

###Sets (Set Types)
- set
- frozenset – immutable set

###Dictionaries (Mapping Types)

- dict - dictionary

##Initializing variables and working with them
In order to immediately declare and initialize a variable, it is necessary to write its name, then put an equal sign and the value with which this variable will be created. For example:

```
value = 10
```
The integer value 10 within the Python language is essentially an object. An object, in this case, is an abstraction for representing data, data is numbers, lists, strings, etc. At the same time, data should be understood as both the objects themselves and the relationships between them. Each object has three attributes – an identifier, a value, and a type. An identifier is a unique feature of an object that allows you to distinguish objects from each other, and a value is directly information stored in memory, which is controlled by an interpreter and a type – a concept that determines what exactly a value is.

To determine the identifier, there is a built-in id function:

```
value = 10
id(value)
140654768777808
```
If you need to get a value, use the print function:

```
value = 10
print(value)
10
```
And it remains to find the type using the "type" function:

There are mutable and immutable types in Python:
 
- Immutable types include: integers (int), floating point numbers (float), complex numbers (complex), boolean variables (bool), tuples (tuple), strings (str) and immutable sets (frozen set).

- Mutable types include: lists , sets , dictionaries (dict).

## Basic data types in Python with examples:

###Numbers

Integers, floating point numbers, and complex numbers belong to a group of numbers. In Python, they are represented by the int, float and complex classes.

Integers can be of any length, they are limited only by available memory.

Floating point numbers have limited precision. Visually, the difference between an integer and a floating—point number can be seen in the console by the presence of a dot: 1 is an integer, 1.0 is a floating-point number.

Complex numbers are written in the form x+yj, where x is the real part of the number and y is the imaginary part. Here are some examples:

```
 
a = 12345678901234567890
a
12345678901234567890
type(a)

b = 0.12345678901234567890
b
0.12345678901234568
type(b)

c = 1 + 2j
c
(1+2j)
type(c)

```
Note that the value of variable b has been truncated.

###Strings

A string is a sequence of characters. We can use single or double quotes to create a string. Multiline strings can be indicated with triple quotes, ''' or """:

```
s = "Simple string"
s = '''multiline string'''
s = """multiline string"""
```
It is worth noting that strings in Python belong to the category of immutable sequences, that is, all functions and methods can only create a new string.

###Lists

A list is an ordered sequence of elements. It is very flexible and is one of the most used types in Python. The list items do not have to be of the same type.

Declaring a list is pretty simple. The comma-separated list items are placed inside the square brackets:

```
 a = [1, 2.2, 'python']
```
We can use the [] operator to extract an element (such an operation is called "index access") or a range of elements (such an operation is called "slice extraction") from a list. In Python, indexing starts from scratch:

```
a = [5,10,15,20,25,30,35,40]
print("a[2] =", a[2])
a[2] = 15

print("a[0:3] =", a[0:3])
a[0:3] = [5, 10, 15]

print("a[5:] =", a[5:])
a[5:] = [30, 35, 40]
```
Lists are a mutable type, i.e. the values of its elements can be changed:

```
a = [1,2,3]
a[2] = 4
a
[1, 2, 4]
```
###Tuples

Just like a list, a tuple is an ordered sequence of elements. The whole difference is that tuples are immutable.

Tuples are used to protect data from overwriting and usually work faster than lists, because they cannot be changed.

To create a tuple, you need to put comma-separated elements inside the parentheses:

```
t = (5,'program', 1+3j)
```
We can use the slice extraction operator [] to extract elements, but we cannot change their values:

```
t = (5,'program', 1+3j)
print("t[1] =", t[1])
t[1] = program

print("t[0:3] =", t[0:3])
t[0:3] = (5, ' program', (1+3j))
# Leads to an error because
# tuples are immutable
t[0] = 10
```
###Sets
The set is an unordered, unique sequence. A set is declared using comma-separated elements inside curly brackets:

```
a = {5,2,3,1,4}
# output of a set variable
print("a =", a)
a = {1, 2, 3, 4, 5}
# data type of variable a
print(type(a))
```
You can perform operations such as union and intersection on sets. Since the elements in the set must be unique, they automatically remove duplicates:

```
a = {1,2,2,3,3,3}
a
{1, 2, 3}
```
Since the set is an unordered sequence, the slice extraction operator does not work here:

```
a = {1,2,3}
a[1]
Traceback (most recent call last):
  File "", line 1, in  
TypeError: 'set' object does not support indexing
```
### Dictionaries
Dictionaries are unordered sets of key-value pairs.

They are used when you need to map a value to each of the keys and be able to quickly access the value by knowing the key. In other languages, dictionaries are usually called map, hash, or object. Dictionaries are optimized for data extraction. To extract the value, you need to know the key.

The dictionary is declared by pairs of elements in the form of a key:value enclosed in curly brackets:

```
d = {1:'value', 'key':2}
type(d)
```
The value can be of any type, but the key is only immutable.

We use the key to get the corresponding value. But not the other way around:

```
d = {1:'value', 'key':2}
print("d[1] =", d[1])
d[1] = value
print("d['key'] =", d['key'])
d['key'] = 2

# Causes an error
printing("d[2] =", d[2]);
```
###Conclusion
The python programming language gives a huge advantage over other languages. No wonder it is one of the most popular and multi-platform, can be used in any direction, and in some cases is indispensable.