PythonPython · Chapterبەش 4 · Lectureوانە 4.3

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

Read what the user types with input(), turn it into numbers with int() and float() (and why eval() must never be used), control print() with sep and end, use escape sequences, and make tidy output with f-strings and format specifiers.

خوێندنەوەی ئەوەی دەینووسێت بە input()، گۆڕینی بۆ ژمارە بە int() و float() (و بۆچی نابێت هەرگیز eval() بەکار بهێنرێت)، کۆنترۆڵکردنی print() بە sep و end، بەکارهێنانی و دروستکردنی ڕێک بە و .

15 min read16 خولەک خوێندنەوە · 18 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Read text from the user with input() and a clear prompt.
  • Convert input with int() and float(), and predict what happens with bad input.
  • Explain why eval(input()) is a security risk and replace it.
  • Control output with sep, end and escape sequences.
  • Write f-strings with format specifiers for decimals, widths and alignment.
  • بە input() و ڕوون دەق لە بخوێنیتەوە.
  • بە int() و float() بگۆڕیت و پێشبینی بکەیت لەگەڵ خراپ چی ڕوو دەدات.
  • ڕوونی بکەیتەوە بۆچی eval(input()) مەترسیی هەیە و بیگۆڕیت.
  • بە sep، end و کۆنترۆڵ بکەیت.
  • لەگەڵ بۆ ، پانی و ڕێکخستن بنووسیت.

Programs that talk to peopleبەرنامەکان کە قسە لەگەڵ خەڵک دەکەن

Most programs follow the same pattern: they take input, process it, and give output. You already know the output side: print() shows text on the screen (Your first program). In this lesson you learn the input side, input(), which lets the user type something while the program runs — and how to make the output neat and readable.

زۆربەی هەمان شێوە پەیڕەو دەکەن: وەردەگرن، دەکەن و دەدەن. لایەنی دەناسیت: print() دەق لەسەر شاشە پیشان دەدات (یەکەم بەرنامە). لەم وانەیەدا لایەنی فێر دەبیت، input()، کە ڕێگە بە دەدات لە کاتی کارکردنی شتێک بنووسێت — هەروەها چۆن ڕێک و خوێندنەوەی ئاسان بکەیت.

The waiter's questionپرسیاری چایچی

A waiter in a tea house asks “What would you like?” and waits. You answer, and he writes your words on his pad. input("What would you like? ") works the same way: it shows the question (the prompt), waits for the user to type and press Enter, and then gives the program exactly what was typed — always as written text.

چایچییەک لە چایخانەدا دەپرسێت «چیت دەوێت؟» و چاوەڕێ دەکات. وەڵام دەدەیتەوە و ئەو وشەکانت لەسەر کاغەزەکەی دەنووسێت. input("What would you like? ") بە هەمان شێوە کار دەکات: پرسیارەکە پیشان دەدات ()، چاوەڕێ دەکات تا بنووسێت و Enter دابگرێت، پاشان ڕێک ئەوەی نووسراوە دەداتە — هەمیشە وەک دەقی نووسراو.

Where the analogy stops workingسنووری ئەم نموونەیە

A waiter understands that “three teas” is a number. Python does not: it keeps “3” as text until you convert it.

چایچی تێدەگات «سێ چا» ژمارەیە. Python تێناگات: «3» وەک دەق دەهێڵێتەوە تا خۆت بیگۆڕیت.

Reading what the user types: input()خوێندنەوەی ئەوەی بەکارهێنەر دەینووسێت: input()

A program that asks for a nameبەرنامەیەک کە ناو دەپرسێتgreet.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
name = input("What is your name? ")
print("Hello, " + name + "!")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

input() shows the prompt, waits, and returns the typed text. = stores it in name.

input() پیشان دەدات، چاوەڕێ دەکات و دەقە نووسراوەکە دەگەڕێنێتەوە. = لە nameـدا هەڵی دەگرێت.

Lineدێڕی 2

+ joins the pieces of text (see Operators).

+ پارچە دەقەکان پێکەوە دەلکێنێت (بڕوانە کارپێکەرەکان).

  1. 1. Show the prompt1. پیشاندانی دەقی داواکردن

    Python prints the text in the brackets, with no new line after it, so the cursor waits just after the question.

    Python ئەو دەقەی ناو کەوانەکان چاپ دەکات بەبێ دێڕی نوێ لە دوایدا، بۆیە ڕێک لە دوای پرسیارەکە چاوەڕێ دەکات.

  2. 2. Wait2. چاوەڕێکردن

    The program stops and waits. Nothing else happens until the user presses Enter.

    دەوەستێت و چاوەڕێ دەکات. هیچ شتێکی تر ڕوو نادات تا Enter دادەگرێت.

  3. 3. Return the text3. گەڕاندنەوەی دەقەکە

    input() returns everything typed on that line as a string. The Enter key itself is not included.

    input() هەموو ئەوەی لەو دێڕەدا نووسراوە وەک دەگەڕێنێتەوە. خودی Enter تێیدا نییە.

Definitionپێناسە

Text or a sign that a program shows to ask the to type something, such as Enter your age: in input("Enter your age: "). The >>> of the Python shell and the >> of the MATLAB Command Window are prompts too: they mean "type a command".

ئەو دەق یان هێمایەیە کە پیشانی دەدات بۆ ئەوەی داوا لە بکات شتێک بنووسێت، وەک Enter your age: لە input("Enter your age: ")ـدا. >>>ـی شێڵی Python و >>ـی ⁦Command Window⁩ـی MATLABـیش دەقی داواکردنن: واتە «فەرمانێک بنووسە».

Quick checkپشکنینی خێراWhat does input() give back when the user types 25 and presses Enter?کاتێک 25 دەنووسێت و Enter دادەگرێت، input() چی دەگەڕێنێتەوە؟

کۆرسی Python بە کوردی | وانەی 2 | Variables، Arithmetic و InputAra Taha · Kurdish (Sorani)کوردی (سۆرانی) · 1.3K viewsبینین · 104 likesلایک · Partبەشی 6:35–10:36 (ofلە 10:36)

In Kurdish: reading what the user types with input() and a prompt, then printing it with commas and with an f-string.

بە کوردی: وەرگرتنی بە input() و ، پاشان پیشاندانی بە کۆما و بە .

Kurdish video from 6:35: input() with a prompt, printing the answer, then an f-string and a second input() for the age.

ڤیدیۆیەکی کوردی، لە ⁦6:35⁩ بەدواوە: input() لەگەڵ ، پیشاندانی وەڵامەکە، پاشان و input()ـی دووەم بۆ تەمەن.

input() always gives textinput() هەمیشە دەق دەدات

This is the most important fact of the lesson: whatever the user types, input() returns a string. Even 17 arrives as the text "17", and you cannot do arithmetic with text:

ئەمە گرنگترین ڕاستیی ئەم وانەیەیە: هەرچی بنووسێت، input() دەگەڕێنێتەوە. تەنانەت 17 وەک دەقی "17" دەگات و ناتوانیت بە دەق هەژمار بکەیت:

The type of what input() returnsجۆری ئەوەی input() دەیگەڕێنێتەوەinput_type.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
age = input("How old are you? ")
print(type(age))
print(age + 1)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

type(age) shows <class 'str'>: a string.

type(age) پیشان دەدات <class 'str'>: .

Lineدێڕی 3

"17" + 1 mixes text and a number, so Python stops with a TypeError.

"17" + 1 دەق و ژمارە تێکەڵ دەکات، بۆیە Python بە TypeError دەوەستێت.

Animation in four frames. Version A reads two numbers with input() only; the user types 7 and 5 and the program prints 75, because two strings are joined. Version B wraps input() in int(); with the same 7 and 5 it prints 12, because two numbers are added.
Animation in four frames. Version A reads two numbers with input() only; the user types 7 and 5 and the program prints 75, because two strings are joined. Version B wraps input() in int(); with the same 7 and 5 it prints 12, because two numbers are added.
Same keys, different answers: without int() the text "7" + "5" is joined into "75".هەمان دوگمە، دوو وەڵامی جیاواز: بەبێ int() دەقی "⁦7" + "5⁩" دەلکێت و دەبێتە "⁦75⁩".CêIT
Quick checkپشکنینی خێراA program does a = input() and b = input(), then prints a + b. The user types 2 and 3. What is printed? a = input() و b = input() دەکات، پاشان a + b چاپ دەکات. 2 و 3 دەنووسێت. چی چاپ دەکرێت؟

Turning text into numbers: int() and float()گۆڕینی دەق بۆ ژمارە: int() و float()

To calculate with what the user typed, convert it straight away. Put input() inside int() for whole numbers, or inside float() for numbers that may have a decimal point. Python first runs the inner call (input()), then hands its text to the outer one.

بۆ هەژمارکردن بەوەی نووسیویەتی، یەکسەر بیگۆڕە. input() بخەرە ناو int() بۆ ، یان ناو float() بۆ ئەو ژمارانەی لەوانەیە هەبێت. Python سەرەتا بانگکردنە ناوەکییەکە (input()) جێبەجێ دەکات، پاشان دەقەکەی دەداتە دەرەکییەکە.

The user types 2 and 1 on the keyboard and presses Enter. input() turns this into the string "21" (text, orange box). int() turns the string into the integer 21 (a number, blue box) that you can calculate with.بەکارهێنەرەکە لەسەر کیبۆرد 2 و 1 دەنووسێت و Enter دادەگرێت. input() ئەمە دەکات بە زنجیرەنووسەکە "21" (دەق، چوارگۆشەی پرتەقاڵی). int() زنجیرەنووسەکە دەکات بە ژمارە تەواوەکە 21 (ژمارە، چوارگۆشەی شین) کە دەتوانیت هەژماری پێ بکەیت.
From the keyboard to a number: input() gives text, int() makes it a number.لە بۆ ژمارە: input() دەق دەدات و int() دەیکات بە ژمارە.CêIT
A tea-house bill with int()پسوولەی چایخانە بە int()tea.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
teas = int(input("How many teas? "))
price = int(input("Price of one tea (dinars): "))
total = teas * price
print("Total:", total, "dinars")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1-2

int(input(...)): read the text and turn it into a whole number in one line.

int(input(...)): دەقەکە بخوێنەوە و لە یەک دێڕدا بیکە بە .

Lineدێڕی 3

Now * multiplies two numbers: 4×500=20004 \times 500 = 2000.

ئێستا * دوو ژمارە لێک دەدات: 4×500=20004 \times 500 = 2000.

