Step by Step Guide How To understand a Data Types in Python 3
The Python 3 programming language is a powerful tool for creating programs for a wide variety of purposes, even for beginners. With its help, it is possible to solve problems of various types.
Note. If you did not watch a previous part where we have explained how to install Python 3 and set up a local programming environment on your windows computer then we highly recommend checking this first!
The content of this video tutorial:
- Numbers
- Booleans
- Strings
- Lists
- Tuples
- Dictionaries
The numbers in Python 3 are no different from ordinary numbers. They support a set of the most common mathematical operations:
x + y – Addition
x – y – Subtraction
x * y – Multiplication
x / y – Division
x // y – Getting the whole part from the division
x% y – The remainder of the division
-x – Changing the sign of a number
abs (x) – The number module
divmod (x, y) – The pair (x // y, x% y)
x ** y – Exponentiation
pow (x, y [, z]) – by module (if the modulus is given)
Also note that integers in python 3, unlike many other languages, support long arithmetic (however, this requires more memory).
- Those who had computer science at school know that numbers can be represented not only in the decimal system.
For example, a binary code is used on the computer, and, for example, the number 19 in the binary system will look like so: 10011. Also sometimes you need to translate numbers from one number system to another.
Integers
int ([object], [base of the number system]) – conversion to an integer in the decimal number system. By default, the system is decimal, but you can specify any base from 2 to 36, inclusive.
bin (x) – converting an integer to a binary string.
hex (x) is the conversion of an integer to a hexadecimal string.
oct (x) is the conversion of an integer to an octal string.
Floating-Point Numbers
Floating-Point Numbers support the same operations as integers. However (due to the representation of numbers in the computer), the real numbers are inaccurate, and this can lead to errors:
<blockquote>>> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 0.9999999999999999</blockquote>
Complex numbers (complex)
In Python, complex numbers are also built:
<blockquote>>> x = complex(1, 2) >>> print(x) (1+2j)</blockquote>
Booleans
bool (x) – conversion to the bool type, using the standard truth validation procedure. If x is false or omitted, it returns False, otherwise, it returns True.
Strings
Strings in Python are ordered sequences of characters used to store and display textual information, so you can use the lines to work with everything that can be represented in text form.
The lines in apostrophes and in quotation marks are the same. The reason for having two options is to allow quotes or apostrophes to be inserted into literal literals without using escaping.
Strings in triple apostrophes
The main advantage of strings in triple quotes is that they can be used to write multi-line blocks of text. Inside such a line, there may be quotes and apostrophes, most importantly, that there are not three inverted commas.
Lists
Lists in Python are ordered modified collections of arbitrary types of objects (almost as an array, but the types may differ).
To use lists, you need to create them. There are several ways to create a list. For example, you can process any iterated object (for example, a string)
Tuple
A tuple, in fact – an unchangeable list.
Why do you need tuples if there are lists?
Protection from a fool. That is, the tuple is protected from changes, both intentional (which is bad), and random (which is good).
Create an empty tuple:
<blockquote>>> >>> a = tuple () # Using the built-in function tuple () >>> a () >>> a = () # Using the literal of the tuple >>> a () >>></blockquote>
For example, the pride of programmers in python is to swap the values of two variables:
a, b = b, a
Dictionaries
Dictionaries in Python are unordered collections of arbitrary objects with access by key. They are sometimes called associative arrays or hash tables.
To work with the dictionary, you need to create it.
You can create it in several ways:
- First, using a literal,
- Secondly, using the function of dist,
- Thirdly, with the help of the Frumkes method,
- Fourthly, with the help of dictionary generators, which are very similar to the list generators.
If you are interested, you can watch the video: