Boqonnaa 6 Teeknooloojii Odeeffannoo kutaa 11
Python data types: when Python converts, and when you must
daqiiqaa 4 · Gaaffii qormaata darbe 2 hojjetaman
Kutaalee barnootaa
Barnoota achii jalqabee taphachiisuuf kutaa tokko tuqi.
Barnoota viidiyoo Temari daqiiqaa 4 boqonnaa 6 Teeknooloojii Odeeffannoo kutaa 11: Python data types: when Python converts, and when you must. Barnoonni kun Afaan Ingiliziitiin.
Dabalata boqonnaa 6
Fuulli boqonnaa kanaa wanta Temari boqonnaa kana irratti qabu kan hafe qaba, fakkeenyaaf yaadannoo gabaabaa fi gaaffii shaakalaa deebii isaanii waliin.
Fuula boqonnaa 6 baniWanta barnoonni jedhu hunda
Jechoota barnootaa kutaa kutaan. Barnoota achii jalqabee taphachiisuuf yeroo tuqi.
0:00Introduction
Every value in Python has a type: a whole number, a decimal, some text. Mix two types, and sometimes Python converts one for you. Sometimes it refuses, and you must convert it yourself. Learn which is which.
0:16Four major data types
A data type says what kind of value a variable holds, and which operations work on it. Python has four major built-in types. int is for integers, whole numbers like thirty four. float is for floating-point numbers, numbers with a decimal point, like four point five. str is a sequence of characters in quotation marks, like Hello world. And bool holds only True or False.
You never write the type down. Python sets it when you assign a value. Assign a whole number, and the variable is an int. To check a variable's type, use the type function.
0:56Implicit: Python converts
When you mix types, Python may convert one. This is type conversion, and it comes in two kinds. The first is implicit conversion, done by Python itself, without you. Say x equals five, an int, and y equals two point five, a float. Now z equals x plus y.
z holds seven point five, and its type is float. Python always converts the smaller type, int, to the larger type, float. Why? To avoid losing data. If z were an int, the point five would be lost, and the value would not be correct.
1:37Practice question 1Qormaata darbe
Here is a practice question. Look at this code. x equals five. y equals two point five. z equals x plus y. What is the data type of z? Pause the video, and try it.
x is an int, and y is a float. Python converts the int to a float, so nothing is lost. z is seven point five, a float. The answer is B. A, int, would lose the point five. C, str, is text, and there is no text here. And D, bool, holds only True or False.
2:22Explicit: you convert
The second kind is explicit conversion. Here you, the programmer, convert the value yourself, with the functions int, float and str. You need them because Python will not convert everything. Python does not allow adding an integer to a string. Try it, and you get an error.
Here is where that catches students. The input function reads what the user types. But it always returns a str, even when the user types a number. So convert it first. x equals input. Then x equals int of x. Now x is a number, and x plus five works.
3:03Practice question 2Qormaata darbe
Try this one. A program asks the user for a number and then adds five to it. The user types a whole number. Which code correctly adds five to the number? Pause, and try it.
The input function returns a str, so it must become an int before you add five. Only C wraps input inside int. The answer is C. A and B add five straight to the string, and that gives an error. D converts to str, and that is still text.
3:41Summary
So remember. Python has four major types: int, float, str and bool. When you mix an int and a float, Python converts the int to a float for you, so no data is lost. But input always returns a str, so convert it yourself before you do maths. Keep practising questions like these on Temari.et.
Boqonnaa kana appii keessatti shaakali
Temari boqonnaa kana gara shaakala yeroon daangeffametti jijjiira; utuu hojjettuu ni sirreessa; wanti si hin galinis barsiisaa AI tiin ni ibsama.
Bilisaan jalqabiTemari.et appii Android qaba; Google Play irratti bilisaan argama.

