Input and output
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A program talks to you through the terminal: print() sends output out, input() brings what you type back in. Step through a five-line program, age.py, that asks your name and age, turns the age into a number with int(), and prints a greeting and an f-string. Then see the classic mistake: forgetting that input() always gives back a string.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01A program runs n = input("How many? ") and you type 3. What is in n?
input() always returns what you typed as a string, even digits. int(n) would give the number 3.
02text holds '15'. What happens when Python runs text + 1 ?
+ cannot join a string and a number, so Python raises TypeError: can only concatenate str (not "int") to str. Write int(text) + 1 instead.
03What does print("a", "b", sep="-") show?
sep is what print() puts between items. It is one space by default; here it is a dash, so the output is a-b.
04age is 15. What does print(f"In a year: {age + 1}") show?
In an f-string Python works out the code inside the braces, age + 1 = 16, and puts the result into the text.
05Which line turns the answer '1.5' into a number you can do sums with?
float() reads decimals. int('1.5') raises ValueError, because '1.5' is not a whole number.
المسرد
- Output
- What a program shows you. print() writes output to the terminal (the standard output stream, sys.stdout).
- Input
- What you give a program while it runs. input() reads one line you type (from the standard input stream, sys.stdin).
- input()
- A built-in function that writes an optional prompt, waits for a line and returns it as a string, without the newline.
- Prompt
- The question input() writes before it waits, such as "Name? ". It is written exactly as given, with no newline.
- int() and float()
- Built-in functions that turn text such as '15' or '1.5' into a whole number or a decimal number.
- print()
- A built-in function that writes its items separated by sep (a space) and followed by end (a newline).
- f-string
- A string with an f before the quotes; code inside {braces} is worked out and its value put into the text.
- TypeError
- The error Python raises when an operation gets a value of the wrong type, such as adding a number to a string.
الملاحظات والمصادر
- On macOS and Linux the command is often python3 rather than python; on Windows the py launcher also works. The $ is the terminal's own prompt: you do not type it.
- input() reads one line. The Enter you press ends that line; input() removes the newline it adds and returns the rest, so the answer never contains it.
- int() ignores spaces around the digits, so int(" 15 ") is 15, but text that is not a whole number, such as "1.5" or "fifteen", raises ValueError. float() reads decimals.
- The figure shortens the error: the real traceback also names the file, the line and the code, and the terminal wraps the long last line.
- Behind the scenes print() writes to sys.stdout and input() reads from sys.stdin: the standard output and standard input streams of the program.
- Python library: Built-in functions, input()
- Python library: Built-in functions, print()
- Python library: Built-in functions, int()
- Python library: Built-in functions, float()
- Python tutorial: Input and Output (formatted string literals)
- Python reference: f-strings
- Python tutorial: Errors and Exceptions
- Python library: Built-in exceptions, TypeError
- Python library: sys.stdin and sys.stdout
اختبار يتاح بعد الخطوة الأخيرة.