Alterna · учебный материал
Data Types, Input and Print
34.2: Data types, input and print. Short theory, one example and Python stepway.
Что в этом материале
- How it works.
- One example in execution
- Remember
Содержание и основные понятия
Input() always returns the string. For numerical calculation, the string is clearly converted by an int() or float() function.
substance and practice
Grade 8
OGE No. 6
01
How it works.
The connection to the console always comes with a line. The program is required to decide whether it remains a text or whether it needs to be converted into a number before calculating.
str.
The string keeps the symbols. '12 and 12 look alike, but these are different types.
int.
An entire number without a fraction. Int('12') creates 12.
float.
The number with the fraction; the decimal in Python is the point.
Format.
Not only are calculations important, but the exact lines of conclusion that the condition requires are also important.
02
One example in execution
Start the short program and go over the lines. The visualiser only shows the code, current line, variables and output.
input()
Reads one line and returns it.
str
The line is divided to read several numbers.
split()
, convert every fragment
int
The text that cannot be read as a whole causes
ValueError
- it's not a default zero.
Algorithm map
Read from left to right, then run the code.
1
Start
2
Read Two Lines
3
Add as text
4
Transform to int and add
5
Report results
03
Remember
'7' + '2' gives '72, and 7 + 2 gives 9.
Do not mix the line and number by the operator + without the obvious string or int.
An empty line after split() gives an empty list, so unpacking the two numbers will not work.
Syntax of this page
input()
Reads one line and returns the string. Do not add an invitation inside input in the examination code.
int(text)
It converts a row with an entire entry to in; for '3.5' and 'three', it calls ValueError.
float(text)
Turns the line with the dot into a fractional number.
str(value)
Creates a text recording of the value; useful in compiling a line.
split()
Without an argument divides the row by the sequences of the blank symbols.
map(int, parts)
Applys int to each element part; the result can be unpacked or transferred to list.
print(..., sep=..., end=...)
Sep changes the argument divider, end changes the symbol at the end of the conclusion.