Last Updated:

Variable Types in Python

Variable Types in Python

In python, as well as in other languages, there is a set of basic variables, as well as all sorts of operations with them. In this article, we will go through all the basic data types in python, as well as analyze the basic actions with them, let's start.

In python, in addition to the main types of variables, there are also not built-in variables, such variables are added with the connection of frameworks, but we will go through the built-in types.

Built-in types:

  • None
  • Boolean type
  • Numbers
    — int
    — float
    — complex
  • Lists
    — list
    — tuple
    — range
  • Strings
    — str
  • Binary lists
    - bytes
    - bytearray
    - memoryview
  • Sets
    — set
    — frozenset
  • Dictionaries
    — dict

Now about each individually.

None

This type is the variable equivalent of the word null in python. This type of variable has no meaning and no behavior. It is usually used to check a block of code.

Boolean type

The boolean type in python is written as bool. It has only two constant values:

  • True
  • False

In the context of numbers, True represents 1 and False represents 0. This type of variable is required to represent truth. It is also worth saying about logical operators. In python, there are 3 such operators:

  • and
  • or
  • not

Each operator compares the conditions differently, let's go through each one.

And

Two expressions are written, the operator and is placed between them, and if at least one expression is false, then the entire expression is false. Therefore, for an expression to be true, all expressions must be true.

Or

Between the two expressions is the sign or, and if one of the expressions is true, then the whole expression is true. If both expressions are true the expression will also be completely true. The entire value will only be false if both expressions are false.

Not

This operator is needed for negation.If we want to make some expression negative, then the not operator is used.

Number

So we got to the numbers. In python, there are only three numeric types: Int, float, complex.

Int

A simple integer data type. This type includes values of integers, for example, 0,1,2,3,4,5... 2 147 483 647. Also values with a minus sign, but which are integers.

The values of variables of this type can be added to each other (+), subtracted (-), divided (/), multiplied (*), the same is inherent in other numeric types.

Float

Float is a floating-point numeric type, in other words, it contains fractional numbers. It is also called the material type. It can contain such numbers as: 0.2, 1.5, 24.34.
By the way, it can also contain integers, but they will be written a little differently than in the int type.

0.0, 1.0, 2.0, 3.0...

Well, as mentioned earlier, various numerical operations can be carried out with them:

  • Fold(+)
  • subtract(-)
  • multiply(*)
  • divide(/)

Complex

The complex data type is a bit more complex than its brethren. Complex numbers are numbers of the form a+bj, where a and b are float or int numbers, and j is an imaginary unit. Here's what complex numbers might look like:

5 + 9j
100.12 + 50j
0.33 + 0.67j
23 + 0.45j

Line

Another important type is strings. This type stores text information. They are very often used. A variable of this type has the designation str. The value of such a variable is enclosed in quotation marks. Therefore, the value of such a variable can be as follows:

“Hello world!”
"Test 1, Test 2, Test 3"
"Good afternoon, "

You can also add lines.

Operations on variables

We have studied the main types of variables, which means that now you can learn how to perform various conversion, conversion and other operations with them. Start.

Conversion

Let's look at the conversion of variable values. There are times during the program when one type of variable needs to be converted to another in order to fulfill a condition. To do this, there are the following functions:

  • int()
  • str()
  • float()

Let's look at a small example:

Test = "Any line"
number = 3
print(Test + number)

This code will give an error, because we have added up variables of completely different types. To add them, you need to translate one of the types into another. A text string cannot be converted to a numeric string, so only the option with a number that can be converted to a string remains. To do this, use the str() function.

The program will take the following form:

Test = "Any line"
number = 3
print(Test + str(number))

Результат:

Any line 3

This is how type conversion can solve such problems.

Tasks

Well, here is a small task for mastering the material:

Print( int( “4” + ”5” ) )

What do you think will bring this code to the console?

The correct answer is the number 45, because first the program adds the lines, in this case it flattens line 4 and 5, you get 45, and then converts to a number.

The next task, what will bring this code to the console?

Print (float(“210” * int (”2”) ) )

Of course the correct answer would be 210210.0

Now an explanation. The number 2 goes from row to number. This number is multiplied by line 210 twice, that is, you need to enter this line twice, and you get the answer 210210, but according to the condition, this number must be converted to float. Therefore, we add the fractional part of the number.

Variable names

Let's talk a little bit about the names of variables. It's pretty simple. Variable names consist of English letters, underscores, and numbers. It is important that the name of the variable does not begin with a number or underscore, such a variable the program simply will not count.

Tasks

Which of the following variables will not count with the program?

Bad_Value
_Good_Velue
360hello
test1

This is the second and third name, as one begins with an underscore and the other begins with a number.

But beyond all this, variable names in the python register are sensitive. That is, variables named Test and test are completely different variables, although their only difference is that the first letter is written in different case. You can create a third tEst variable, which is also a completely different variable.
This is important to know.

Total

In this article, we analyzed the basic data types, studied the conversion of data, and also learned about the features of variable names.

That's it, thanks for reading. Interesting projects and good mood!