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.
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.
Your exercise will be checked with these tests:
import runpy
def test(capsys):
expected = "Заказ №1337\nСтатус: доставляется\nПримерный срок: 2 дня"
runpy.run_module('solution')
out, _ = capsys.readouterr()
assert out.strip() == expectedTeacher's solution will be available in:
20:00
