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.

23 min read23 خولەک خوێندنەوە · 23 stepsهەنگاو

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 هەژمار دەکات و یەک بەدەست دەهێنێت.

The tea-house billپسوولەی چایخانە

A waiter writes on his pad: 3 teas at 500 dinars and one kulicha at 250. Before you pay, he works it out: 3×500=15003 \times 500 = 1500, then 1500+250=17501500 + 250 = 1750. 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⁩. پێش ئەوەی پارە بدەیت هەژماری دەکات: 3×500=15003 \times 500 = 1500، پاشان 1500+250=17501500 + 250 = 1750. 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 یاسای جێگیر پەیڕەو دەکات کە کام یەکەم کار دەکات؛ لەم وانەیەدا فێریان دەبیت.

Typing expressions in the Python shellنووسینی دەربڕینەکان لە شێڵی کارلێکداPYTHONType in a terminalلە تێرمیناڵدا بینووسە
>>> 3 * 500 + 250
1750
>>> 1750 / 2
875.0
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1-2

At the >>> prompt the shell shows the value of an expression at once; in a script you need print().

لە بەردەم >>>ـدا شێڵەکە یەکسەر پیشان دەدات؛ لە پێویستت بە print() هەیە.

Quick checkپشکنینی خێراIn the shell you type 2 * 50 + 10. What does Python show?لە شێڵەکەدا دەنووسیت 2 * 50 + 10. Python چی پیشان دەدات؟

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 ژمارەیەک دەکات بە .

One symbol, two jobsیەک هێما، دوو کارPYTHON
x = 7
print(-x)       # unary minus: one operand
print(10 - x)   # binary minus: two operands
Quick checkپشکنینی خێراIn the expression price * 3, what are the operands?لە price * 3ـدا کامانەن؟

Quick checkپشکنینی خێراWhich line uses - as a unary operator?لە کام دێڕدا - وەک بەکار هاتووە؟

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، بۆ ئەوەی بەراوردیان بکەیت.

Arithmetic operators in Python (checked with Python 3.12) لە Pythonـدا (بە ⁦Python 3.12⁩ پشکنراون)
OperatorNameناوExampleنموونەResultئەنجام
+Additionکۆکردنەوە17 + 522
-Subtractionلێدەرکردن17 - 512
*Multiplicationلێکدان17 * 585
/Division (always a float) (هەمیشە )17 / 53.4
//Floor division (whole part) (بەشی تەواو)17 // 53
%Remainder (modulus)17 % 52
**Power (exponentiation)17 ** 2289
All seven operators at workهەر حەوت کارپێکەرەکە لە کارداarithmetic.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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)
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.

Quick checkپشکنینی خێراWhat does 17 // 5 give?17 // 5 چی دەدات؟

06- مامه ڵه كردن له گه ڵ ژماره دا له پایثۆنا Working With Numbers | Math In PythonMR. Ant · Kurdish (Sorani)کوردی (سۆرانی) · 18K viewsبینین · 597 likesلایک

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 / 2 is 5.0, not 5.
  • // is floor division. It divides and then rounds down to a whole number: 7 // 2 is 3.
  • % gives the remainder (also called the modulus): what is left after the whole groups are taken out. 7 % 2 is 1.
  • / دابەشکردنی ڕاستەقینەیە. ئەنجامەکەی هەمیشە ، تەنانەت کاتێک دابەشکردنەکە تەواو دەردەچێت: 10 / 2 دەکاتە 5.0، نەک 5.
  • // . دابەش دەکات و پاشان ئەنجامەکە بەرەو خوار دەکات بە : 7 // 2 دەکاتە 3.
  • % دەدات: ئەوەی دوای دەرهێنانی کۆمەڵە تەواوەکان دەمێنێتەوە. 7 % 2 دەکاتە 1.
