You store values in variables and work with the int, float, str, and bool types.
Open this lesson in KodokonA variable is like a labeled box in which you store a value to reuse it later. You create a variable with the = sign, which means "store this value in this box" (and not "is equal to" as in mathematics). On the left, the name of the variable; on the right, the value.
age = 25
price = 19.99
name = "Alice"
is_student = True
print(age)
print(name)Every value has a type, that is, a category that determines what Python can do with it. The four basic types: int for a whole number (25), float for a decimal number (19.99, written with a dot, never a comma), str for a string inside quotation marks, and bool for a boolean, a value that can only be True or False. The type function reveals the type of a value.
print(type(25))
print(type(19.99))
print(type("Alice"))
print(type(True))You often need to convert a value from one type to another. Three functions handle this: int(...) turns a value into an integer, float(...) into a decimal number, str(...) into a string. A classic example: to insert a number into a sentence with +, you first have to convert it into a string.
age = 25
message = "I am " + str(age) + " years old"
print(message)
text_number = "42"
number = int(text_number)
print(number + 8)3.14?x = 5 do?'10' into an integer?