የ11ኛ ክፍል የመረጃ ቴክኖሎጂ ምዕራፍ 6
Python data types: when Python converts, and when you must
4 ደቂቃ · የተሠሩ 2 ያለፉ የፈተና ጥያቄዎች
የትምህርቱ ክፍሎች
ትምህርቱን ከዚያ ጀምሮ ለማጫወት አንድ ክፍል ይንኩ።
የ11ኛ ክፍል የመረጃ ቴክኖሎጂ ምዕራፍ 6 የ4 ደቂቃ የ Temari ቪዲዮ ትምህርት፦ Python data types: when Python converts, and when you must። 2 ያለፉ የፈተና ጥያቄዎችን ደረጃ በደረጃ ይሠራል። ትምህርቱ በእንግሊዝኛ ነው።
ስለ ምዕራፍ 6 ተጨማሪ
የምዕራፉ ገጽ Temari በዚህ ምዕራፍ ላይ ያለውን ሌላውን ይዟል፤ ለምሳሌ አጭር ማስታወሻዎችና የመልመጃ ጥያቄዎች ከመልሶቻቸው ጋር።
የምዕራፍ 6 ገጽ ይክፈቱትምህርቱ የሚናገረው ሁሉ
የትምህርቱ ቃላት በየክፍሉ። ትምህርቱን ከዚያ ጀምሮ ለማጫወት ሰዓቱን ይንኩ።
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 1ያለፈ ፈተና
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 2ያለፈ ፈተና
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.
ይህን ምዕራፍ በመተግበሪያው ይለማመዱ
Temari ይህን ምዕራፍ ወደ ጊዜ የተቆረጠለት መልመጃ ይለውጠዋል፣ እየሠሩ ያርምዎታል፤ ያልገባዎትንም የሚያብራራ የ AI አስተማሪ አለ።
በነጻ ይጀምሩTemari.et ለ Android ስልኮች መተግበሪያ አለው፤ በ Google Play ላይ በነጻ ይገኛል።

