DATA TYPES



Oh, I definitely feel that you wish to move to practice. Please, wait a little more. The more efficiently we work with you now, the better we perceive and understand, the easier and faster our subsequent work will be!

You can't snap you fingers and get systemic thinking, but when you take efforts and get it, it will change everything: new horizons will be open, speed will increase, special skills will be improved, IQ will rise, appetite will become more appetizing, energy - more energetic, future - brighter. So, keep attention and focus. The battle is about to begin!

Humans have 5 basic senses: sight, hearing, touch, smell and taste. By the way, different sources classify senses differently. Some of them claim that there is the sixth sense - spatial orientation. It includes a sense of falling down, lifting up and so on. Buddhists describe one more feeling - "mind", usually restless and constantly worrying, in a word, unhappy.

Human is magnificent, the supreme creation that invented geography, coffee and toilet paper, but we are studying computer science. How much "senses" does a computer have?

Five. But distinct from the human ones. Computer "senses" are called the types of information which a computer can receive, process and output (by analogy with a person: receiving information by looking, processing by thinking, outputting by telling what has been seen).

A computer receives, processes, outputs:

  1. Numerical data. A computer have some skills in addition, subtraction, multiplication and even division.
  2. Character or text data. Everything that can be transmitted with characters, everything that can be typed with a keyboard, with the Alt codes, with the character insert function refers to this category.
  3. Graphical data. Static images, static vector graphics. It includes all that can be called "drawn".
  4. Video data. Moving pictures. And (sure, sure) our lovely YouTube, films, cartoons, Simpsons, Ricks and Morties are related. All that video is video data (this is simple, even I, the interpreter, understood).
  5. Audio data. Any sound, music and all about what you can say "I hear." А computer understands this type too.

Perhaps, computers will be taught to reproduce and to understand smells or tastes in far far future (or in close future, nothing is exact nowadays). They will be taught to swear from their iron hearts, to respond emotionally and sincerely. PCs will learn the sense of humor, and then they'll start to troll their owners. Highly professional trolling, with best knock-knock jokes. Sound awesome, eh?

So, let's repeat: numbers, text, graphic, video, audio. That's all. So far, a computer doesn't understand anything else. But even here there is a snag.