Kilos at the bazaar need float()کیلۆ لە بازاڕ float()ـی پێویستەapples.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
kilos = float(input("How many kilos of apples? "))
price = 1500          # dinars for one kilo
print("You pay", kilos * price, "dinars")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

1.5 kilos is not a whole number, so float() is the right choice. int("1.5") would fail.

⁦1.5⁩ کیلۆ نییە، بۆیە float() هەڵبژاردنی دروستە. int("1.5") سەرناکەوێت.

Quick checkپشکنینی خێراThe user will type a price such as 2.75. Which line reads it correctly? نرخێکی وەک 2.75 دەنووسێت. کام دێڕ بە دروستی دەیخوێنێتەوە؟

Turning text into numbers: int() and float()گۆڕینی دەق بۆ ژمارە: int() و float()

What int() and float() acceptint() و float() چی وەردەگرن

This is the modern, safe way the whole course reads numbers. You met int(), float() and str() in Variables and data types; here is what they do with typical input:

ئەمە ڕێگا نوێ و سەلامەتەکەیە کە هەموو کۆرسەکە ژمارەی پێ دەخوێنێتەوە. int()، float() و str()ـت لە گۆڕاوەکان و جۆرەکانی دراوە ناسی؛ ئەمە ئەوەیە کە لەگەڵ ئاسایی دەیکەن:

What the converters acceptگۆڕەرەکان چی وەردەگرنconversions.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print(int("42"))
print(int("  42  "))
print(float("3.5"))
print(int(float("3.5")))
print(str(42) + "!")
print(int("3.5"))
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Spaces around the digits are allowed: int(" 42 ") is 42.

بۆشایی لە دەوری ژمارەکان ڕێگەپێدراوە: int(" 42 ") دەکاتە ⁦42⁩.

Lineدێڕی 4

To get a whole number from "3.5", go through float() first; int() then cuts off the .5.

بۆ بەدەستهێنانی لە "3.5"، سەرەتا بە float()ـدا تێپەڕە؛ پاشان int() .5 دەبڕێت.

Lineدێڕی 5

str() goes the other way: number → text.

str() بە پێچەوانەوە دەڕوات: ژمارە ← دەق.

Lineدێڕی 6

int("3.5") fails: int() accepts only text that looks like a whole number.

int("3.5") سەرناکەوێت: int() تەنها ئەو دەقە وەردەگرێت کە وەک دەردەکەوێت.

Quick checkپشکنینی خێراWhat does int(float("7.9")) give?int(float("7.9")) چی دەدات؟

When the user types something unexpectedکاتێک بەکارهێنەر شتێکی چاوەڕواننەکراو دەنووسێت

Users make mistakes. If the program asks for an age and gets the word seventeen, int() cannot convert it, and the program stops with a ValueError:

هەڵە دەکەن. ئەگەر تەمەن بپرسێت و وشەی seventeen وەربگرێت، int() ناتوانێت بیگۆڕێت و بە ValueError دەوەستێت:

The same program with bad and good inputهەمان بەرنامە بە نێوخستەی خراپ و باشbad_input.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
age = int(input("How old are you? "))
print("Next year you will be", age + 1)

Read the last line of the message first: the error type (ValueError) and what went wrong (the text could not be read as a base-10 whole number). Errors and debugging explains how to read such a traceback. Later, Handling errors with try and except shows how to catch the error and ask again instead of crashing.

سەرەتا دوایین دێڕی پەیامەکە بخوێنەوە: جۆری هەڵەکە (ValueError) و چی هەڵە بوو (دەقەکە نەتوانرا وەک بنکە ⁦10⁩ بخوێنرێتەوە). هەڵەکان و کرمەکوژی ڕوونی دەکاتەوە چۆن ئەم جۆرە بخوێنیتەوە. دواتر مامەڵەکردن لەگەڵ هەڵەکان بە try و except پیشان دەدات چۆن هەڵەکە بگریت و دووبارە بپرسیتەوە لەبری ئەوەی بکەوێت.

Quick checkپشکنینی خێراA program runs age = int(input("Age: ")) and the user types 17.5. What happens? age = int(input("Age: ")) جێبەجێ دەکات و 17.5 دەنووسێت. چی ڕوو دەدات؟

Why this course never uses eval(input())بۆچی ئەم کۆرسە هەرگیز eval(input()) بەکار ناهێنێت

Quick checkپشکنینی خێراWhy is int(input()) better than eval(input()) for reading a number?بۆچی int(input()) بۆ خوێندنەوەی ژمارە لە eval(input()) باشترە؟

Why this course never uses eval(input())بۆچی ئەم کۆرسە هەرگیز eval(input()) بەکار ناهێنێت

See the danger, then the safe versionمەترسییەکە ببینە، پاشان وەشانی سەلامەت

See it with your own eyes. Here the program asks for a number, but the user types a line of Python instead:

بە چاوی خۆت بیبینە. لێرەدا ژمارەیەک دەپرسێت، بەڵام لەبری ئەوە دێڕێک کۆدی Python دەنووسێت:

eval() runs the user's code — do not copy thiseval() کۆدی بەکارهێنەر جێبەجێ دەکات — ئەمە کۆپی مەکەeval_danger.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
number = eval(input("Enter a number: "))   # DANGEROUS - never do this
print(number)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

eval() treats the typed text as Python code and runs it.