Seventeen sweets packed into bags of five. Three bags are full, so 17 // 5 is 3. Two sweets are left outside the bags, so 17 % 5 is 2. Underneath: 17 = 3 × 5 + 2.حەڤدە شیرینی کە لە کیسەی پێنج دانەییدا دانراون. سێ کیسە پڕن، بۆیە 17 // 5 دەکاتە 3. دوو شیرینی لە دەرەوەی کیسەکان دەمێننەوە، بۆیە 17 % 5 دەکاتە 2. لە خوارەوە: 17 = 3 × 5 + 2.
Packing 17 sweets in bags of 5: // counts the full bags, % counts what is left.دانانی ⁦17⁩ شیرینی لە کیسەی 5 دانەییدا: // کیسە پڕەکان دەژمێرێت و % ئەوەی دەمێنێتەوە.CêIT
17=3⏟17 // 5×5+2⏟17 % 517 = \underbrace{3}_{17\,//\,5} \times 5 + \underbrace{2}_{17\,\%\,5}

// and % always fit together: (quotient × divisor) + remainder gives back the number you started with.

// و % هەمیشە پێکەوە گونجاون: (ئەنجامی // × دابەشکەر) + هەمان ژمارەی سەرەتا دەداتەوە.

Dividing in three waysدابەشکردن بە سێ ڕێگاdivision.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print(10 / 2)
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(divmod(17, 5))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

Even 10 / 2 gives 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() کە هەردوو ئەنجامەکە پێکەوە دەدات: ئەنجامی // و ئەنجامی %.

Quick checkپشکنینی خێراWhat does 10 / 2 print in Python?10 / 2 لە Pythonـدا چی چاپ دەکات؟

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 % 2 is 0, and odd when it is 1. You will use this in if statements.
  • Splitting units. 135 minutes is 135 // 60 hours and 135 % 60 minutes.
  • Digits of a number. n % 10 is the last digit, and n // 10 removes 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 لای دەبات. پڕۆژەی دووانی بۆ دەیی بە هەمان بیرۆکە کار لەگەڵ دەکات.
Three everyday uses of // and %سێ بەکارهێنانی ڕۆژانەی // و %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 odd
Quick checkپشکنینی خێراWhich expression gives the last digit of 2026?کام دوایین 2026 دەدات؟

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 2102^{10}. The number on the left is the base, the number on the right is the exponent.

** ژمارەیەک بەرز دەکاتەوە بۆ : 2 ** 10 واتە 2 دە جار لە خۆی لێک بدرێت. لە بیرکاریدا دەینووسیت 2102^{10}. ژمارەی لای چەپ ـە و ژمارەی لای ڕاست ـە.

Powers, roots and very big numbersتوانەکان، ڕەگ و ژمارەی زۆر گەورەpower.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print(2 ** 10)
print(5 ** 3)
print(9 ** 0.5)
print(2 ** -1)
print(2 ** 100)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

5 ** 3 is the volume of a cube with sides of 5: the functions chapter builds a cube_volume function with exactly this expression.

5 ** 3 قەبارەی مکەعەبێکە (سندوقێک کە درێژی، پانی و بەرزیی یەکسانن) کە هەر لایەکی 5ـە: بەشی cube_volume بە هەمان ئەم دروست دەکات.

Lineدێڕی 3

A power of 0.5 is a square root: 9 ** 0.5 is 3.0.

⁦0.5⁩ : 9 ** 0.5 دەکاتە 3.0.

Lineدێڕی 4

A negative exponent divides: 2 ** -1 is 12\frac{1}{2}.

سالب دابەش دەکات: 2 ** -1 دەکاتە 12\frac{1}{2}.

Lineدێڕی 5

