Concepts / Introduction to if Statements

Introduction to if Statements

When a statement is entered, Python can execute it and provide a result immediately.

  • Programming

Start with an Executed Instruction

Before working with conditional logic, begin with the basic idea of a Python statement. A Python program is composed of statements. When a statement is entered, Python can execute it and provide a result immediately. The source material introduces this process with a print statement.

What do you think happens?

What happens when Python receives a statement that calls print and supplies the text hello world?

  • The text is printed to the screen

  • The text is counted

  • The text is stored without being displayed

Reveal answer
Answer:

The text is printed to the screen.

The print statement receives the supplied text and promptly displays it as output.

Trace the Statement

python
  1. 1
    Python receives one statement.
  2. 2
    The statement calls print.
  3. 3
    The text hello world is supplied to print.
  4. 4
    Python prints that text to the screen.
Output
hello world

What a Statement Does

The example contains only one statement: a call to print that is supplied with the text hello world. The important foundation is the relationship between an instruction and its action. The statement asks Python to print a value, and Python displays that value on the screen.

Definition

A Python program is composed of statements. In the introductory example, the program has one statement, and that statement calls print with the text hello world.

A Second Execution Trace

Worked example

Following one print statement

Explain what the statement print 'hello world' does.

  1. 1
    Find the statement

    The complete line is one Python statement.

  2. 2
    Find the operation

    The operation is print.

  3. 3
    Find the supplied value

    The supplied value is the text hello world.

  4. 4
    Observe the result

    The text is promptly printed to the screen.

Result

The statement produces the displayed text hello world.

The execution trace is useful because it separates the instruction from the value being handled. print is the statement described in the source material, while hello world is the text supplied to it.

Key point

Repeated calls to print appear on separate lines because print ends with an invisible newline character, written as \n.

Useful Interpreter Support

When quick information is needed about a Python function or statement, the built-in help functionality can be used. The source gives help('len') as an example; it displays help for len, which is used to count the number of items.

Mistakes with the First Example

  • Common mistake

    Treating the displayed text as a second statement.

    What it looks like

    print 'hello world' produces the text hello world on the screen.

    Why it fails

    The source describes the entered line as the statement. The displayed text is the output produced by that statement.

    Do this instead

    Separate the instruction, print, from the supplied text and the resulting output.

  • Common mistake

    Expecting repeated print calls to remain on the same line.

    What it looks like

    The source notes that print ends with an invisible newline character.

    Why it fails

    The newline causes repeated calls to print on separate lines.

    Do this instead

    Remember that print normally ends with \n.

  • Common mistake

    Assuming this example explains conditional behavior.

    What it looks like

    print 'hello world'

    Why it fails

    The supplied material demonstrates a single print statement but does not define if syntax or conditional execution.

    Do this instead

    Use this example to learn what a statement is and how execution produces output; do not infer additional conditional rules from it.

Check Your Understanding

EASY

Describe the execution of print 'hello world' in four steps. Identify the statement, the operation, the supplied value, and the resulting output.

Hints
  • The complete entered line is the statement.
  • The operation is the name that requests output.
  • The supplied value is the text between the quotation marks.
  • The result is what appears on the screen.

Key Takeaways

  1. 1
    A Python program is composed of statements.
  2. 2
    The introductory example contains one statement that calls print.
  3. 3
    The text supplied to print is promptly displayed on the screen.
  4. 4
    print normally ends with an invisible newline character, so repeated calls appear on separate lines.
  5. 5
    The supplied material establishes the statement-and-output foundation but does not specify the behavior or syntax of if statements.

At a glance

  • A Python program is made of statements.
  • The source example uses print to display supplied text immediately.
  • The displayed text is output, not a separate statement.
  • print normally places a newline after its output.
  • The provided material introduces statements through print but does not define conditional if behavior.

Related concepts

More concepts starting with I