eval() دەقە نووسراوەکە وەک کۆدی Python دادەنێت و جێبەجێی دەکات.

The first run printed a message chosen by the user — the program obeyed a stranger. The same door lets in code that reads, changes or deletes files. This kind of attack is called code injection.

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

Definitionپێناسە

An attack in which a program runs text typed by a user as if it were code, so the user can make the program do anything. Python's eval(input()) is open to it; convert input with int() or float() instead.

هێرشێکە کە تێیدا ئەو دەقەی نووسیویەتی وەک کۆد جێبەجێ دەکات، بۆیە دەتوانێت ناچار بکات هەر کارێک بکات. eval(input())ـی Python بەرەوڕووی ئەم مەترسییەیە؛ لەبری ئەوە بە int() یان float() بگۆڕە.

The safe version refusesوەشانی سەلامەت ڕەتی دەکاتەوەint_safe.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
number = int(input("Enter a number: "))
print(number)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

int() only converts text that looks like a whole number. Anything else is rejected with a ValueError; no code is ever run.

int() تەنها ئەو دەقە دەگۆڕێت کە وەک دەردەکەوێت. هەر شتێکی تر بە ValueError ڕەت دەکرێتەوە؛ هیچ کۆدێک جێبەجێ ناکرێت.

Quick checkپشکنینی خێراIn the first run, why did the program print the stranger's message?لە کارکردنی یەکەمدا، بۆچی پەیامی کەسە نەناسراوەکەی چاپ کرد؟

Special characters: n, t and friendsنووسە تایبەتەکان: \n، \t و هاوشێوەکانیان

Inside a string, a backslash (\) starts an escape sequence: two characters that stand for one special character.

لەناو ، هێمای \ (backslash) دەست پێ دەکات: دوو کە یەک تایبەت دەنوێنن.

WriteبنووسەYou getبەدەست دێت
\na new lineدێڕێکی نوێ
\ta tab (a jump to the next column)Tab (بازدان بۆ ستوونی دواتر)
\"a double quote inside a "..." string جووت لەناو "..."
\'a single quote inside a '...' string تاک لەناو '...'
\\one backslashیەک هێمای \
Escape sequences at workهێما تایبەتەکان لە کارداescapes.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print("Line one\nLine two")
print("Name:\tAram")
print("Mark:\t95")
print("She said \"hello\".")
print('It\'s fine.')
print("A backslash: \\")
Quick checkپشکنینی خێراHow many lines does print("Erbil\nDuhok\nSulaimani") print?print("Erbil\nDuhok\nSulaimani") چەند دێڕ چاپ دەکات؟

f-strings: values inside textf-string: نرخەکان لەناو دەقدا

Joining text with + and str() quickly becomes messy. Modern Python has a cleaner way: put the letter f before the opening quote, and write any variable or expression inside { }. Python replaces each {...} with its value.

لکاندنی دەق بە + و str() زوو تێکەڵ دەبێت. Pythonـی نوێ ڕێگایەکی پاکتری هەیە: پیتی f بخەرە پێش یەکەم و هەر یان لەناو { }ـدا بنووسە. Python هەر {...}ـێک بە دەگۆڕێت.

Definitionپێناسە

A Python with the letter f before the quotes. Expressions inside { } are replaced by their values, and a after a colon controls how they look: f"{price:.2f}".

Pythonـە کە پیتی f پێش دەنووسرێت. ناو { } بە نرخەکانیان دەگۆڕدرێن و دوای دوو خاڵ شێوەیان دیاری دەکات: f"{price:.2f}".

f-strings in actionf-string لە کارداfstrings.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
name = "Dilan"
age = 19
print(f"{name} is {age} years old.")
print(f"Next year {name} will be {age + 1}.")
a = 7
b = 5
print(f"{a} + {b} = {a + b}")
print("{name} forgot the f")
print(f"{age = }")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

No str() and no +: the int age is put into the text for you.

نە str() و نە +: intـی age بۆت دەخرێتە ناو دەقەکە.

Lineدێڕی 4

Any expression works inside the braces: {age + 1}.

هەر لەناو کەوانەکاندا کار دەکات: {age + 1}.

Lineدێڕی 8

Without the f, the braces are printed as they are.

بەبێ f، کەوانەکان وەک خۆیان چاپ دەکرێن.

Lineدێڕی 9

{age = } prints the name and the value: handy when you look for a bug.

{age = } ناو و چاپ دەکات: کاتێک بەدوای دەگەڕێیت بەسوودە.

Three ways to print Dilan is 19سێ ڕێگا بۆ چاپکردنی Dilan is 19
WayڕێگاCodeکۆدNotesتێبینی
Commas in print()کۆما لە print()ـداprint(name, "is", age)Simple; adds spaces for you.سادەیە؛ بۆشایی خۆی زیاد دەکات.
Concatenationprint(name + " is " + str(age))Needs str() and careful spaces.str() و بۆشایی وردی پێویستە.
f-stringprint(f"{name} is {age}")Clear, and allows formatting (next screen).ڕوونە و ڕێگە بە ڕێکخستن دەدات (شاشەی داهاتوو).
Quick checkپشکنینی خێراx = 4. What does print(f"{x} squared is {x ** 2}") print?x = 4. print(f"{x} squared is {x ** 2}") چی چاپ دەکات؟

