PythonPython · Chapterبەش 4 · Lectureوانە 4.2
Operators and Expressionsکارپێکەرەکان و دەربڕینەکان
Calculate with Python: the seven arithmetic operators, the three kinds of division, powers, operator precedence, short forms such as +=, + and * with text, the tiny errors of decimal numbers and round(), and a first look at the math module.
هەژمارکردن بە Python: حەوت ، سێ جۆرەکەی ، توان، ، شێوە کورتەکانی وەک +=، + و * لەگەڵ دەق، هەڵە بچووکەکانی و round()، لەگەڵ یەکەم سەیرکردنی math.
What you will learnچی فێر دەبیت
- Use the seven arithmetic operators and predict the type of each result.
- Choose between
/,//and%, and use//and%to split numbers into parts. - Work out an expression by hand using operator precedence, and add parentheses where they are needed.
- Update variables with
+=,-=,*=and the other short forms. - Explain why floats are not exact, round results for display, and compare floats safely.
- حەوت بەکار بهێنیت و جۆری هەر ئەنجامێک پێشبینی بکەیت.
- لە نێوان
/،//و%ـدا هەڵبژێریت و//و%بۆ دابەشکردنی ژمارە بۆ بەشەکانی بەکار بهێنیت. - بە دەست بەپێی هەژمار بکەیت و لە شوێنی پێویستدا کەوانە زیاد بکەیت.
- بە
+=،-=،*=و شێوە کورتەکانی تر نوێ بکەیتەوە. - ڕوونی بکەیتەوە بۆچی ورد نین، ئەنجامەکان بۆ پیشاندان نزیک بکەیتەوە و بە سەلامەتی بەراورد بکەیت.
Expressions: Python as a calculatorدەربڕینەکان: Python وەک حاسیبە
Programs calculate all the time. A shop adds up a bill, a school works out an average mark, a game counts points. In Python you write a calculation as an expression: values joined by operators, such as 3 * 500 + 250. Python works the expression out and gets one value.
هەمیشە هەژمار دەکەن. دوکانێک کۆی پسوولەیەک دەردەهێنێت، قوتابخانەیەک تێکڕای نمرەکان دەدۆزێتەوە، یارییەک خاڵەکان دەژمێرێت. لە Pythonـدا هەژمارێک وەک دەنووسیت: چەند کە بە پێکەوە بەستراون، وەک 3 * 500 + 250. Python هەژمار دەکات و یەک بەدەست دەهێنێت.
A waiter writes on his pad: 3 teas at 500 dinars and one kulicha at 250. Before you pay, he works it out: , then . Python does the same with 3 * 500 + 250: it reads the whole expression and reduces it, step by step, to one number.
چایچییەک لەسەر کاغەزەکەی دەنووسێت: 3 چا بە 500 دینار و کولیچەیەک بە 250. پێش ئەوەی پارە بدەیت هەژماری دەکات: ، پاشان . Python هەمان کار بە 3 * 500 + 250 دەکات: هەموو دەخوێنێتەوە و هەنگاو بە هەنگاو کورتی دەکاتەوە تا دەبێتە یەک ژمارە.
Where the analogy stops workingسنووری ئەم نموونەیە
The waiter may add the items in any order he likes. Python follows fixed rules about which operator goes first; you will learn them in this lesson.
چایچییەکە دەتوانێت بە هەر ڕیزبەندییەک بیەوێت کۆیان بکاتەوە. Python یاسای جێگیر پەیڕەو دەکات کە کام یەکەم کار دەکات؛ لەم وانەیەدا فێریان دەبیت.
>>> 3 * 500 + 250
1750
>>> 1750 / 2
875.0Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-2
At the
>>>prompt the shell shows the value of an expression at once; in a script you needprint().لە بەردەم
>>>ـدا شێڵەکە یەکسەر پیشان دەدات؛ لە پێویستت بەprint()هەیە.
Expressions: Python as a calculatorدەربڕینەکان: Python وەک حاسیبە
Operators and operandsکارپێکەرەکان و کارپێکراوەکان
The symbols +, -, * and / are operators. The values they work on are their operands. In 3 * 500, the operator is * and the operands are 3 and 500. An expression can be a single value (42), a variable (price) or many parts joined together ((price + tax) * 2).
هێماکانی +، -، * و / . ئەو کاریان لەسەر دەکەن ئەوانن. لە 3 * 500ـدا *ـە و 3 و 500ـن. دەکرێت تەنها یەک بێت (42)، بێت (price) یان چەند بەشێک بێت کە پێکەوە بەستراون ((price + tax) * 2).
Most operators stand between two operands, as in a - b; they are called binary operators (bi means two). A few take only one operand: in -x the minus sign flips the sign of x. That is a unary operator (uni means one). So the same symbol - can do two jobs: a - b subtracts, -x makes a number negative.
زۆربەی لە نێوان دوو دەنووسرێن، وەک a - b؛ پێیان دەوترێت (بە ئینگلیزی binary؛ bi واتە دوو). هەندێکیان تەنها یەک هەیە: لە -xـدا نیشانەی کەم نیشانەی x پێچەوانە دەکاتەوە. ئەمە پێی دەوترێت (بە ئینگلیزی unary؛ uni واتە یەک). کەواتە هەمان هێمای - دوو کار دەکات: a - b لێدەردەکات و -x ژمارەیەک دەکات بە .
x = 7
print(-x) # unary minus: one operand
print(10 - x) # binary minus: two operands-7 3
The seven arithmetic operatorsحەوت کارپێکەرە ژمێرەییەکە
Python has seven arithmetic operators. Here they all work on the same two numbers, 17 and 5, so you can compare them.
لە Pythonـدا حەوت هەیە. لێرەدا هەموویان لەسەر هەمان دوو ژمارە کار دەکەن، 17 و 5، بۆ ئەوەی بەراوردیان بکەیت.
| Operator | Nameناو | Exampleنموونە | Resultئەنجام |
|---|---|---|---|
+ | Additionکۆکردنەوە | 17 + 5 | 22 |
- | Subtractionلێدەرکردن | 17 - 5 | 12 |
* | Multiplicationلێکدان | 17 * 5 | 85 |
/ | Division (always a float) (هەمیشە ) | 17 / 5 | 3.4 |
// | Floor division (whole part) (بەشی تەواو) | 17 // 5 | 3 |
% | Remainder (modulus) | 17 % 5 | 2 |
** | Power (exponentiation) | 17 ** 2 | 289 |
a = 17
b = 5
print("17 + 5 =", a + b)
print("17 - 5 =", a - b)
print("17 * 5 =", a * b)
print("17 / 5 =", a / b)
print("17 // 5 =", a // b)
print("17 % 5 =", a % b)
print("17 ** 2 =", a ** 2)17 + 5 = 22 17 - 5 = 12 17 * 5 = 85 17 / 5 = 3.4 17 // 5 = 3 17 % 5 = 2 17 ** 2 = 289
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-2
Two variables hold the numbers, so every line uses the same values.
دوو ژمارەکان هەڵدەگرن، بۆیە هەموو دێڕەکان هەمان بەکار دەهێنن.
- Lineدێڕی 3-9
print()shows a label (text in quotes) and then the value of the expression after the comma.print()دەقێکی ڕوونکەرەوە (دەقی ناو ) و دوای کۆماکە (,) پیشان دەدات.
Spaces around operators do not change the result, but they make code easier to read: write 17 + 5, not 17+5.
بۆشایی لە دەوری ئەنجام ناگۆڕێت، بەڵام خوێندنەوەی کۆد ئاسانتر دەکات: بنووسە 17 + 5، نەک 17+5.
In Kurdish: working with numbers in Python — arithmetic and simple maths functions.
بە کوردی: کارکردن لەگەڵ ژمارە لە : و سادەی بیرکاری.
Kurdish video: working with numbers and maths in Python.
ڤیدیۆیەکی کوردی: کارکردن لەگەڵ ژمارە و بیرکاری لە .
Three kinds of division: /, // and %سێ جۆر دابەشکردن: /، // و %
Python has two ways to divide, and one way to get what is left over:
Python دوو ڕێگای دابەشکردنی هەیە و ڕێگایەکیش بۆ ئەوەی بزانیت چی دەمێنێتەوە:
/is true division. Its answer is always a , even when the division comes out even:10 / 2is5.0, not5.//is floor division. It divides and then rounds down to a whole number:7 // 2is3.%gives the remainder (also called the modulus): what is left after the whole groups are taken out.7 % 2is1.
/دابەشکردنی ڕاستەقینەیە. ئەنجامەکەی هەمیشە ، تەنانەت کاتێک دابەشکردنەکە تەواو دەردەچێت:10 / 2دەکاتە5.0، نەک5.//. دابەش دەکات و پاشان ئەنجامەکە بەرەو خوار دەکات بە :7 // 2دەکاتە3.%دەدات: ئەوەی دوای دەرهێنانی کۆمەڵە تەواوەکان دەمێنێتەوە.7 % 2دەکاتە1.
// counts the full bags, % counts what is left.دانانی 17 شیرینی لە کیسەی 5 دانەییدا: // کیسە پڕەکان دەژمێرێت و % ئەوەی دەمێنێتەوە.CêIT// and % always fit together: (quotient × divisor) + remainder gives back the number you started with.
// و % هەمیشە پێکەوە گونجاون: (ئەنجامی // × دابەشکەر) + هەمان ژمارەی سەرەتا دەداتەوە.
print(10 / 2)
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(divmod(17, 5))5.0 3.5 3 1 (3, 2)
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Even
10 / 2gives a float:5.0.تەنانەت
10 / 2ـش دەدات:5.0.- Lineدێڕی 5
divmod()is a built-in function that gives both answers at once: the result of//and the result of%.divmod()کە هەردوو ئەنجامەکە پێکەوە دەدات: ئەنجامی//و ئەنجامی%.
Three kinds of division: /, // and %سێ جۆر دابەشکردن: /، // و %
What the remainder is good forپاشماوە بۆ چی باشە؟
% looks like a small operator, but programmers use it every day:
% وەک بچووک دەردەکەوێت، بەڵام هەموو ڕۆژێک بەکاری دەهێنن:
- Even or odd? A number is even when
n % 2is0, and odd when it is1. You will use this in if statements. - Splitting units. 135 minutes is
135 // 60hours and135 % 60minutes. - Digits of a number.
n % 10is the last digit, andn // 10removes it. The binary-to-decimal project works with digits in the same spirit.
- جووت یان تاک؟ ژمارەیەک جووتە کاتێک
n % 2یەکسانی0بێت و تاکە کاتێک1بێت. ئەمە لە ڕستەکانی if بەکار دەهێنیت. - دابەشکردنی یەکەکان. 135 خولەک دەکاتە
135 // 60کاتژمێر و135 % 60خولەک. - ژمارەیەک.
n % 10دوایین وn // 10لای دەبات. پڕۆژەی دووانی بۆ دەیی بە هەمان بیرۆکە کار لەگەڵ دەکات.
// و %remainder_uses.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسەminutes = 135
hours = minutes // 60 # whole hours
rest = minutes % 60 # minutes left over
print(minutes, "minutes =", hours, "h", rest, "min")
number = 347
print("Last digit:", number % 10)
print("Without the last digit:", number // 10)
n = 14
print(n % 2) # 0 means n is even
n = 9
print(n % 2) # 1 means n is odd135 minutes = 2 h 15 min Last digit: 7 Without the last digit: 34 0 1
Three kinds of division: /, // and %سێ جۆر دابەشکردن: /، // و %
Extra: dividing negative numbersزیاتر: دابەشکردنی ژمارە سالبەکان
Powers with **بەرزکردنەوە بۆ توان بە **
** raises a number to a power: 2 ** 10 means 2 multiplied by itself 10 times. In maths you write it . The number on the left is the base, the number on the right is the exponent.
** ژمارەیەک بەرز دەکاتەوە بۆ : 2 ** 10 واتە 2 دە جار لە خۆی لێک بدرێت. لە بیرکاریدا دەینووسیت . ژمارەی لای چەپ ـە و ژمارەی لای ڕاست ـە.
print(2 ** 10)
print(5 ** 3)
print(9 ** 0.5)
print(2 ** -1)
print(2 ** 100)1024 125 3.0 0.5 1267650600228229401496703205376
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
5 ** 3is the volume of a cube with sides of 5: the functions chapter builds acube_volumefunction with exactly this expression.5 ** 3قەبارەی مکەعەبێکە (سندوقێک کە درێژی، پانی و بەرزیی یەکسانن) کە هەر لایەکی 5ـە: بەشیcube_volumeبە هەمان ئەم دروست دەکات.- Lineدێڕی 3
A power of 0.5 is a square root:
9 ** 0.5is3.0.0.5 :
9 ** 0.5دەکاتە3.0.- Lineدێڕی 4
A negative exponent divides:
2 ** -1is .سالب دابەش دەکات:
2 ** -1دەکاتە .- Lineدێڕی 5
Python integers have no fixed size limit (only the computer's memory limits them), so
2 ** 100is printed exactly.لە Pythonـدا سنوورێکی جێگیری قەبارەیان نییە (تەنها سنووریان بۆ دادەنێت)، بۆیە
2 ** 100بە وردی چاپ دەکرێت.
Which operator goes first?کام کارپێکەر یەکەم کار دەکات؟
When an expression has several operators, Python does not simply work from left to right. It follows operator precedence: a fixed ranking of the operators, like the one you learned at school.
کاتێک چەند هەیە، Python تەنها لە چەپەوە بۆ ڕاست کار ناکات. پەیڕەو دەکات: ڕیزبەندییەکی جێگیری ، وەک ئەوەی لە قوتابخانە فێری بوویت.
The rules that decide which operator runs first when an expression has several. * and / come before + and -, so 2 + 3 * 4 is 14; use parentheses to change the order.
ئەو یاسایانەن کە دیاری دەکەن کام یەکەم جێبەجێ دەکرێت کاتێک چەند کارپێکەرێکی تێدایە. * و / پێش + و - دێن، بۆیە 2 + 3 * 4 دەکاتە 14؛ بۆ گۆڕینی ڕیزبەندییەکە کەوانە بەکاربهێنە.
- Parentheses first. Anything inside
( )is worked out before everything else. - Then
**, then a sign in front of a number (-x), then*,/,//and%, and last+and-. - Same row: left to right.
10 - 4 - 3means(10 - 4) - 3, which is3. - Except
**, which goes right to left:2 ** 3 ** 2means2 ** 9, which is512.
- یەکەم کەوانە. هەرچی لەناو
( )ـدایە پێش هەموو شتێک هەژمار دەکرێت. - پاشان
**، پاشان نیشانەی پێش ژمارە (-x)، پاشان*،/،//و%، لە کۆتاییشدا+و-. - هەمان ڕیز: لە چەپەوە بۆ ڕاست.
10 - 4 - 3واتە(10 - 4) - 3، کە دەکاتە3. - جگە لە
**کە لە ڕاستەوە بۆ چەپ دەڕوات:2 ** 3 ** 2واتە2 ** 9، کە دەکاتە512.
Which operator goes first?کام کارپێکەر یەکەم کار دەکات؟
Watch Python work it outسەیری Python بکە کە چۆن هەژماری دەکات
Python keeps replacing the part with the highest precedence by its value, until only one value is left. Here is 2 + 3 * 4 ** 2:
Python بەردەوام ئەو بەشە بە دەگۆڕێت کە بەپێی لە هەمووان پێشترە، تا تەنها یەک دەمێنێتەوە. با 2 + 3 * 4 ** 2 ببینین:


The same three steps written as maths.
هەمان سێ هەنگاو بە شێوەی بیرکاری.
Which operator goes first?کام کارپێکەر یەکەم کار دەکات؟
Precedence trapsتەڵەکانی پێشینەیی کارپێکەرەکان
print(2 + 3 * 4 ** 2)
print((2 + 3) * 4 ** 2)
print(-3 ** 2)
print((-3) ** 2)
print(2 ** 3 ** 2)
print(10 - 4 - 3)
print(100 / 10 / 5)50 80 -9 9 512 3 2.0
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-2
Parentheses change the order: 50 without them, 80 with them.
کەوانە ڕیزبەندی دەگۆڕێت: بەبێ کەوانە 50، بە کەوانە 80.
- Lineدێڕی 3-4
**is done before the minus sign, so-3 ** 2is . Put the minus inside parentheses to square .**پێش نیشانەی کەم ئەنجام دەدرێت، بۆیە-3 ** 2دەکاتە . بۆ دووجاکردنی نیشانەی کەم بخەرە ناو کەوانەوە.- Lineدێڕی 5
**goes right to left: first3 ** 2is9, then2 ** 9is512.**لە ڕاستەوە بۆ چەپ دەڕوات: یەکەم3 ** 2دەکاتە9، پاشان2 ** 9دەکاتە512.- Lineدێڕی 6-7
Other operators of the same row go left to right:
(10 - 4) - 3and(100 / 10) / 5.تری هەمان ڕیز لە چەپەوە بۆ ڕاست دەڕۆن:
(10 - 4) - 3و(100 / 10) / 5.
The most common real bug is the average. To average two numbers you must add them before dividing:
باوترین ڕاستەقینە تێکڕایە. بۆ تێکڕای دوو ژمارە دەبێت پێش دابەشکردن کۆیان بکەیتەوە:
a = 8
b = 6
print(a + b / 2) # wrong: only b is divided
print((a + b) / 2) # right: the average11.0 7.0
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
/is done first, so this is8 + 3, which gives11.0— a logic error: no message, just a wrong answer./یەکەم ئەنجام دەدرێت، بۆیە ئەمە دەبێتە8 + 3، واتە11.0— : هیچ پەیامێک نییە، تەنها وەڵامێکی هەڵە.- Lineدێڕی 4
With parentheses the sum is divided:
(8 + 6) / 2gives7.0.بە کەوانە کۆکەیە کە دابەش دەکرێت:
(8 + 6) / 2دەکاتە7.0.
Mixing whole numbers and decimalsتێکەڵکردنی ژمارە تەواوەکان و ژمارە کەرتییەکان
Python has two kinds of numbers you met in Variables and data types: int (whole numbers such as 7) and float (numbers with a decimal point such as 7.5). When an operator gets one of each, Python turns the int into a float first, so the answer is a float.
Python دوو جۆر ژمارەی هەیە کە لە گۆڕاوەکان و جۆرەکانی دراوە ناسیتن: int (، وەک 7) و float (ژمارەی خاوەن ، وەک 7.5). کاتێک یەکێک لە هەر دووکیانی پێ دەدرێت، Python سەرەتا int دەگۆڕێت بۆ float، بۆیە ئەنجامەکە float دەبێت.
print(4 * 2.5)
print(7 + 1.0)
print(10 / 5)
print(type(10 / 5))
print(int(7.9))10.0 8.0 2.0 <class 'float'> 7
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-2
An int and a float give a float:
10.0and8.0.int و float پێکەوە float دەدەن:
10.0و8.0.- Lineدێڕی 3-4
/gives a float even with two ints;type()shows it./تەنانەت بە دوو intـیش float دەدات؛type()پیشانی دەدات.- Lineدێڕی 5
int()cuts off the decimal part (it does not round): 7.9 becomes 7.int()بەشی کەرتی دەبڕێت ( ناکات): 7.9 دەبێتە 7.
Short forms: +=, -= and friendsشێوە کورتەکان: +=، -= و هاوشێوەکانیان
Programs often change a variable using its own old value: add 1 to a counter, add a price to a total. count = count + 1 works, but Python has a shorter way to write it: count += 1. This is called augmented assignment, and every arithmetic operator has one.
زۆرجار بە کۆنەکەی خۆی دەگۆڕن: زیادکردنی 1 بۆ ، زیادکردنی نرخێک بۆ کۆی گشتی. count = count + 1 کار دەکات، بەڵام Python ڕێگایەکی کورتتری هەیە: count += 1. ئەمە شێوەیەکی کورتی و هەموو یەکێکی هەیە.
score = 10
score += 5 # score = score + 5
print(score)
score *= 2 # score = score * 2
print(score)
score -= 4 # score = score - 4
print(score)
score //= 3 # score = score // 3
print(score)
score **= 2 # score = score ** 2
print(score)
score %= 10 # score = score % 10
print(score)15 30 26 8 64 4
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
score += 5reads the old value (10), adds 5 and stores 15 back inscore.score += 5کۆنەکە (10) دەخوێنێتەوە، 5ـی بۆ زیاد دەکات و 15 دەخاتەوە ناوscore.- Lineدێڕی 8
//=and%=work the same way with floor division and remainder.//=و%=بە هەمان شێوە لەگەڵ و کار دەکەن.
| Lineدێڕ | Statement | score after itscore دوای ئەو |
|---|---|---|
| 1 | score = 10 | 10 |
| 2 | score += 5 | 15 |
| 4 | score *= 2 | 30 |
| 6 | score -= 4 | 26 |
| 8 | score //= 3 | 8 |
| 10 | score **= 2 | 64 |
| 12 | score %= 10 | 4 |
The course's own programs use this all the time: a quiz program counts matching numbers with count += 1 inside a loop. You will write many such loops in the while loop lesson.
خودی کۆرسەکە هەمیشە ئەمە بەکار دەهێنن: بەرنامەیەکی کویز ژمارە گونجاوەکان بە count += 1 لەناو دەژمێرێت. لە وانەی خولی while زۆر لەم جۆرە دەنووسیت.
+ and * with text+ و * لەگەڵ دەق
Two arithmetic operators also work on (text). + joins two strings into one: this is called concatenation. * with a whole number repeats a string.
دوو لەسەر (دەق) کار دەکەن. + دوو پێکەوە دەلکێنێت: پێی دەوترێت . * لەگەڵ دووبارە دەکاتەوە ().
print("Ha" * 3)
print("-" * 20)
print("5" + "5")
print(5 + 5)
greeting = "Good morning, " + "Aram"
print(greeting)HaHaHa -------------------- 55 10 Good morning, Aram
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
"-" * 20draws a line of 20 dashes. Lab 1's multiplication table draws its line under the header in exactly this way."-" * 20هێڵێک لە 20 داش دەکێشێت. خشتەی لێکدانی تاقیگەی 1 هێڵی ژێر سەردێڕەکەی ڕێک بەم شێوەیە دەکێشێت.- Lineدێڕی 3-4
"5" + "5"joins two pieces of text and gives55;5 + 5adds two numbers and gives10. The quotes decide."5" + "5"دوو پارچە دەق دەلکێنێت و55دەدات؛5 + 5دوو ژمارە کۆ دەکاتەوە و10دەدات. بڕیار دەدەن.
Python never mixes text and numbers on its own. "Age: " + 21 stops the program with a TypeError:
Python هەرگیز خۆی دەق و ژمارە تێکەڵ ناکات. "Age: " + 21 بە TypeError دەوەستێنێت:
age = 21
print("Age: " + age)Traceback (most recent call last):
File "string_error.py", line 2, in <module>
print("Age: " + age)
~~~~~~~~^~~~~
TypeError: can only concatenate str (not "int") to strage = 21
print("Age: " + str(age))
print("Age:", age)Age: 21 Age: 21
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
str(age)turns the number into text first, then+can join.str(age)سەرەتا ژمارەکە دەکات بە دەق، پاشان+دەتوانێت بیلکێنێت.- Lineدێڕی 3
Or let
print()do it: values separated by commas are printed with a space between them.یان با
print()بیکات: ئەو بە کۆما (,) جیا کراونەتەوە، بە بۆشاییەک لە نێوانیاندا چاپ دەکرێن.
There is a third, even better way, the f-string, in Input and output. Strings have many more tools, which you will meet in Working with strings.
ڕێگای سێیەم کە باشتریشە، ، لە نێوخستە و دەرخستە دایە. ئامرازی زۆر زیاتریان هەیە کە لە کارکردن لەگەڵ زنجیرەنووسەکان دەیانناسیت.
Decimal surprises and round()سەرسوڕمانی ژمارە کەرتییەکان و round()
Try 0.1 + 0.2 in Python. You expect 0.3, but you get something slightly different:
0.1 + 0.2 لە Pythonـدا تاقی بکەرەوە. چاوەڕێی 0.3 دەکەیت، بەڵام شتێکی کەمێک جیاواز وەردەگریت:
0.1 + 0.2 is not exactly 0.30.1 + 0.2 ڕێک 0.3 نییەfloats.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسەprint(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
print(round(0.1 + 0.2, 2))
import math
print(math.isclose(0.1 + 0.2, 0.3))0.30000000000000004 False 0.3 True
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
So never test floats with
==: the tiny error makes the testFalse.بۆیە هەرگیز بە
==تاقی مەکەرەوە: ئەو هەڵە بچووکە تاقیکردنەوەکە دەکات بەFalse.- Lineدێڕی 3
round(x, 2)keeps two decimal places.round(x, 2)دوو دەهێڵێتەوە.- Lineدێڕی 5-6
math.isclose()asks “are these two floats almost equal?” — the safe way to compare.math.isclose()دەپرسێت «ئایا ئەم دوو ژمارەیە نزیکەی یەکسانن؟» — ڕێگای سەلامەتی بەراوردکردن.
Write as a decimal on paper: You must stop somewhere, so your number is a tiny bit wrong. A computer stores numbers in binary (0s and 1s), and in binary even 0.1 never ends. Python keeps the closest value it can, so small errors appear.
لەسەر کاغەز بە ژمارەی بنووسە: دەبێت لە شوێنێکدا بوەستیت، بۆیە ژمارەکەت کەمێک هەڵەیە. ژمارەکان بە (0 و 1) هەڵدەگرێت و لە تەنانەت 0.1ـیش کۆتایی نایەت. Python نزیکترین هەڵدەگرێت کە بتوانێت، بۆیە هەڵەی بچووک دەردەکەون.
Where the analogy stops workingسنووری ئەم نموونەیە
On paper you choose how many digits to keep. The computer always keeps the same number of binary digits (about 16 decimal digits), whatever the number.
لەسەر کاغەز خۆت هەڵدەبژێریت چەند بهێڵیتەوە. هەمیشە هەمان ژمارە هەڵدەگرێت (نزیکەی 16 )، ژمارەکە هەرچییەک بێت.
Why 0.7 + 0.6 does not print exactly 1.3: computers store decimals in binary, so floats are approximations.
بۆچی Python بۆ 0.7 + 0.6 ژمارەی 1.2999999999999998 پیشان دەدات: ژمارەی بە هەڵدەگرێت، بۆیە زۆر ژمارەی سادە تەنها نزیکەیین.
Whole video (5:46).
هەموو ڤیدیۆکە (5:46).
Decimal surprises and round()سەرسوڕمانی ژمارە کەرتییەکان و round()
The tail in Lab 3's sumکلکەکەی ناو کۆی تاقیگەی 3
Lab 3 asks for the sum and average of ten numbers. Adding them one by one shows the same effect in the middle of the work:
تاقیگەی 3 داوای کۆ و تێکڕای دە ژمارە دەکات. کۆکردنەوەیان یەک لە دوای یەک هەمان دیاردە لە ناوەڕاستی کارەکەدا پیشان دەدات:
part = 2.7 + 32 + 768 + 89.1 # the first four numbers of Lab 3
print(part)
print(round(part, 2))
total = part + 21 + 90.3 + 135 + 77 + 43 + 38.2
print(total)
print(total / 10)891.8000000000001 891.8 1296.3 129.63
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
After four numbers the total shows a tail:
891.8000000000001.دوای چوار ژمارە کۆکە کلکێکی هەیە:
891.8000000000001.- Lineدێڕی 3
round(part, 2)shows the clean value.round(part, 2)پاکەکە پیشان دەدات.- Lineدێڕی 6
The final average happens to print cleanly: 129.63.
تێکڕای کۆتایی بە ڕێکەوت پاک چاپ دەبێت: 129.63.
Decimal surprises and round()سەرسوڕمانی ژمارە کەرتییەکان و round()
round() step by stepround() هەنگاو بە هەنگاو
round(number, digits) gives a number with that many digits after the point. Without digits it gives a whole number (an int).
round(number, digits) ژمارەیەک دەدات کە ئەوەندە دوای هەیە. بەبێ digits (int) دەدات.
print(round(3.14159, 2))
print(round(7.5))
print(round(2.5))
print(round(3.5))
print(round(2.675, 2))3.14 8 2 4 2.67
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Two digits after the point: 3.14.
دوو دوای : 3.14.
- Lineدێڕی 2-4
An exact half goes to the even neighbour: 7.5 becomes 8, 2.5 becomes 2 and 3.5 becomes 4. This is not the school rule (always up); it avoids pushing totals upwards.
نیوەی ڕێک دەچێت بۆ دراوسێ جووتەکە: 7.5 دەبێتە 8، 2.5 دەبێتە 2 و 3.5 دەبێتە 4. ئەمە یاسای قوتابخانە نییە (هەمیشە بەرەو سەرەوە)؛ ڕێگری دەکات لەوەی کۆکان بەرەو سەرەوە بڕۆن.
- Lineدێڕی 5
2.675 is really stored as 2.67499999…, so it rounds down to 2.67. The statistics course shows the stored number and how to round exactly: Why
round(2.675, 2)gives 2.67.2.675 لە ڕاستیدا وەک 2.67499999… هەڵدەگیرێت، بۆیە دەبێتە 2.67. کۆرسی ژمارە هەڵگیراوەکە و ڕێگەی ورد پیشان دەدات: بۆچی
round(2.675, 2)دەکاتە 2.67.
More maths: the math moduleبیرکاریی زیاتر: مۆدیوڵەکە math
For square roots, π and similar tools, Python has a ready-made module called math. Write import math once at the top of the program, then use math. in front of each tool. You will learn how modules work in Modules and import; for now, this preview is enough.
بۆ ، π و ئامرازی هاوشێوە، Python ئامادەی هەیە بە ناوی math. یەک جار لە سەرەوەی بنووسە import math، پاشان math. لە پێش هەر ئامرازێک بەکار بهێنە. لە مۆدیوڵەکان و import فێری کارکردنی دەبیت؛ بۆ ئێستا ئەم پێشەکییە بەسە.
mathmath.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسەimport math
print(math.sqrt(16))
print(math.pi)
print(math.floor(7.8))
print(math.ceil(7.2))
print(math.gcd(12, 18))
radius = 3
area = math.pi * radius ** 2
print(round(area, 2))4.0 3.141592653589793 7 8 6 28.27
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
math.sqrt()always gives a float.math.sqrt()هەمیشە دەدات.- Lineدێڕی 5-6
floorrounds down,ceilrounds up.floorبەرەو خوار نزیک دەکاتەوە وceilبەرەو سەرەوە.- Lineدێڕی 7
math.gcd()gives the greatest common divisor. The functions chapter writes its own GCD function and then meets this ready-made one.math.gcd()دەدات. بەشی GCDـی خۆی دەنووسێت و پاشان ئەم ئامادەیە دەناسێت.- Lineدێڕی 9-11
Area of a circle: . With the area is about 28.27.
ڕووبەری بازنە: . بە ڕووبەرەکە نزیکەی 28.27ـە.
The same formula as the last lines of the program.
هەمان وەک دوایین دێڕەکانی .
How to use the math module: import math, then math.pi, math.sqrt() and rounding up or down with math.ceil() and math.floor().
بەکارهێنانی math: import math، پاشان math.pi، math.sqrt() و بۆ سەرەوە یان خوارەوە بە math.ceil() و math.floor().
Watch 6:43-8:44, the chapter 'math module': import math, pi, sqrt, ceil and floor.
لە 6:43 تا 8:44 سەیر بکە، بەشی math module: import math، pi، sqrt، ceil و floor.
More maths: the math moduleبیرکاریی زیاتر: مۆدیوڵەکە math
The math toolboxئامرازەکانی math
| Toolئامراز | What it givesچی دەدات | Exampleنموونە | Resultئەنجام |
|---|---|---|---|
math.sqrt(x) | square root, always a float، هەمیشە | math.sqrt(16) | 4.0 |
math.pow(x, y) | x to the power y, always a float (** keeps ints exact)x بەرز دەکاتەوە بۆ y، هەمیشە (** بە وردی دەهێڵێتەوە) | math.pow(2, 3) | 8.0 |
math.floor(x) | rounds down to a whole numberبەرەو خوار دەیکات بە | math.floor(89.7) | 89 |
math.ceil(x) | rounds up to a whole numberبەرەو سەرەوە دەیکات بە | math.ceil(89.7) | 90 |
math.fabs(x) | absolute value (distance from 0), as a float (دووری لە سفرەوە)، وەک | math.fabs(-4) | 4.0 |
math.fmod(x, y) | remainder of x ÷ y, as a float; the usual choice for floats دابەشکردنی x بەسەر y، وەک ؛ بۆ ئەمە باشترە | math.fmod(17, 5) | 2.0 |
math.factorial(n) | : | math.factorial(5) | 120 |
math.gcd(a, b) | greatest common divisor | math.gcd(12, 18) | 6 |
math.sin(x) | sine; also math.cos(x) and math.tan(x) — x in radians؛ هەروەها math.cos(x) و math.tan(x) — x بە | math.sin(math.pi / 2) | 1.0 |
math.radians(d) | degrees → radians بۆ | math.radians(180) | 3.141592653589793 |
math.pi, math.e | the constants π and eنەگۆڕەکانی π و e | math.e | 2.718281828459045 |
import math
print(math.floor(89.7), math.ceil(89.7))
print(math.fabs(-4), math.fmod(17, 5))
print(math.fmod(-7, 2), -7 % 2)
print(math.factorial(5), math.pow(2, 3))
print(math.sin(math.pi / 2))
print(math.sin(90))89 90 4.0 2.0 -1.0 1 120 8.0 1.0 0.8939966636005579
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
floorandceilgive ints: 89.7 goes down to 89 or up to 90.floorوceilدەدەن: 89.7 بەرەو خوار دەبێتە 89 یان بەرەو سەرەوە دەبێتە 90.- Lineدێڕی 5
With a negative number they differ:
fmodkeeps the sign of the first number,%the sign of the second (see Extra: dividing negative numbers).لەگەڵ جیاوازن:
fmodنیشانەی ژمارەی یەکەم دەهێڵێتەوە و%نیشانەی ژمارەی دووەم (بڕوانە زیاتر: دابەشکردنی ژمارە سالبەکان).- Lineدێڕی 8
A trap:
math.sin(90)treats 90 as radians, not degrees. Convert first withmath.radians(90).تەڵەیەک:
math.sin(90)ژمارە 90 وەک دادەنێت، نەک . سەرەتا بەmath.radians(90)بیگۆڕە.
Angles in the math module are measured in radians: a full turn is radians, so 180° is . The calculus course explains why in Measuring angles in radians.
لە mathـدا گۆشەکان بە دەپێورێن: سووڕانەوەیەکی تەواو ، بۆیە 180 دەکاتە . کۆرسی ماتماتیکی ئەم هۆیە لە پێوانی گۆشە بە ڕادیان ڕوون دەکاتەوە.
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What happensچی ڕوو دەدات | Fixچارەسەر |
|---|---|---|
a + b / 2 for an averagea + b / 2 بۆ تێکڕا | Only b is divided: a wrong answer, no message.تەنها b دابەش دەکرێت: وەڵامی هەڵە، بێ پەیام. | (a + b) / 2(a + b) / 2 |
2 ^ 3 for a power2 ^ 3 بۆ | Gives 1.1 دەدات. | 2 ** 32 ** 3 |
/ when you need a whole number/ کاتێک پێویستە | You get 5.0 instead of 5.لەبری 5، 5.0 وەردەگریت. | //, or int() if you want to cut the decimals//، یان int() ئەگەر دەتەوێت بەشی کەرتی ببڕیت |
count++count++ | SyntaxErrorSyntaxError | count += 1count += 1 |
-3 ** 2 to square -3 ** 2 بۆ دووجاکردنی | Gives -9.-9 دەدات. | (-3) ** 2(-3) ** 2 |
0.1 + 0.2 == 0.30.1 + 0.2 == 0.3 | False, because of a tiny rounding error.False، بەهۆی زۆر بچووک. | math.isclose(...) or compare rounded valuesmath.isclose(...) یان نزیککراوەکان بەراورد بکە |
"Total: " + 500"Total: " + 500 | TypeErrorTypeError | "Total: " + str(500) or an f-string"Total: " + str(500) یان |
| Dividing by a variable that can be 0دابەشکردن بەسەر کە دەکرێت 0 بێت | ZeroDivisionErrorZeroDivisionError | Check it first with an if statementسەرەتا بە ڕستەی if بیپشکنە |
Exercisesڕاهێنانەکان
Write down the value of each expression before you run it. Then check with Python.
پێش جێبەجێکردن، هەر بنووسە. پاشان بە Python پشتڕاستی بکەرەوە.
7 + 3 * 2
(7 + 3) * 2
20 // 6
20 % 6
2 ** 3 ** 2
-2 ** 2
9 / 3
7 // 2 * 2
1 + 2 * 3 % 4Show answerوەڵامەکە پیشان بدە
print() پشکنراوەex_eval.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسەprint(7 + 3 * 2)
print((7 + 3) * 2)
print(20 // 6)
print(20 % 6)
print(2 ** 3 ** 2)
print(-2 ** 2)
print(9 / 3)
print(7 // 2 * 2)
print(1 + 2 * 3 % 4)13 20 3 2 512 -4 3.0 6 3
The last two: 7 // 2 * 2 means (7 // 2) * 2, which is (same row, left to right). 1 + 2 * 3 % 4 means 1 + ((2 * 3) % 4), which is .
دوو دانەی کۆتایی: 7 // 2 * 2 واتە (7 // 2) * 2، کە دەکاتە (هەمان ڕیز، لە چەپەوە بۆ ڕاست). 1 + 2 * 3 % 4 واتە 1 + ((2 * 3) % 4)، کە دەکاتە .
A video is 3725 seconds long. Use only // and % to print how many hours, minutes and seconds that is.
ڤیدیۆیەک 3725 چرکە درێژە. تەنها بە // و % چاپ بکە کە ئەمە چەند کاتژمێر، خولەک و چرکەیە.
Hintئاماژە
One hour is 3600 seconds and one minute is 60 seconds.
یەک کاتژمێر 3600 چرکەیە و یەک خولەک 60 چرکە.
Show answerوەڵامەکە پیشان بدە
seconds = 3725
hours = seconds // 3600
seconds = seconds % 3600
minutes = seconds // 60
seconds = seconds % 60
print(hours, "h", minutes, "min", seconds, "s")1 h 2 min 5 s
A shopkeeper sells goods for 3,750 dinars and gets a 10,000-dinar note. Print the change, then how many 5,000, 1,000 and 250-dinar notes he gives back (as few notes as possible).
دوکاندارێک کاڵایەک بە 3,750 دینار دەفرۆشێت و دراوێکی 10,000 دیناری وەردەگرێت. باقییەکە چاپ بکە، پاشان چەند دراوی 5,000، 1,000 و 250 دینار دەداتەوە (کەمترین ژمارەی دراو).
Hintئاماژە
Take out the biggest notes first with //, and keep what is left with %.
سەرەتا دراوە گەورەکان بە // دەربهێنە و ئەوەی دەمێنێتەوە بە % بهێڵەوە.
Show answerوەڵامەکە پیشان بدە
price = 3750
paid = 10000
change = paid - price
print("Change:", change, "dinars")
fives = change // 5000 # 5,000-dinar notes
change = change % 5000
thousands = change // 1000 # 1,000-dinar notes
change = change % 1000
two_fifties = change // 250 # 250-dinar notes
print(fives, "x 5000,", thousands, "x 1000,", two_fifties, "x 250")Change: 6250 dinars 1 x 5000, 1 x 1000, 1 x 250
Change: .
باقییەکە: .
For the three-digit number 347, print its hundreds, tens and ones digits, and their sum.
ژمارەی 347 سێ هەیە. سەدان، دەیان و یەکانی و کۆکەیان چاپ بکە.
Show answerوەڵامەکە پیشان بدە
n = 347
hundreds = n // 100
tens = n // 10 % 10
ones = n % 10
print(hundreds, tens, ones)
print("Digit sum:", hundreds + tens + ones)3 4 7 Digit sum: 14
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
n // 10is 34;% 10then keeps its last digit, 4.n // 10دەکاتە 34؛ پاشان% 10دوایین دەهێڵێتەوە، 4.
Three marks are 78, 85 and 90. Print their average, first in full and then rounded to two decimal places.
سێ نمرە 78، 85 و 90ـن. تێکڕاکەیان چاپ بکە، سەرەتا بە تەواوی و پاشان نزیککراوە بۆ دوو .
Show answerوەڵامەکە پیشان بدە
mark1 = 78
mark2 = 85
mark3 = 90
average = (mark1 + mark2 + mark3) / 3
print(average)
print(round(average, 2))84.33333333333333 84.33
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
The parentheses make Python add before it divides.
کەوانەکان وا دەکەن Python پێش دابەشکردن کۆ بکاتەوە.
Exercisesڕاهێنانەکان
Exercises: formulas in codeڕاهێنان: فۆرمولەکان لە کۆددا
Einstein's formula says how much energy (in joules) is hidden in a mass (in kilograms); is the speed of light, about metres per second. Write a program that works out for a mass of 2 kg.
ئەنیشتاین دەڵێت چەند وزەی (بە جوول) لە بارستاییەکی (بە کیلۆگرام)دا شاراوەتەوە؛ خێرایی ڕووناکییە، نزیکەی مەتر لە چرکەیەکدا. بنووسە کە بۆ بارستاییەکی 2 کیلۆگرامی هەژمار بکات.
Hintئاماژە
Python writes as 3e8. Remember: ** is the power, not ^.
Python وەک 3e8 دەنووسێت. لەبیرت بێت: ** ، نەک ^.
Show answerوەڵامەکە پیشان بدە
c = 3e8 # speed of light in metres per second
mass = 2 # kilograms
energy = mass * c ** 2
print(energy)
print(type(c))1.8e+17 <class 'float'>
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
3e8is a float written in scientific notation: .3e8کە بە نووسراوە: .- Lineدێڕی 4
Very big floats are printed the same way:
1.8e+17means joules.کاتێک زۆر گەورە بن بە هەمان شێوە چاپ دەکرێن:
1.8e+17واتە جوول.
A round dartboard has a radius of 10 cm and its centre at (0, 0). A dart at (x, y) is on the board when . Write this test as one Python expression and try it for the darts at (0, 0), (6, 8), (7, 7) and (10, 1).
تەختەیەکی خڕی تیرهاوێشتن نیوەتیرەی 10 سانتیمەترە و ناوەندەکەی لە (0, 0)دایە. تیرێک لە (x, y) لەسەر تەختەکەیە کاتێک . ئەم تاقیکردنەوەیە وەک یەک Python بنووسە و بۆ تیرەکانی (0, 0)، (6, 8)، (7, 7) و (10, 1) تاقی بکەرەوە.
Hintئاماژە
<= means “less than or equal to” and gives True or False. Comparison operators are taught in Making decisions; arithmetic is done before the comparison.
<= واتە «کەمترە یان یەکسانە بە» و True یان False دەدات. لە بڕیاردان دەخوێنرێن؛ هەژمارکردن پێش بەراوردکردن ئەنجام دەدرێت.
Show answerوەڵامەکە پیشان بدە
x = 0
y = 0
print(x ** 2 + y ** 2 <= 10 ** 2)
x = 6
y = 8
print(x ** 2 + y ** 2 <= 10 ** 2)
x = 7
y = 7
print(x ** 2 + y ** 2 <= 10 ** 2)
x = 10
y = 1
print(x ** 2 + y ** 2 <= 10 ** 2)True True True False
(6, 8) lies exactly on the edge: , and <= counts the edge. (7, 7) gives 98, inside. (10, 1) gives 101, just outside.
(6, 8) ڕێک لەسەر لێوارەکەیە: و <= لێوارەکەش دەژمێرێت. (7, 7) دەکاتە 98، لە ناوەوە. (10, 1) دەکاتە 101، کەمێک لە دەرەوە.
The volume of a sphere with radius is given by the formula below. A football has a radius of about 11 cm. Use math.pi to print its volume in cm³, rounded to 2 decimal places. Then try a small ball with .
قەبارەی گۆیەک بە نیوەتیرەی بەم خوارەوە دەدۆزرێتەوە. تۆپی پێ نیوەتیرەکەی نزیکەی 11 سانتیمەترە. بە math.pi قەبارەکەی بە cm³ چاپ بکە، نزیککراوە بۆ 2 . پاشان تۆپێکی بچووک بە تاقی بکەرەوە.
Show answerوەڵامەکە پیشان بدە
import math
r = 11 # radius of a football in cm
volume = 4 / 3 * math.pi * r ** 3
print(round(volume, 2))
r = 3
print(round(4 / 3 * math.pi * r ** 3, 2))5575.28 113.1
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
**comes first (), then the multiplications and the division from left to right.**یەکەم دێت ()، پاشان لێکدان و دابەشکردنەکان لە چەپەوە بۆ ڕاست.
A 5-metre ladder leans against a wall and makes an angle of 70° with the ground. The height it reaches is , where is the length of the ladder and the angle. Print the height, rounded to 2 decimal places.
پەیژەیەکی 5 مەتری بە دیوارێکەوە پاڵ دراوە و گۆشەیەکی 70 لەگەڵ زەویدا دروست کردووە. ئەو بەرزییەی پێی دەگات ـە، کە درێژیی پەیژەکەیە و گۆشەکەیە. بەرزییەکە چاپ بکە، نزیککراوە بۆ 2 .
Hintئاماژە
math.sin() wants radians. Convert the angle with math.radians(70) first.
math.sin() دەوێت. سەرەتا گۆشەکە بە math.radians(70) بگۆڕە.
Show answerوەڵامەکە پیشان بدە
import math
length = 5 # metres
angle = 70 # degrees between the ladder and the ground
height = length * math.sin(math.radians(angle))
print(round(height, 2))
print(round(length * math.sin(angle), 2)) # wrong: 70 is read as radians4.7 3.87
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
Degrees → radians → sine → times the length: about 4.70 m.
بۆ ، پاشان ، پاشان جاران درێژییەکە: نزیکەی 4.70 مەتر.
- Lineدێڕی 8
Without
math.radians()Python takes 70 radians (about 11 full turns) and gives a wrong height, with no error message.بەبێ
math.radians()، Python 70 وەردەگرێت (نزیکەی 11 سووڕانەوەی تەواو) و بەرزییەکی هەڵە دەدات، بێ هیچ .
Summaryپوختە
- An expression is values joined by operators; Python reduces it to one value.
/always gives a float;//rounds down to a whole number;%gives the remainder;**is a power.- Precedence:
( ), then**, then*///%, then+-; same row left to right, except**. x += 1is short forx = x + 1; there is no++in Python.+joins strings and*repeats them; text and numbers are never mixed automatically.- Floats carry tiny rounding errors: round what you show, and never compare floats with
==.
- چەند کە بە بەستراون؛ Python کورتی دەکاتەوە بۆ یەک .
/هەمیشە دەدات؛//بەرەو خوار دەیکات بە ؛%دەدات؛**.- :
( )، پاشان**، پاشان*///%، پاشان+-؛ هەمان ڕیز لە چەپەوە بۆ ڕاست، جگە لە**. x += 1شێوەی کورتیx = x + 1ـە؛ لە Pythonـدا++نییە.+دەلکێنێت و*دووبارەیان دەکاتەوە؛ دەق و ژمارە هەرگیز خۆکار تێکەڵ ناکرێن.- زۆر بچووکیان هەیە: ئەوەی پیشانی دەدەیت نزیک بکەرەوە و هەرگیز بە
==بەراورد مەکە.
What is the difference between 17 / 5 and 17 // 5?جیاوازیی نێوان 17 / 5 و 17 // 5 چییە؟
17 / 5 is 3.4 (a float); 17 // 5 is 3 (the whole part, rounded down).
17 / 5 دەکاتە 3.4 ()؛ 17 // 5 دەکاتە 3 (بەشی تەواو، بەرەو خوار).
How do you test whether n is even?چۆن تاقی دەکەیتەوە کە n جووتە؟
n % 2 == 0: an even number leaves no remainder when divided by 2.
n % 2 == 0: ژمارەی جووت کاتێک بەسەر 2دا دابەش دەکرێت هیچ نامێنێتەوە.
What is 2 + 3 * 4 ** 2, and why?2 + 3 * 4 ** 2 چەندە و بۆچی؟
50: first 4 ** 2 is 16, then 3 * 16 is 48, then 2 + 48 is 50.
50: یەکەم 4 ** 2 دەکاتە 16، پاشان 3 * 16 دەکاتە 48، پاشان 2 + 48 دەکاتە 50.
Why does "Age: " + 21 fail?بۆچی "Age: " + 21 سەرناکەوێت؟
+ cannot join text and a number. Use str(21), a comma in print(), or an f-string.
+ ناتوانێت دەق و ژمارە بلکێنێت. str(21)، کۆما لە print()ـدا یان بەکار بهێنە.
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
Python as a calculator: + - * / // ** %, the order of operations, += and the built-in abs() and round().
وەک : + - * / // ** %، ، += و abs() و round().
Watch 0:00-6:43: arithmetic, modulus (2:19), order of operations (3:40), += (4:35), abs() and round() (5:31).
لە 0:00 تا 6:43 سەیر بکە: ، (2:19)، (3:40)، += (4:35)، abs() و round() (5:31).
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Input and Outputنێوخستە و دەرخستە
Related lecturesوانە پەیوەندیدارەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Python Tutorial 3.1.1: Numbers (operators, / // % **, mixed types)
- Python Language Reference 6: Expressions (6.17 Operator precedence, ** and unary minus, floor division)
- Python Library: Built-in types — numeric types, floor division, round half to even
- Python Library: Built-in functions (round, divmod, int, pow)
- Python Library: math — Mathematical functions (floor, ceil, fabs, fmod, factorial, gcd, pow, sin, radians, pi, e)
- Python Tutorial 15: Floating-Point Arithmetic — Issues and Limitations
- Harvard CS50's Introduction to Programming with Python, Lecture 0 notes (integers, floats, round)
- Liang, Introduction to Programming Using Python — table of contents (chapter 2, Elementary Programming)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.