Python integers have no fixed size limit (only the computer's memory limits them), so 2 ** 100 is printed exactly.

لە Pythonـدا سنوورێکی جێگیری قەبارەیان نییە (تەنها سنووریان بۆ دادەنێت)، بۆیە 2 ** 100 بە وردی چاپ دەکرێت.

Quick checkپشکنینی خێراWhich line prints 8?کام دێڕ 8 چاپ دەکات؟

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 تەنها لە چەپەوە بۆ ڕاست کار ناکات. پەیڕەو دەکات: ڕیزبەندییەکی جێگیری ، وەک ئەوەی لە قوتابخانە فێری بوویت.

Definitionپێناسە

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⁩؛ بۆ گۆڕینی ڕیزبەندییەکە کەوانە بەکاربهێنە.

A ladder of Python operators from first to last: parentheses ( ); ** power; a plus or minus sign in front of a number; * / // %; + and -; then the comparison operators; not; and; or. The first five rows are taught in this lesson, the last four in the decisions chapter.پەیژەیەکی کارپێکەرەکانی Python لە یەکەمەوە بۆ دوایین: کەوانە؛ ** توان؛ نیشانەی کۆ یان کەم لە پێش ژمارەیەک؛ * / // %؛ + و -؛ پاشان کارپێکەرەکانی بەراوردکردن؛ not؛ and؛ or. پێنج ڕیزی یەکەم لەم وانەیەدا دەخوێنرێن و چوار ڕیزی کۆتایی لە بەشی بڕیاردان.
Operator precedence in Python: higher rows are done first. لە Pythonـدا: ڕیزی سەرەوە یەکەم ئەنجام دەدرێت.CêIT
  1. Parentheses first. Anything inside ( ) is worked out before everything else.
  2. Then **, then a sign in front of a number (-x), then *, /, // and %, and last + and -.
  3. Same row: left to right. 10 - 4 - 3 means (10 - 4) - 3, which is 3.
  4. Except **, which goes right to left: 2 ** 3 ** 2 means 2 ** 9, which is 512.
  1. یەکەم کەوانە. هەرچی لەناو ( )ـدایە پێش هەموو شتێک هەژمار دەکرێت.
  2. پاشان **، پاشان نیشانەی پێش ژمارە (-x)، پاشان *، /، // و %، لە کۆتاییشدا + و -.
  3. هەمان ڕیز: لە چەپەوە بۆ ڕاست. 10 - 4 - 3 واتە (10 - 4) - 3، کە دەکاتە 3.
  4. جگە لە ** کە لە ڕاستەوە بۆ چەپ دەڕوات: 2 ** 3 ** 2 واتە 2 ** 9، کە دەکاتە 512.
Quick checkپشکنینی خێراWhat is the value of 2 + 3 * 4? 2 + 3 * 4 چەندە؟

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 ببینین:

Animation: the expression 2 + 3 * 4 ** 2 is evaluated step by step. First 4 ** 2 is highlighted and becomes 16. Then 3 * 16 is highlighted and becomes 48. Then 2 + 48 becomes 50, the result.
Animation: the expression 2 + 3 * 4 ** 2 is evaluated step by step. First 4 ** 2 is highlighted and becomes 16. Then 3 * 16 is highlighted and becomes 48. Then 2 + 48 becomes 50, the result.
Python replaces the highest-ranked part by its value, again and again, until one value is left.Python بەرزترین بەش بە دەگۆڕێت، جار لە دوای جار، تا تەنها یەک دەمێنێتەوە.CêIT
2+3×42=2+3×16=2+48=50\begin{aligned} 2 + 3 \times 4^{2} &= 2 + 3 \times 16 \\ &= 2 + 48 \\ &= 50 \end{aligned}

The same three steps written as maths.

هەمان سێ هەنگاو بە شێوەی بیرکاری.

Quick checkپشکنینی خێراIn 10 - 2 * 3 ** 2, which part does Python work out first?لە 10 - 2 * 3 ** 2ـدا Python یەکەم جار کام بەش هەژمار دەکات؟

Which operator goes first?کام کارپێکەر یەکەم کار دەکات؟

Precedence trapsتەڵەکانی پێشینەیی کارپێکەرەکان

Seven expressions, seven lessonsحەوت دەربڕین، حەوت وانەprecedence.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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)
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 ** 2 is −(32)=−9-(3^2) = -9. Put the minus inside parentheses to square −3-3.

