Python Data Types - Numbers, Strings, and Booleans

See this page you are reading. It's a heap of text.
Text, specifically, each character, is a data type, a type of information.

There in the systems of NASA or a bank, there are a number of numbers: account number, how much each client has, how much he borrowed, salary of the employees ... that is, we have another type of data: numbers.

Computing is basically for this: working with data. Everything programming does is it, fiddling with data, information, manipulating, searching, identifying, displaying it, calculating that ... and this is what we will learn now in this tutorial of our Python Tutorial.

In this lesson, we're not going to spy on Trump's bank account, hack into NASA systems, or disrupt Al-Qaeda's plans, as we do in other tutorials. It will be a little more theoretical, but absolutely important for you to become a Python programmer.

Numbers in Python

The most basic and important type of data is undoubtedly numbers.

If the universe were a book, surely it would be written with numbers and language used would be Mathematics.

It is even unnecessary to talk here about the importance of numbers, but you can be sure that there are satellites orbiting through space, being controlled by software, which processes numerical data, including using Python.



Integers Numbers

Integers umbers are those that have no decimal part, such as 0, the positives (1, 2, 3, 4, 5, ..., 2112 ...) and the negative ones (-1, -2, -3, .. ., -2112, ...).

If you only use integers in Python, it will give you integers as a result:
Curso de Python online


Within these, there are those called long integers, to represent really stratospheric values like 111111111111111111111L (have L in the end, large). Depending on what you want to do, you may need to use long integers, such as to discover new prime numbers (very important in data encryption).


Calculate: 11111111111111111111111111L + 222222222222222222222222L



Floating Numbers

They are numbers that float ... joking, nothing to see.
This is what is called the decimal numbers, commonly called 'broken numbers'.

Thus, floating numbers are written like this:
0.5
1.5
21.12


If at least one of your used data is decimal, Python already gives you the decimal result. If you use comma instead of dot, it will give problem or things nothing to see:
Números flutuantes em Python

Other Decimal Bases

We are talking, so far, of numbers in the decimal base, which mankind usually uses.
But there are other types of decimal bases, such as binary, octal and hexadecimal.

Binary numbers start with '0b' at the beginning, octal with '0o' (zero and letter o) and hexes with '0x' at the beginning.

The cool thing is that if you enter a binary, octal or hexadecimal in the interpreter, it already converts automatically to the decimal, see:
Binário, octal e hexadecimal

Notice that the number 10 in the binary system equals the value 2 in our decimal system.

Now you understand and are free to make the following joke: there are 10 types of people, those who understand binary code and those who do not understand.

Free Python tutorials

If you want to know how much a decimal number in binary is, enter bin(x), where x is the number you want to know. For octal, oct(x) and hexadecimal hex(x).

Exercise: In the comments, write your age in the binary, octal and hexadecimal system.

Complex numbers

If you have already done high school, you have certainly heard of the complex numbers (numbers outside the real plane, have real part and imaginary part).

The first part is real, and the one with j (imaginary number) is imaginary.
Calculate the sum of two complex numbers in Python:
(1 + 2j) + (3 + 4j)

What was the result?
Now you can say that you are studying a complex subject in programming.

String - Text in Python

Another important type of data is the strings, which are nothing more than a set of characters.

Its name is a string, its address too, the address of a site is a string, we treat a user's IP as a string. That is, any text or symbol can be treated as a string.

A single character, for example, as 'a' or 'b' is a string.
1 is a number, but '1' is a string.

Adding 1 + 1 in Python, it's ok, after all, it's just two numbers.

Now try to do 1 + '1' (remember: strings are represented in single or double quotation marks), the result will be an error:

Apostila de Python para download

It alerts you that it is not possible to add an integer with a string.
That makes sense, does not it? It's like adding a banana with an apple.

We will devote a whole section of our course to study strings especially, after all, text is something of the utmost importance.

You can get, for example, the HTML code of a page, it's a giant string and try to find there videos, photos, audio and create a program in Python that downloads media. For this, you will have to 'treat' the string, finding the things that matter.

It's something good, but really interesting, working with strings, can do a 'damage', a programmer who knows how to handle them well.

Obviously, you will learn absolutely everything in our Progressive Python Course, no wonder if you start getting work proposals for NASA, White House, Space Station, being a hacker of the Chinese government ...

Booleans - True and False

Surely you have heard that everything in computing, is made by 0 and 1.

Do you know the movies you like to watch? All a combination of 0 and 1.
A picture of you pretending to be meditating on nature? Everything 0 and 1.
Your favorite song? 0 and 1.

In fact, a computer has no idea what numbers, strings or any of that is, it only understands 1 and 0, which is actually an interpretation of voltages (high we call 1, no voltage is 0).

Well, since we're professional Python programmers, let's dig deeper into it.

There is a data type in Python called Boolean, it is very simple, it can only assume two values: True and False.

Ready. Only that.
If you agree that 1 is true, True.
And 0 is false, False.

If you want to compliment someone, say 'you're very 1, so I fell in love'.
Let's see how Python interprets this kind of data.

1 is greater 2? And 3 is greater than 2?

Tutorial de Python

Python is the ideal boyfriend, everything you ask he answers and with sincerity, always speaking the truth.


Exercise: Find out why we call 'boolean', this kind of data.
Yes, a good programmer is one who is able to search, tweak, and discover things by himself.

Another types in Python

Actually, being very specific, data in Python are objects.
You will understand this better when you study object orientation.

When we talk about numbers, strings, characters, booleans ... we talk about some types of data, the main and most used.

However, there are many others that we will even use much (even) later in our Python course, such as:

  • Files
  • Tuples
  • Lists
  • Dictionaries


All of them are basic and internal data.
They are internal because they are already built into Python.

Python is so fucking, but so fucking good, that we'll even be able to create our own kind of data, our own information structure.

If you want 'cheese' to be a data type, as a number is a given, the letter 'A' is a given, then it will be given, and it will be as you want, behave as you wish and period.

If you want the square root of a cheese to be milk, it will be, because you define what your data is and how it behaves.

But for now, let's just leave it as a curiosity.
Let's continue our course ...

No comments:

Post a Comment