AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Contents · Computing

Data types and a practical lesson

Lessons 24–25 · 2 lessons · M. R. Fayziyeva, D. M. Sayfurov, N. S. Xaytullayeva. Informatics and Information Technologies, Grade 9. “Nashriyot uyi Tasvir”, Tashkent, 2020
24

Data types in Python

Textbook: pp. 59–61
GoalTells the data types (int, float, str, bool) apart, finds the type of a value with type(), and converts a string read with input() into a number with int().
New words
data type: the form of the data in a variable or constant; it decides how much memory is used and which operations are allowed · ma’lumotlar turiint is a whole number, float is a real number with a decimal point · int va floatstr: a string, text placed in quotes · strbool: a logical type with only two values, True and False · bool
Explanation

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.

TypeMeaningExample
intwhole number15
floatreal number20.5
strstring (text)"Lola"
boollogical valueTrue, False
💡 Where will I use this?
In real life

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.

Who uses it

Programmers, database staff and accountants classify data correctly as numbers or text.

Try it today

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.

Worked examples
Python
1yosh = 15
2narx = 20.5
3ism = "Lola"
4ok = True
5print(type(yosh))
6print(type(narx))
7print(type(ism))
8print(type(ok))
Output
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
Seeing the types with type() – code above. The screen shows in turn <class 'int'>, <class 'float'>, <class 'str'>, <class 'bool'>.
Python
1s = input()
2print(int(s) + 1)
Output
16
Converting the typed string to a number – code above. If 15 is typed, 16 is printed. Without int(), print(input() + 1) gives a TypeError, because a number cannot be added to a string.
Class activity

“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.

Practice
1
Name the types of the values 579, "579", 5.79 and False.
2
What does print(type(3.0)) print on the screen?
3
Write a program that reads a whole number and prints it plus 1.
4
Why does input() + 1 give an error?
Story of the Week · The Khwarizmi WayNew Year ordersClose the week: all of this week’s subjects in one storyRead →