Python: Statements
When we're making a dish, we follow the recipe carefully. Otherwise, the food won't turn out as expected. The same rule applies to programming.
If you want to see the expected result on screen, the computer needs clear, step-by-step directions. This is done using instructions. An instruction is a command to the computer — a unit of execution. Python code is a set of instructions, presented like a step-by-step recipe.
Python code is run by an interpreter, a program that executes instructions strictly, line by line. Like the steps in a recipe, the instructions for the interpreter are written in order and separated from each other by a line break.
Instruction 1: print('Hello') → executed
↓
Instruction 2: print('World') → executed
↓
Instruction 3: print('!') → executedHere is an example of code with two instructions. When it runs, two lines appear on the screen one after another:
print('Mother of Dragons.') # first instruction
print('Dracarys!') # second instruction
# => Mother of Dragons.
# => Dracarys!Order matters
The interpreter executes code in exactly the order you write it. Swap the lines, and the output changes too:
print('Dracarys!')
print('Mother of Dragons.')
# => Dracarys!
# => Mother of Dragons.Developers need to keep the order of operations in mind and be able to mentally divide a program into independent parts.
Alternative syntax
Instructions are usually written on separate lines, but Python also allows multiple instructions on one line, separated by a semicolon ;:
print('Mother of Dragons.'); print('Dracarys!')There is no technical difference between the two versions — the interpreter handles them the same way. But the second version is harder to read, so in real projects instructions are always written one per line.
Instructions
Display three names, one after another: Robert, Stannis, Renly. The result should be that the following is shown on the screen:
Robert
Stannis
RenlyFor each name, use Python's own print() call.
Tips
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
import importlib
def test(capsys):
expected = "Заказ №1337\nСтатус: доставляется\nПримерный срок: 2 дня"
expect_output(capsys, expected)
def expect_output(capsys, expected):
importlib.import_module('solution')
out, _err = capsys.readouterr()
actual = out.strip('\n')
with capsys.disabled():
print('\n')
print(out)
assert actual == expectedTeacher's solution will be available in:
20:00