** پێش نیشانەی کەم ئەنجام دەدرێت، بۆیە -3 ** 2 دەکاتە −(32)=−9-(3^2) = -9. بۆ دووجاکردنی −3-3 نیشانەی کەم بخەرە ناو کەوانەوە.

Lineدێڕی 5

** goes right to left: first 3 ** 2 is 9, then 2 ** 9 is 512.

** لە ڕاستەوە بۆ چەپ دەڕوات: یەکەم 3 ** 2 دەکاتە 9، پاشان 2 ** 9 دەکاتە 512.

Lineدێڕی 6-7

Other operators of the same row go left to right: (10 - 4) - 3 and (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:

باوترین ڕاستەقینە تێکڕایە. بۆ تێکڕای دوو ژمارە دەبێت پێش دابەشکردن کۆیان بکەیتەوە:

Average: parentheses matterتێکڕا: کەوانە گرنگەaverage_bug.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
a = 8
b = 6
print(a + b / 2)      # wrong: only b is divided
print((a + b) / 2)    # right: the average
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

/ is done first, so this is 8 + 3, which gives 11.0 — a logic error: no message, just a wrong answer.

/ یەکەم ئەنجام دەدرێت، بۆیە ئەمە دەبێتە 8 + 3، واتە 11.0 — : هیچ پەیامێک نییە، تەنها وەڵامێکی هەڵە.

Lineدێڕی 4

With parentheses the sum is divided: (8 + 6) / 2 gives 7.0.

بە کەوانە کۆکەیە کە دابەش دەکرێت: (8 + 6) / 2 دەکاتە 7.0.

Quick checkپشکنینی خێراWhat does print(-2 ** 2) print?print(-2 ** 2) چی چاپ دەکات؟

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 دەبێت.

int, float and the type of the resultint، float و جۆری ئەنجامەکەmixed.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print(4 * 2.5)
print(7 + 1.0)
print(10 / 5)
print(type(10 / 5))
print(int(7.9))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1-2

An int and a float give a float: 10.0 and 8.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.

Quick checkپشکنینی خێراWhat is the type of 6 / 3?جۆری 6 / 3 چییە؟

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. ئەمە شێوەیەکی کورتی و هەموو یەکێکی هەیە.

One variable, six changesیەک گۆڕاو، شەش گۆڕانaugmented.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

score += 5 reads the old value (10), adds 5 and stores 15 back in score.

score += 5 کۆنەکە ⁦(10)⁩ دەخوێنێتەوە، 5ـی بۆ زیاد دەکات و ⁦15⁩ دەخاتەوە ناو score.

Lineدێڕی 8

//= and %= work the same way with floor division and remainder.

//= و %= بە هەمان شێوە لەگەڵ و کار دەکەن.

A trace table: follow the value line by line: دێڕ بە دێڕ بەدوا بکەوە
LineدێڕStatementscore after itscore دوای ئەو
1score = 1010
2score += 515
4score *= 230
6score -= 426
8score //= 38
10score **= 264
12score %= 104

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 زۆر لەم جۆرە دەنووسیت.

Quick checkپشکنینی خێراtotal = 100, then total -= 30. What is total now?total = 100، پاشان total -= 30. ئێستا total چەندە؟

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

دوو لەسەر (دەق) کار دەکەن. + دوو پێکەوە دەلکێنێت: پێی دەوترێت . * لەگەڵ دووبارە دەکاتەوە ().

Joining and repeating textپێکەوەلکاندن و دووبارەکردنەوەی دەقstrings.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print("Ha" * 3)
print("-" * 20)
print("5" + "5")
print(5 + 5)
greeting = "Good morning, " + "Aram"
print(greeting)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

"-" * 20 draws 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 gives 55; 5 + 5 adds two numbers and gives 10. 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 دەوەستێنێت:

Text + number = TypeErrorدەق + ژمارە = TypeErrorstring_error.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
age = 21
print("Age: " + age)
Two ways to fix itدوو ڕێگای چاکردنەوەstring_fixed.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
age = 21
print("Age: " + str(age))
print("Age:", age)
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.

ڕێگای سێیەم کە باشتریشە، ، لە نێوخستە و دەرخستە دایە. ئامرازی زۆر زیاتریان هەیە کە لە کارکردن لەگەڵ زنجیرەنووسەکان دەیانناسیت.

Quick checkپشکنینی خێراWhat does print("ab" * 3) print?print("ab" * 3) چی چاپ دەکات؟

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))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