Python F-strings - Visually ExplainedVisually Explained · Englishئینگلیزی · 67K viewsبینین · 3.2K likesلایک · Partبەشی 0:00–2:19 (ofلە 7:22)

How an f-string puts the values of variables and expressions inside text with { }.

چۆن بە { } و دەخاتە ناو دەقەوە.

Watch 0:00-2:19: f-string syntax, values and expressions inside { }.

لە ⁦0:00⁩ تا ⁦2:19⁩ سەیر بکە: ، و لەناو { }.

Neat numbers and columnsژمارە و ستوونی ڕێک

After the value in an f-string you can add a colon and a format specifier: a short code that says how to show the value — how many decimal places, how wide, and which side to line up on.

دوای لە دەتوانیت دوو خاڵ (:) و زیاد بکەیت: کۆدێکی کورت کە دەڵێت چۆن پیشان بدرێت — چەند ، چەند پان و بەلای کام لادا ڕێک بخرێت.

Format specifiersدیاریکەرەکانی فۆرماتformat_spec.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
price = 2250.0
print(f"{price:.2f}")
print(f"{3.14159:.3f}")
print(f"{1250000:,}")
print(f"[{7:4}]")
print(f"[{'ab':4}]")
print(f"[{'ab':>4}]")
print(f"[{'ab':^6}]")
print(f"[{5:03}]")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-3

.2f = exactly 2 digits after the point (rounded); .3f = 3 digits.

.2f = ڕێک 2 دوای (نزیککراوە)؛ .3f = 3 .

Lineدێڕی 4

, puts a comma every three digits: handy for dinars.

, دوای هەر سێ کۆمایەک دادەنێت: بۆ دینار بەسوودە.

Lineدێڕی 5-6

A width alone: numbers line up on the right, text on the left (the brackets show the edges).

تەنها پانی: ژمارەکان بەلای ڕاستدا ڕێک دەخرێن و دەق بەلای چەپدا (کەوانە چوارگۆشەکان لێوارەکان پیشان دەدەن).

Lineدێڕی 7-8

> lines up on the right, ^ in the centre (and < on the left).

> بەلای ڕاستدا ڕێک دەخات، ^ لە ناوەڕاستدا (و < بەلای چەپدا).

Lineدێڕی 9

03 = width 3, filled with zeros.

03 = پانیی 3، پڕکراوە بە سفر.

The most useful format specifiersبەسوودترین
SpecifierMeaningواتاExampleنموونەResultئەنجام
.2f2 decimal placesدوو f"{2250:.2f}"2250.00
,thousands separatorجیاکەرەوەی هەزارانf"{1250000:,}"1,250,000
6width 6پانیی 6f"[{7:6}]"[ 7]
<8left, width 8چەپ، پانیی 8f"[{'Tea':<8}]"[Tea ]
>8right, width 8ڕاست، پانیی 8f"[{'Tea':>8}]"[ Tea]
^8centre, width 8ناوەڕاست، پانیی 8f"[{'Tea':^8}]"[ Tea ]

Widths are how Lab 1's multiplication table keeps its columns straight: each product is printed with f"{r * c:4}", four characters wide. You will build that table in Nested loops.

پانی ئەو شتەیە کە خشتەی لێکدانی تاقیگەی 1 ستوونەکانی ڕێک دەهێڵێتەوە: هەر ئەنجامێکی لێکدان بە f"{r * c:4}" چاپ دەکرێت، بە پانیی چوار . ئەو خشتەیە لە خولە ناوەکییەکان دروست دەکەیت.

Quick checkپشکنینی خێراWhich f-string prints 3.14 when pi = 3.14159?کاتێک pi = 3.14159، کام 3.14 چاپ دەکات؟

Learn Python format specifiers in 5 minutes! 💬Bro Code · Englishئینگلیزی · 165K viewsبینین · 4.3K likesلایک

Format specifiers in f-strings: decimal places (.2f), padding, left/right/centre alignment and comma separators.

لە : (.2f)، ، بۆ چەپ، ڕاست یان ناوەڕاست و کۆما بۆ جیاکردنەوەی هەزارەکان.

Whole video (5:21): precision (1:35), padding (2:41), alignment (3:15), commas (4:18).

هەموو ڤیدیۆکە ⁦(5:21)⁩: ژمارەی ⁦(1:35)⁩، ⁦(2:41)⁩، ⁦(3:15)⁩ و کۆما ⁦(4:18)⁩.

Putting it together: a tea-house billکۆکردنەوەی هەمووی: پسوولەی چایخانە

This program uses everything from the lesson: a prompt, int(input()), arithmetic, an f-string with widths and thousands separators, and "-" * 26 for a line.

ئەم هەموو شتەکانی وانەکە بەکار دەهێنێت: ، int(input())، هەژمارکردن، لەگەڵ پانی و جیاکەرەوەی هەزاران و "-" * 26 بۆ هێڵێک.

A small receiptپسوولەیەکی بچووکreceipt.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
tea_price = 500
kulicha_price = 750

print("=== Tea house bill ===")
teas = int(input("Teas: "))
kulicha = int(input("Kulicha: "))

tea_total = teas * tea_price
kulicha_total = kulicha * kulicha_price
total = tea_total + kulicha_total

