Getting started with Python
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
Python is a readable, general-purpose programming language. Meet the interpreter that runs it, try a line at the >>> prompt, then step through a four-line program, hello.py, that greets you, asks your name and says hello back. Finish with the one rule that surprises every beginner: spaces at the start of a line matter.
جرّب
0/2 تمارين ناجحة
Your first message
Store the text Hello, Python! in a variable called message, then print it.
اضغط Ctrl/Cmd + Enter للتشغيل. يضيف Tab مسافة بادئة؛ واضغط Escape ثم Tab لمغادرة المحرّر.
شغّل الشيفرة لرؤية مخرجاتها.
- تحقّق من الشيفرة عندما تكون مستعدًا.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01You save some Python lines in a file called game.py. How do you run it from a terminal?
python game.py starts the interpreter and tells it which file to run. On macOS and Linux the command may be python3; on Windows, py game.py also works.
02What does Python do with this line? # print("Hi")
A # starts a comment that runs to the end of the line. Python skips it, so even the print() inside it never runs.
03A program runs name = input("Name? ") and you type 42. What is stored in name?
input() always gives back what you typed as a string, even digits. int("42") would turn it into the number 42.
04What does print("Hi,", "Sam") show?
print() writes its items with one space between them and no quotes, then ends the line: Hi, Sam.
05Line 2 of a file starts with two spaces it does not need. What happens when you run the file?
Spaces at the start of a line group lines into blocks, so a stray indent is an error. Python checks the whole file before it runs any of it, so not even line 1 runs.
المسرد
- Python
- A readable, general-purpose programming language, used for scripts, websites, data and much more.
- Interpreter
- The program, python, that reads your Python code and carries it out.
- >>> prompt (REPL)
- Python's interactive mode: it reads a line, runs it, prints the answer and loops back for the next one.
- Script (.py file)
- A plain-text file of Python code, such as hello.py, run with python hello.py from top to bottom.
- Comment
- Text after a #. It is a note for people; Python skips it.
- print()
- A built-in function that shows values in the terminal, separated by spaces.
- input()
- A built-in function that writes a question, waits for you to type a line and press Enter, and returns that line as a string.
- Indentation
- Spaces at the start of a line. Python uses them to group lines into blocks; a stray one is an IndentationError.
الملاحظات والمصادر
- On macOS and Linux the command is often python3 rather than python; on Windows the py launcher also works. The $ and > at the start of a line are the terminal's own prompt: you do not type them.
- When the interactive prompt starts, Python first prints a short banner with its version number; the figure leaves it out.
- Python reads and compiles the whole file before it runs the first line, which is why a badly indented line 2 stops the program before Hello! is printed. In the real message, the ... is the full path to the file.
- input() always returns a str, even when you type digits; int() turns text such as "42" into a number (the next lessons show how).
- A .py file is plain text. Any code editor works; IDLE, the editor that comes with Python, can also run it.
- Python tutorial: Using the Python interpreter
- Python tutorial: An informal introduction to Python
- Python setup and usage: Command line and environment
- Python setup and usage: Using Python on Windows (the py launcher)
- Python reference: Lexical analysis (comments and indentation)
- Python library: Built-in functions (print, input)
- Python glossary (interpreter, interactive, REPL)
- Python library: IDLE
اختبار يتاح بعد الخطوة الأخيرة.