So never test floats with ==: the tiny error makes the test False.

بۆیە هەرگیز بە == تاقی مەکەرەوە: ئەو هەڵە بچووکە تاقیکردنەوەکە دەکات بە 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() دەپرسێت «ئایا ئەم دوو ژمارەیە نزیکەی یەکسانن؟» — ڕێگای سەلامەتی بەراوردکردن.

Writing one third as a decimalنووسینی یەک لەسەر سێ بە ژمارەی دەیی

Write 13\frac{1}{3} as a decimal on paper: 0.3333…0.3333\ldots 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.

13\frac{1}{3} لەسەر کاغەز بە ژمارەی بنووسە: 0.3333…0.3333\ldots دەبێت لە شوێنێکدا بوەستیت، بۆیە ژمارەکەت کەمێک هەڵەیە. ژمارەکان بە (⁦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⁩ )، ژمارەکە هەرچییەک بێت.

Quick checkپشکنینی خێراWhy is 0.1 + 0.2 == 0.3 False in Python?بۆچی 0.1 + 0.2 == 0.3 لە Pythonـدا Falseـە؟

Why Is This Happening?! Floating Point Approximationb001 · Englishئینگلیزی · 162K viewsبینین · 5.3K likesلایک

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 داوای کۆ و تێکڕای دە ژمارە دەکات. کۆکردنەوەیان یەک لە دوای یەک هەمان دیاردە لە ناوەڕاستی کارەکەدا پیشان دەدات:

The numbers of Lab 3, added in two stepsژمارەکانی تاقیگەی 3، بە دوو هەنگاو کۆکراونەتەوەlab3_floats.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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)
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⁩.

Quick checkپشکنینی خێراYou add many prices and the running total shows 891.8000000000001. What is the best thing to do?زۆر نرخ کۆ دەکەیتەوە و کۆی گشتی پیشان دەدات 891.8000000000001. باشترین کار چییە؟

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)⁩ دەدات.

Rounding in Pythonنزیککردنەوە لە Pythonـداround.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print(round(3.14159, 2))
print(round(7.5))
print(round(2.5))
print(round(3.5))
print(round(2.675, 2))
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⁩.

Quick checkپشکنینی خێراWhat does round(4.5) give in Python?round(4.5) لە Pythonـدا چی دەدات؟

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 فێری کارکردنی دەبیت؛ بۆ ئێستا ئەم پێشەکییە بەسە.

A few tools from mathچەند ئامرازێکی 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))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

math.sqrt() always gives a float.

math.sqrt() هەمیشە دەدات.

Lineدێڕی 5-6

floor rounds down, ceil rounds 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: πr2\pi r^2. With r=3r = 3 the area is about 28.27.

ڕووبەری بازنە: πr2\pi r^2. بە r=3r = 3 ڕووبەرەکە نزیکەی ⁦28.27⁩ـە.

A=πr2=π×32≈28.27A = \pi r^2 = \pi \times 3^2 \approx 28.27

The same formula as the last lines of the program.

