Variable type
In addition to the name and value, each variable has its own type. The type shows what values and what operations can be performed with this variable. In addition, the type of the variable shows how to store these variables in memory.
The types we will use most often:
str
- character string
int
- integer (from English
integer )
float
- real number
Unlike other popular programming languages (C ++, Java), the Python translator automatically determines the type of a variable by the value that is assigned to it.
Keying in numeric values
To enter data from the keyboard, we studied the
input ()
operator, but this operator allows you to enter only character strings. We need to indicate that the entered lines must be converted to a number. To do this, we need the built-in
int ()
function - to convert to an integer, or
float ()
- to convert to a real number (we will talk more about real numbers later).
For example,
a = int(input()) # an integer is entered from the keyboard and written to the variable a
b = float(input()) # a real number is entered from the keyboard and written to the variable b
In the program above, the numbers must be entered one per line, because after entering the first value, you must press
Enter to write the number to the variable.
Sometimes you need to enter data on one line.
In order to remember data that is entered on one line, the input line must be divided into values by spaces, using the
split ()
function.
For example, if there are two integers on the same line, you can enter them this way:
a, b = input().split() # We use multiple assignment.
a = int(a) # convert string to integer
b = int(b)
You can replace all these actions with one line:
a, b = map(int, input().split())
the
map
function applies another function (indicated first in parentheses -
int
) to each part obtained after dividing the input string into numbers by spaces
The number of variables on the left must strictly match the number of numbers entered
REMEMBER
1 Python variable type is automatically detected
2 To enter numbers one per line, use
a = int(input())
- for an integer и
b = float(input())
- for real
3 If all numbers are set on one line, then you need to use
split ()
, for example, for two integers
a, b = map(int, input().split())
TRAIN MORE AND ALL YOU WILL GET!