Syntax basics (indentation, print)

Computer Science · Python

Question 1 of 10

0

    How many lines does this Python program print? for i in range(6): print("loop")

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Syntax basics (indentation, print) explained

    Python programs are made of statements, normally one per line, and they run from top to bottom. Python marks the blocks of code that belong together with indentation rather than with braces. The lines inside an if, a loop or a function must be indented the same amount, usually four spaces. A missing or inconsistent indent stops the program with an IndentationError before any code runs.

    The print function displays values on the screen. It accepts several values separated by commas, and it puts a space between them. Each call ends with a new line unless you pass end="" or a different ending. In Python 3, print is a function, so its values go in brackets, and print "hello" on its own is a syntax error.

    A comment starts with the hash symbol # and runs to the end of the line. The computer ignores it, so comments are a good place to explain why the code does something.

    name = "Ada"
    age = 36
    print("Name:", name, "Age:", age)
    print("Next year:", age + 1, end=" years")
    print()
    

    Worked example

    Trace the program above.

    1. The first print shows its values with a space between each one: Name: Ada Age: 36.
    2. The second print shows Next year: 37 years. The end argument replaces the usual new line, so the output stays on that line.
    3. The empty print() adds only the new line that the previous print left out.

    The output is two lines: "Name: Ada Age: 36" followed by "Next year: 37 years".

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      How many lines does this Python program print? for i in range(3): print("loop")

    • Medium

      Counting every character printed (including separators and line breaks), how many characters does this Python program print? print(9, "cde", sep="|", end=" ") print("hi", 7, sep="|", end="!") print(7, 7, 7, sep="*", end=" ")

    • Hard

      Indentation decides what is inside each loop. How many lines does this Python program print? for i in range(3): for i in range(3): print("cell") print("cell end") print("row") print("done")