هەمان وەک دوایین دێڕەکانی .

Quick checkپشکنینی خێراWhat does math.sqrt(25) give?math.sqrt(25) چی دەدات؟

Math in Python is easy! 📐Bro Code · Englishئینگلیزی · 270K viewsبینین · 5.8K likesلایک · Partبەشی 6:43–8:44 (ofلە 13:50)

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

The most useful tools of the math module (after import math)بەسوودترین ئامرازەکانی math (دوای import 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)n!=1×2×⋯×nn! = 1 \times 2 \times \cdots \times n: n!=1×2×⋯×nn! = 1 \times 2 \times \cdots \times nmath.factorial(5)120
math.gcd(a, b)greatest common divisormath.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.ethe constants π and eنەگۆڕەکانی π و emath.e2.718281828459045
Trying the toolboxتاقیکردنەوەی ئامرازەکانPYTHON
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))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

floor and ceil give 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: fmod keeps 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 with math.radians(90).

تەڵەیەک: math.sin(90) ژمارە ⁦90⁩ وەک دادەنێت، نەک . سەرەتا بە math.radians(90) بیگۆڕە.

Angles in the math module are measured in radians: a full turn is 2π2\pi radians, so 180° is π\pi. The calculus course explains why in Measuring angles in radians.

لە mathـدا گۆشەکان بە دەپێورێن: سووڕانەوەیەکی تەواو 2π2\pi ، بۆیە ⁦180⁩ دەکاتە π\pi. کۆرسی ماتماتیکی ئەم هۆیە لە پێوانی گۆشە بە ڕادیان ڕوون دەکاتەوە.

Quick checkپشکنینی خێراWhat does math.ceil(89.7) give?math.ceil(89.7) چی دەدات؟

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++SyntaxErrorSyntaxErrorcount += 1count += 1
-3 ** 2 to square −3-3-3 ** 2 بۆ دووجاکردنی −3-3Gives -9.-9 دەدات.(-3) ** 2(-3) ** 2
0.1 + 0.2 == 0.30.1 + 0.2 == 0.3False, because of a tiny rounding error.False، بەهۆی زۆر بچووک.math.isclose(...) or compare rounded valuesmath.isclose(...) یان نزیککراوەکان بەراورد بکە
"Total: " + 500"Total: " + 500TypeErrorTypeError"Total: " + str(500) or an f-string"Total: " + str(500) یان
Dividing by a variable that can be 0دابەشکردن بەسەر کە دەکرێت 0 بێتZeroDivisionErrorZeroDivisionErrorCheck it first with an if statementسەرەتا بە ڕستەی if بیپشکنە

Exercisesڕاهێنانەکان

ExerciseڕاهێنانWork it out by handبە دەست هەژماری بکە

Write down the value of each expression before you run it. Then check with Python.

پێش جێبەجێکردن، هەر بنووسە. پاشان بە 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 % 4
Show answerوەڵامەکە پیشان بدە
Checked with print()بە 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)

The last two: 7 // 2 * 2 means (7 // 2) * 2, which is 3×2=63 \times 2 = 6 (same row, left to right). 1 + 2 * 3 % 4 means 1 + ((2 * 3) % 4), which is 1+2=31 + 2 = 3.

دوو دانەی کۆتایی: 7 // 2 * 2 واتە (7 // 2) * 2، کە دەکاتە 3×2=63 \times 2 = 6 (هەمان ڕیز، لە چەپەوە بۆ ڕاست). 1 + 2 * 3 % 4 واتە 1 + ((2 * 3) % 4)، کە دەکاتە 1+2=31 + 2 = 3.