print(f"{'Tea':<8} {teas:>2} x {tea_price:>3} = {tea_total:>6,}")
print(f"{'Kulicha':<8} {kulicha:>2} x {kulicha_price:>3} = {kulicha_total:>6,}")
print("-" * 26)
print(f"{'Total':<8} {total:>17,} IQD")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1-2

Fixed prices are kept in variables at the top, so they are easy to change.

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

Lineدێڕی 5-6

Two inputs, converted at once.

دوو ، یەکسەر گۆڕدراون.

Lineدێڕی 12-13

<8 lines the item names up on the left; >2, >3 and >6, line the numbers up on the right.

<8 ناوی کاڵاکان بەلای چەپدا ڕێک دەخات؛ >2، >3 و >6, ژمارەکان بەلای ڕاستدا.

Lineدێڕی 15

The total's width (17) is chosen so that its last digit sits under the other totals.

پانیی کۆی گشتی ⁦(17)⁩ وا هەڵبژێردراوە کە دوایین بکەوێتە ژێر کۆکانی تر.

Quick checkپشکنینی خێراIn the receipt, why does line 12 use {teas:>2} and not just {teas}?لە پسوولەکەدا، بۆچی دێڕی ⁦12⁩ {teas:>2} بەکار دەهێنێت نەک تەنها {teas}؟

ExerciseڕاهێنانYour turn: a student ID cardنۆرەی تۆیە: کارتی ناسنامەی قوتابی

Read a student's name, student number, department and year of study, then print them as a neat card like the one below. Use "-" * 32 for the lines and f-string widths so that the right edge stays straight whatever the user types.

ناو، ژمارەی قوتابی، بەش و ساڵی خوێندنی قوتابییەک بخوێنەوە، پاشان لە شێوەی کارتێکی ڕێکدا، وەک ئەوەی خوارەوە، چاپیان بکە. بۆ هێڵەکان "-" * 32 و بۆ ئەوەی لێواری ڕاست ڕێک بمێنێتەوە، هەرچی بنووسێت، پانیی بەکار بهێنە.

Hintئاماژە

{name:<19} fills the name up to 19 characters with spaces; {'STUDENT ID CARD':^32} centres the title.

{name:<19} ناوەکە بە پڕ دەکاتەوە تا ⁦19⁩ ؛ {'STUDENT ID CARD':^32} سەردێڕەکە دەخاتە ناوەڕاست.

Show answerوەڵامەکە پیشان بدە
PYTHON
name = input("Name: ")
number = input("Student number: ")
department = input("Department: ")
year = int(input("Year of study: "))

line = "+" + "-" * 32 + "+"
print(line)
print(f"|{'STUDENT ID CARD':^32}|")
print(line)
print(f"| {'Name:':<12}{name:<19}|")
print(f"| {'Number:':<12}{number:<19}|")
print(f"| {'Department:':<12}{department:<19}|")
print(f"| {'Year:':<12}{year:<19}|")
print(line)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 10-13

Each row: 1 space + 12 for the label + 19 for the value = 32, the same width as the line.

هەر ڕیزێک: 1 + ⁦12⁩ بۆ ناونیشانەکە + ⁦19⁩ بۆ = ⁦32⁩، هەمان پانیی هێڵەکە.

Common mistakesهەڵە باوەکان

MistakeهەڵەWhat happensچی ڕوو دەداتFixچارەسەر
Calculating with input() directlyهەژمارکردن ڕاستەوخۆ بە input()Text is joined ("7" + "5" → 75) or a TypeError.دەق دەلکێت ("7" + "5" ← 75) یان TypeError.int(input(...)) or float(input(...))int(input(...)) یان float(input(...))
int() for a decimal such as 2.5int() بۆ ژمارەیەکی وەک 2.5ValueErrorValueErrorfloat()float()
eval(input())eval(input())Runs any code the user types.هەر کۆدێک کە بینووسێت جێبەجێ دەکات.int() / float()int() / float()
Comparing input with a number: input() == 4بەراوردکردنی لەگەڵ ژمارە: input() == 4Always False: "4" is not 4.هەمیشە Falseـە: "4" یەکسانی 4 نییە.Convert first: int(input()) == 4سەرەتا بیگۆڕە: int(input()) == 4
Forgetting the fلەبیرکردنی fThe braces are printed: {name}.کەوانەکان چاپ دەکرێن: {name}.f"Hello, {name}"f"Hello, {name}"
No space at the end of the promptنەبوونی بۆشایی لە کۆتایی Age:17 stuck together.Age:17 پێکەوە نووساون.input("Age: ")input("Age: ")
\n hidden in a Windows path\nـی شاراوە لە WindowsA line break appears.دێڕ دەشکێت."C:\\new" or r"C:\new""C:\\new" یان r"C:\new"

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

ExerciseڕاهێنانA four-function calculatorحاسیبەیەکی بچووک: کۆ، لێدەرکردن، لێکدان و دابەشکردن

Read two numbers (they may have decimals). Print their sum, difference, product and quotient, each with two decimal places.

دوو ژمارە بخوێنەوە (لەوانەیە کەرتییان هەبێت). کۆ، جیاوازی، لێکدان و دابەشکردنیان چاپ بکە، هەر یەکەیان بە دوو .

