Data types in Python
Data in a computer is of different kinds: numbers, text, true or false values. So every value has its own type: int is a whole number (15), float is a real number (20.5, the fractional part is written with a dot), str is a string in quotes (“Lola”), bool is a logical value (True or False, with a capital first letter). In Python you do not have to declare a type beforehand: the type of a variable is found from the value it receives, and the type() function shows it. When the type must be changed, int(), float() or str() is used. An important case: input() returns whatever the user types as a string (str); so to add 1 to a typed 15 you must first convert it with int(), otherwise a TypeError appears. Do not forget that "15" in quotes is text, not a number.
| Type | Meaning | Example |
|---|---|---|
| int | whole number | 15 |
| float | real number | 20.5 |
| str | string (text) | "Lola" |
| bool | logical value | True, False |
Pupils saw the number 8 and the text “8” in quotes on a list: they look alike, but a sum works only with the first. A program also tells numbers and text apart.
Programmers, database staff and accountants classify data correctly as numbers or text.
Write five facts about your classroom, such as the number of windows or the name of the school, and mark each as “number” or “text”. Paper is enough.
1yosh = 15 2narx = 20.5 3ism = "Lola" 4ok = True 5print(type(yosh)) 6print(type(narx)) 7print(type(ism)) 8print(type(ok))
<class 'int'> <class 'float'> <class 'str'> <class 'bool'>
1s = input() 2print(int(s) + 1)
16
“Type sorting”: each group writes a value on 12 cards (7, 7.0, "7", True, -3, "Lola", 0.5, False, "3.14", 100, "", 2.0) and sorts them into the int, float, str, bool boxes.