ExerciseڕاهێنانSeconds to hours, minutes and secondsچرکە بۆ کاتژمێر، خولەک و چرکە

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وەڵامەکە پیشان بدە
ex_time.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
seconds = 3725
hours = seconds // 3600
seconds = seconds % 3600
minutes = seconds // 60
seconds = seconds % 60
print(hours, "h", minutes, "min", seconds, "s")
ExerciseڕاهێنانChange at the bazaarباقی پارە لە بازاڕ

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وەڵامەکە پیشان بدە
ex_change.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
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=1×5000+1×1000+1×2506250 = 1 \times 5000 + 1 \times 1000 + 1 \times 250.

باقییەکە: 6250=1×5000+1×1000+1×2506250 = 1 \times 5000 + 1 \times 1000 + 1 \times 250.

ExerciseڕاهێنانDigits without a loopتاکژمارەکان بەبێ خول

For the three-digit number 347, print its hundreds, tens and ones digits, and their sum.

ژمارەی ⁦347⁩ سێ هەیە. سەدان، دەیان و یەکانی و کۆکەیان چاپ بکە.

Show answerوەڵامەکە پیشان بدە
ex_digits.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
n = 347
hundreds = n // 100
tens = n // 10 % 10
ones = n % 10
print(hundreds, tens, ones)
print("Digit sum:", hundreds + tens + ones)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

n // 10 is 34; % 10 then keeps its last digit, 4.

n // 10 دەکاتە ⁦34⁩؛ پاشان % 10 دوایین دەهێڵێتەوە، 4.

ExerciseڕاهێنانThe average of three marksتێکڕای سێ نمرە

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وەڵامەکە پیشان بدە
ex_average.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
mark1 = 78
mark2 = 85
mark3 = 90
average = (mark1 + mark2 + mark3) / 3
print(average)
print(round(average, 2))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

The parentheses make Python add before it divides.

کەوانەکان وا دەکەن Python پێش دابەشکردن کۆ بکاتەوە.

Quick checkپشکنینی خێراx = 7, then x //= 2, then x **= 2. What is x?x = 7، پاشان x //= 2، پاشان x **= 2. x چەندە؟

Exercisesڕاهێنانەکان

Exercises: formulas in codeڕاهێنان: فۆرمولەکان لە کۆددا

ExerciseڕاهێنانE=mc2E = mc^2 in codeE=mc2E = mc^2 لە کۆددا

Einstein's formula says how much energy EE (in joules) is hidden in a mass mm (in kilograms); cc is the speed of light, about 3×1083 \times 10^8 metres per second. Write a program that works out EE for a mass of 2 kg.

ئەنیشتاین دەڵێت چەند وزەی EE (بە جوول) لە بارستاییەکی mm (بە کیلۆگرام)دا شاراوەتەوە؛ cc خێرایی ڕووناکییە، نزیکەی 3×1083 \times 10^8 مەتر لە چرکەیەکدا. بنووسە کە EE بۆ بارستاییەکی 2 کیلۆگرامی هەژمار بکات.

E=mc2E = m c^2
Hintئاماژە

Python writes 3×1083 \times 10^8 as 3e8. Remember: ** is the power, not ^.

Python 3×1083 \times 10^8 وەک 3e8 دەنووسێت. لەبیرت بێت: ** ، نەک ^.

Show answerوەڵامەکە پیشان بدە
PYTHON
c = 3e8            # speed of light in metres per second
mass = 2           # kilograms
energy = mass * c ** 2
print(energy)
print(type(c))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

3e8 is a float written in scientific notation: 3×1083 \times 10^8.

3e8 کە بە نووسراوە: 3×1083 \times 10^8.

Lineدێڕی 4

Very big floats are printed the same way: 1.8e+17 means 1.8×10171.8 \times 10^{17} joules.

کاتێک زۆر گەورە بن بە هەمان شێوە چاپ دەکرێن: 1.8e+17 واتە 1.8×10171.8 \times 10^{17} جوول.