Show answerوەڵامەکە پیشان بدە
ex_four.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
a = float(input("First number: "))
b = float(input("Second number: "))
print(f"{a} + {b} = {a + b:.2f}")
print(f"{a} - {b} = {a - b:.2f}")
print(f"{a} * {b} = {a * b:.2f}")
print(f"{a} / {b} = {a / b:.2f}")
ExerciseڕاهێنانCelsius to Fahrenheitسێلسیۆس بۆ فەهرەنهایت

Read a temperature in degrees Celsius and print it in Fahrenheit. Use the formula below.

پلەی گەرمی بە سێلسیۆس بخوێنەوە و بە فەهرەنهایت چاپی بکە. ئەم هاوکێشەیەی خوارەوە بەکار بهێنە.

F=C×95+32F = C \times \frac{9}{5} + 32
Show answerوەڵامەکە پیشان بدە
ex_temp.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
celsius = float(input("Temperature in Celsius: "))
fahrenheit = celsius * 9 / 5 + 32
print(f"{celsius} C = {fahrenheit} F")

Check by hand: 25×9/5+32=45+32=7725 \times 9 / 5 + 32 = 45 + 32 = 77.

بە دەست پشکنین: 25×9/5+32=45+32=7725 \times 9 / 5 + 32 = 45 + 32 = 77.

ExerciseڕاهێنانPredict the outputدەرخستە پێشبینی بکە

Without running it, write down exactly what this program prints. Watch sep and end.

بەبێ جێبەجێکردن، ڕێک ئەوە بنووسە کە ئەم چاپی دەکات. ئاگاداری sep و end بە.

PYTHON
print("A", "B", sep="", end="!\n")
print("C", end=" ")
print("D")
print(1, 2, 3, sep="\n")
Show answerوەڵامەکە پیشان بدە
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
print("A", "B", sep="", end="!\n")
print("C", end=" ")
print("D")
print(1, 2, 3, sep="\n")

end=" " keeps C and D on one line; sep="\n" puts every value on its own line.

end=" " وا دەکات C و D لە یەک دێڕدا بن؛ sep="\n" هەر دەخاتە دێڕی خۆیەوە.

ExerciseڕاهێنانFind and fix the bugباگەکە بدۆزەرەوە و چاکی بکە

This program should print the user's age next year, but it crashes. Explain why and fix it.

ئەم دەبێت تەمەنی ساڵی داهاتووی چاپ بکات، بەڵام دەکەوێت. ڕوونی بکەرەوە بۆچی و چاکی بکە.

ex_bug.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
age = input("Your age: ")
print("Next year you will be", age + 1)
Show answerوەڵامەکە پیشان بدە

input() returns the string "20", and "20" + 1 mixes text with a number. Convert the input with int():

input() "20" دەگەڕێنێتەوە و "20" + 1 دەق و ژمارە تێکەڵ دەکات. بە int() بگۆڕە:

ex_bug_fixed.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
age = int(input("Your age: "))
print("Next year you will be", age + 1)
ExerciseڕاهێنانA small marks tableخشتەیەکی بچووکی نمرە

Print this table with f-string widths: names on the left in 8 characters, marks on the right in 6 characters, and the average of 87, 92 and 78 with one decimal place.

ئەم خشتەیە بە پانیی چاپ بکە: ناوەکان بەلای چەپدا لە 8 ، نمرەکان بەلای ڕاستدا لە 6 و تێکڕای ⁦87⁩، ⁦92⁩ و ⁦78⁩ بە یەک .

Show answerوەڵامەکە پیشان بدە
ex_table.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print(f"{'Name':<8}{'Mark':>6}")
print(f"{'Aram':<8}{87:>6}")
print(f"{'Shilan':<8}{92:>6}")
print(f"{'Karwan':<8}{78:>6}")
average = (87 + 92 + 78) / 3
print(f"{'Average':<8}{average:>6.1f}")

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

Exercises: money and yearsڕاهێنان: پارە و ساڵ

ExerciseڕاهێنانSimple interestسوودی سادە

A bank pays simple interest: every year it adds the same percentage of the money you first saved. Read the amount saved (in dinars), the yearly rate in % and the number of years, then print the interest and the total, with thousands separators.

بانکێک سوودی سادە دەدات: هەموو ساڵێک هەمان لەو پارەیەی سەرەتا پاشەکەوتت کردووە زیاد دەکات. بڕی پاشەکەوتکراو (بە دینار)، ڕێژەی ساڵانە بە % و ژمارەی ساڵەکان بخوێنەوە، پاشان سوودەکە و کۆی گشتی بە جیاکەرەوەی هەزاران چاپ بکە.

I=P×R×T100total=P+II = \frac{P \times R \times T}{100} \qquad \text{total} = P + I

PP = the money saved, RR = the rate in %, TT = the number of years, II = the interest.

PP = پارە پاشەکەوتکراوەکە، RR = ڕێژەکە بە %، TT = ژمارەی ساڵەکان، II = سوودەکە.

Show answerوەڵامەکە پیشان بدە
PYTHON
principal = int(input("Money saved (dinars): "))
rate = float(input("Yearly interest rate (%): "))
years = int(input("Number of years: "))

interest = principal * rate * years / 100
total = principal + interest

print(f"Interest: {interest:,.0f} dinars")
print(f"Total after {years} years: {total:,.0f} dinars")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5

2000000×4×3100=240000\frac{2000000 \times 4 \times 3}{100} = 240000.

2000000×4×3100=240000\frac{2000000 \times 4 \times 3}{100} = 240000.