The thing is that all data (and text, and numbers, and I'm the map 1 hour long version clip, and the "How to Talk to Your Cat About Gun Safety: And Abstinence, Drugs, Satanism, and Other Dangers That Threaten Their Nine Lives" book in PDF-file) are stored in a computer in the form of ones and zeros.

It's named binary system. Only zeros. Only ones. The text you're reading right now is stored in memory, processed by CPU of your smartphone (tablet or maybe remote control) as a coherent and incomprehensible set: 01110100001101...

Not a lot, right?



TYPES OF DATA IN PYTHON


Now attention! How does a computer understand which sequence of ones and zeros is for a video file and which is for an audio one? Which is text and which is numbers? No way! It can't define a chain of zeros and ones as anything other than a chain of zeros and ones. Looks like a tautology, but that's true. "I'm a simple machine: I saw zeros and ones - I perceived it as zeros and ones".

In confidence, the processor doesn't understand a lot. It is important for it to receive bits (zeros and ones as we said), to process them by its own rules and to do nothing else.

So how to explain that 1000001 is the number 65, and not the symbol "A" and not the number one million one? And who will explain?

The Easter egg for programmers

The number 1000001 written in the binary system is written as 65 in the decimal system. The number 65 is nothing but the character code "A". We can also perceive 1000001 as one million one. As in quantum physics, nothing is clear before the act of observation. But as soon as we find out which number is a symbol, which one is a binary or a decimal number, everything will immediately fall into place.


To start, we’ll learn just three types of data:

  1. str - character or text data.
  2. int - integers.
  3. bool - logic (boolean) variable.

Memorize them, memorize by all means! And then learn by heart.


CLASSIFI­CATION OF LAN­GUAGES


There are low-level programming languages. There are high-level ones. The difference is that high-level languages use ready-made commands instead of routine, often implemented operations. For example, the print() function, which kindly displays the necessary information on the screen, will consist of 4-6 lines in the low-level Assembly language (it depends on different conditions). And there is only one line for this in Python. Feel the difference.

Python is a high-level programming language that optimizes routine operations.

The high-level languages do routine work for us. On the one hand, it's good because you don't have to waste time writing additional code. On the other hand, it’s bad - there is no full control over the program. On the third hand, it’s also good because it is almost impossible to write a program in a high-level programming language that can break a computer paralyze the operating system work.

We can buy a bar of soap in the store and wash all clothes with it. It will take a long time, will be partly unpleasant and even funny sometimes. But we would wash every spot, every dot. We would control the entire washing process from start to finish. Soap is a low-level washing.

But you can take care of yourself and buy an expensive massive washing machine. Then put clothes inside it, add some laundry detergent and let it snow wash. After finishing just reap the result. And what is happening inside the machine and in which direction is spinning - we do not care at all. Just to make clothes clean! A washing machine is a high-level washing.

Again, we can't clearly answer the question clearly. Which programming languages are better: high- or low-level ones? Each language is used for certain situations. For example, it is better to program devices in Assembler. It requires a little memory, over which the programmer gets full control and works directly with the processor.

Interactive features for sites, all sorts of chat rooms, forums and social networks are for PHP. Or Python.

If you want to write your own Counter-Strike with blackjack and bots, learn C, C++, C# and, to start, the Unity engine.

Yes, you've heard correctly. Each language has its own specialization. But having studied one language, you can easily switch to any other. Don't worry about it. Especially in the modern world, Python is quite relevant due to its plugins (libraries). But that's another story. Focus on learning Python, and everything will work itself out.


STRONG AND WEAK TYPING


Python is a high-level programming language with one interesting feature. While data is received by the program, the interpreter itself determines what kind of data it belongs to. That is, whether it is a string, a number, or a logic variable. We almost don't need to think: as soon as we input data, Python sets a data type by itself.

In other languages, ​​it's a little different. As soon as we enter data into the program, we should mark the data type in the same place - string, short number, long number, integer or fractional number, boolean variable or hyperlink.

This interesting feature of Python is called weak typing.

Weak typing means a more democratic approach to working with data. The programming language determines the type of data as independently as possible and organizes its storage in RAM. Moreover, weak typing supports flexible data conversion tools. For example, if you have to convert a string to a number or vice versa

Nevertheless, we must definitely get to know the data types better. So, what do we have?



str


The text data type is responsible for storing and processing only text data (thanks, Cap). Text data is any data expressed with the characters which a computer can identify. They include, first of all, alphabet symbols: A..Z, a..z, (and any other alphabet symbols of your native language) numbers 0..9, punctuation, special characters and anything included in the encoding table.

An encoding table is a special table in which each character has its own code. Codes are unique, and any character is identified primarily by the code.

The full version of the encoding table can be found through search engines. I present you a part of it. It contains the first 11 capital letters of the English and Russian alphabet and their codes: Character A, code 65 Character А, code 1105 Character B, code 66 Character Б, code 1106 Character C, code 67 Character В, code 1107 Character D, code 68 Character Г, code 1108 Character E, code 69 Character Д, code 1109 Character F, code 70 Character Е, code 1110 Character G, code 71 Character Ж, code 1111 Character H, code 72 Character З, code 1112 Character I, code 73 Character И, code 1113 Character J, code 74 Character Й, code 1114 Character K, code 75 Character К, code 1115

There are global standards for encoding tables. Therefore, we can be sure that the characters displayed on our device will also be displayed on other devices in the same way.

Characters are identified by their codes. It means that upper and lowercase letters are completely different for a computer. For example, character "А" isn't equal to character "а", and the word "WATER" is not equal to the word "Water", although they are similar in meaning.

Even more. The character "А" (in English) and the character "А" (in Russian) are completely different for a computer too, despite the fact they are absolutely identical in style!

The text data must be put in quotation marks. They could be double:

"This is text data"

or single:

'This is also text data'

The main thing is that the opening and the closing quotes must be the same. "text here" or 'text here' (and there is no other way).

Now, here is question for you. Is it text data or not?

"3.14"

Answer

Yes!


The text data type supports the following operations:

Sign Purpose Example Result
+ Concate­nation is addition of lines. It is implemented with the sign +. The result is the attachment of summa­rized line to the source "AB" + "CD" "ABCD"
* Line multipli­cation. The parameter can be only a positive integer. The result is creating a certain number of copies of the string "A" * 5 "AAAAA"

If we decide to divide (/) or to subtract (-) lines and lines or lines and numbers, we'll receive the error, eventually. Try to write in Python Shell:

"New Year" / 2

Press Enter. Explore the result.


int


The integer data type is for integer numbers (easy). The size of processed data directly depends on available RAM of a computer.

The integer data type supports the following operations:

Sign Purpose Example Result
*
/
+
-
Mathe­matical opera­tions of multipli­cation, division, addition and subtrac­tion 10 * 3
10 / 3
10 + 3
10 - 3
30
3.33
13
7
// Integer division (floor division) in which the frac­tional part (remain­der) is dis­carded (isn't rounded) 10 // 3 3
% Division remain­der (modulus, modulo) 10 % 3 1
** Exponentiation 10 ** 3 1000
(
)
Priority of opera­tions, equal to math brackets 10 * (3 + 3) 60

Don't forget! int works only with positive, negative whole numbers (and with zero too).


bool


This is the most interesting data type. It takes one of two logic values: True or False. Only True, only False, and the values are written with a capital letter. Take into account.

Usually, the result of the logical expression, made with comparison operators, is placed in a logical variable.

You entered the room and was asked, "Is the light on?". You could answer this question with “Yes” (True) or “No” (False), and your binary answer is easily described with a boolean variable.

The dialogue may look like this:
- Is the light on?
- True!

What happened right now? You were asked a question, you analyzed the situation, looked at a chandelier or a lamp, compared it with the one turned off and realized that the light is currently on.

The key word is compared. A computer also compares. It compares data, not the light bulbs. The result of the comparison is just a value of a logical type. Either True or False.

The logical (boolean) type bool always takes only one of two values: True or False. The values ​​can be obtained by comparing certain variables ​​with each other or with a constant, or they can be entered by a programmer unconditionally.

Using the following operators you can compare data. Practice in Python Shell. Write, for example, 5 > 5. Press Enter and look at the result.

Sign Purpose Example Result
> Greater than. Truth always comes when the first operand is larger than the second 5 > 3
"A" > "B"
True
False
< Less than. Truth always comes when the second operand is larger than the first 5 < 3
"A" < "B"
False
True
>= Greater than or equal to. Truth always comes when the first operand is larger than or equal to the second 7 >= 7
"C" >= "E"
True
False
<= Less than or equal to. Truth always comes when the second operand is larger than or equal to the first 8 <= 7
"C" <= "C"
False
True
!= Not equal to. Truth always comes when the first operand is not equal to the second 5 != -5
"Z" != "Z"
True
False
== Equals. Truth always comes when the first operand is equal to the second 8 == 7
"Z" == "Z"
False
True

The value True is also considered as 1, and False as 0.

Try this:

3 / True

The result: 3.0

3 / False

The result: "ZeroDivisionError: division by zero".

The result of a logical expression is determined by the comparison operations between two operands. The result is based on the rules of mathematics and logic.


SUMMARY


Python has slightly more data types than those considered here. But we do not need to rush into battle and learn everything. Firstly, you need to understand the basic principles and processing mechanisms of the types presented here; we’ll have time to study, do not hurry. Already much can be done with only a string, an integer and a logical (true/false) data type.

To the string (text, character) data we can apply two operations:

To the integer data type we can apply the following operations:

To the logical result we have the right to apply (is not prohibited by law) the following operations:

Breathe out! You don't have to learn it. As you progress and as you practice, these operations and rules will be applied by you intuitively. You don't think how to write addition sign in the entry "3 + 5", do you? So everything will be fine.

Now just remember where to get the information: in the chapter "Data Types". Maybe you’ll have to come here more often than expected!


© 2019-2024 Viktor Trofimov
© 2019-2024 Translation and adaptation by Danil Shentsov
[ Top page ]