ExerciseڕاهێنانDid the dart hit the board?ئایا تیرەکە بەر تەختەکە کەوت؟

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 x2+y2≤102x^2 + y^2 \le 10^2. 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)⁩ لەسەر تەختەکەیە کاتێک x2+y2≤102x^2 + y^2 \le 10^2. ئەم تاقیکردنەوەیە وەک یەک 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وەڵامەکە پیشان بدە
PYTHON
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)

(6, 8) lies exactly on the edge: 36+64=10036 + 64 = 100, and <= counts the edge. (7, 7) gives 98, inside. (10, 1) gives 101, just outside.

⁦(6, 8)⁩ ڕێک لەسەر لێوارەکەیە: 36+64=10036 + 64 = 100 و <= لێوارەکەش دەژمێرێت. ⁦(7, 7)⁩ دەکاتە ⁦98⁩، لە ناوەوە. ⁦(10, 1)⁩ دەکاتە ⁦101⁩، کەمێک لە دەرەوە.

ExerciseڕاهێنانThe volume of a ballقەبارەی تۆپێک

The volume of a sphere with radius rr 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 r=3r = 3.

قەبارەی گۆیەک بە نیوەتیرەی rr بەم خوارەوە دەدۆزرێتەوە. تۆپی پێ نیوەتیرەکەی نزیکەی ⁦11⁩ سانتیمەترە. بە math.pi قەبارەکەی بە cm³ چاپ بکە، نزیککراوە بۆ 2 . پاشان تۆپێکی بچووک بە r=3r = 3 تاقی بکەرەوە.

V=43πr3V = \frac{4}{3} \pi r^3
Show answerوەڵامەکە پیشان بدە
PYTHON
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))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

** comes first (r3r^3), then the multiplications and the division from left to right.

** یەکەم دێت (r3r^3)، پاشان لێکدان و دابەشکردنەکان لە چەپەوە بۆ ڕاست.

ExerciseڕاهێنانHow high does the ladder reach?پەیژەکە تا چ بەرزییەک دەگات؟

A 5-metre ladder leans against a wall and makes an angle of 70° with the ground. The height it reaches is h=Lsin⁡θh = L \sin\theta, where LL is the length of the ladder and θ\theta the angle. Print the height, rounded to 2 decimal places.

پەیژەیەکی 5 مەتری بە دیوارێکەوە پاڵ دراوە و گۆشەیەکی ⁦70⁩ لەگەڵ زەویدا دروست کردووە. ئەو بەرزییەی پێی دەگات h=Lsin⁡θh = L \sin\thetaـە، کە LL درێژیی پەیژەکەیە و θ\theta گۆشەکەیە. بەرزییەکە چاپ بکە، نزیککراوە بۆ 2 .

Hintئاماژە

math.sin() wants radians. Convert the angle with math.radians(70) first.

math.sin() دەوێت. سەرەتا گۆشەکە بە math.radians(70) بگۆڕە.

Show answerوەڵامەکە پیشان بدە
PYTHON
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 radians
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⁩ سووڕانەوەی تەواو) و بەرزییەکی هەڵە دەدات، بێ هیچ .

Quick checkپشکنینی خێراHow do you write 3×1083 \times 10^8 in Python?3×1083 \times 10^8 لە Pythonـدا چۆن دەنووسیت؟

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 += 1 is short for x = 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ـدا ++ نییە.
  • + دەلکێنێت و * دووبارەیان دەکاتەوە؛ دەق و ژمارە هەرگیز خۆکار تێکەڵ ناکرێن.
  • زۆر بچووکیان هەیە: ئەوەی پیشانی دەدەیت نزیک بکەرەوە و هەرگیز بە == بەراورد مەکە.
Review questionsپرسیارەکانی پێداچوونەوە
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 Tutorial for Beginners 3: Integers and Floats - Working with Numeric DataCorey Schafer · Englishئینگلیزی · 1M viewsبینین · 25K likesلایک · Partبەشی 0:00–6:43 (ofلە 11:55)

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زاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Input and Outputنێوخستە و دەرخستە

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.