Lineدێڕی 8-9

,.0f: thousands separators and no decimals.

,.0f: جیاکەرەوەی هەزاران و بێ .

ExerciseڕاهێنانWhen will you be 100?کەی دەبیتە ⁦100⁩ ساڵ؟

Ask for the user's name, age and the current year, then print the year in which the user turns 100.

ناو و تەمەنی و ساڵی ئێستا بپرسە، پاشان ئەو ساڵە چاپ بکە کە تێیدا دەبێتە ⁦100⁩ ساڵ.

Show answerوەڵامەکە پیشان بدە
PYTHON
name = input("Your name: ")
age = int(input("Your age: "))
this_year = int(input("This year: "))

year_100 = this_year - age + 100
print(f"{name}, you will be 100 years old in {year_100}.")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5

2026−19=20072026 - 19 = 2007 is the birth year, and 2007+100=21072007 + 100 = 2107. If your birthday has not come yet this year, it is one year earlier.

2026−19=20072026 - 19 = 2007 ساڵی لەدایکبوونە و 2007+100=21072007 + 100 = 2107. ئەگەر ئەمساڵ ڕۆژی لەدایکبوونت هێشتا نەهاتبێت، ساڵێک زووترە.

A program can also read the year from the computer's clock instead of asking: from datetime import date and then date.today().year. You will learn how import works in Modules and import.

دەتوانێت لەبری پرسین ساڵەکە لە کاتژمێری بخوێنێتەوە: from datetime import date و پاشان date.today().year. لە مۆدیوڵەکان و import فێری کارکردنی import دەبیت.

Quick checkپشکنینی خێراtotal = 2240000. Which line prints 2,240,000?total = 2240000. کام دێڕ 2,240,000 چاپ دەکات؟

Summaryپوختە

  • input(prompt) shows the prompt, waits for Enter and returns the typed line as a string.
  • Convert at once: int(input(...)) for whole numbers, float(input(...)) for decimals. Bad text gives a ValueError.
  • Never use eval(input()): it runs any code the user types (code injection).
  • print() takes sep= (between values) and end= (after the last value).
  • \n is a new line and \t a tab; double a backslash or use r"..." for paths.
  • f-strings put values in text: f"{name} is {age}"; format specifiers such as :.2f, :, and :>6 make numbers and columns neat.
  • input(prompt) پیشان دەدات، چاوەڕێی Enter دەکات و دێڕە نووسراوەکە وەک دەگەڕێنێتەوە.
  • یەکسەر بیگۆڕە: int(input(...)) بۆ ، float(input(...)) بۆ . دەقی خراپ ValueError دەدات.
  • هەرگیز eval(input()) بەکار مەهێنە: هەر کۆدێک بینووسێت جێبەجێ دەکات ().
  • print() sep= (نێوان ) و end= (دوای دوایین ) وەردەگرێت.
  • \n دێڕی نوێیە و \t tab؛ بۆ هێمای \ دووبارە بکەرەوە یان r"..." بەکار بهێنە.
  • دەخاتە ناو دەق: f"{name} is {age}"؛ وەک :.2f، :, و :>6 ژمارە و ستوونەکان ڕێک دەکەن.
Review questionsپرسیارەکانی پێداچوونەوە
What type does input() return?input() چ جۆرێک دەگەڕێنێتەوە؟

Always str (a string), even when the user types digits.

هەمیشە str ()، تەنانەت کاتێک ژمارە دەنووسێت.

Why is eval(input()) dangerous?بۆچی eval(input()) مەترسیدارە؟

Because eval() runs the typed text as Python code, so a user can make the program do anything.

چونکە eval() دەقە نووسراوەکە وەک کۆدی Python جێبەجێ دەکات، بۆیە دەتوانێت ناچار بکات هەر کارێک بکات.

How do you print Total: 2,250.00 from total = 2250?چۆن لە total = 2250 Total: 2,250.00 چاپ دەکەیت؟

print(f"Total: {total:,.2f}")

print(f"Total: {total:,.2f}")

What does end="" do in print()?end="" لە print()ـدا چی دەکات؟

It stops print() from starting a new line, so the next output continues on the same line.

ڕێگری لە print() دەکات کە دێڕێکی نوێ دەست پێ بکات، بۆیە دواتر لە هەمان دێڕدا بەردەوام دەبێت.

Videoڤیدیۆ

Watch a videoسەیری ڤیدیۆیەک بکە

A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

User input in Python is easy + exercises ⌨️Bro Code · Englishئینگلیزی · 265K viewsبینین · 5.6K likesلایک

Reading what the user types with input(), turning it into numbers, and three small programs: Mad Libs, an area calculator and a shopping cart.

وەرگرتنی بە input()، گۆڕینی بۆ ژمارە و سێ بچووک: ⁦Mad Libs⁩، ڕووبەر و سەبەتەی کڕین.

Whole video (13:04): input() (0:00), Mad Libs (3:29), area calculator (7:10), shopping cart (9:42).

هەموو ڤیدیۆکە ⁦(13:04)⁩: input() ⁦(0:00)⁩، ⁦Mad Libs (3:29)⁩، ڕووبەر ⁦(7:10)⁩ و سەبەتەی کڕین ⁦(9:42)⁩.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

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

Making Decisions: if, elif, elseبڕیاردان: if، elif و else

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

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