Concepts / Introduction to if Statements

Introduction to if Statements

Notice that Python gives you the output of the line immediately! What you just entered is a single Python statement . We use print to (unsurprisingly) print any value that you supply to it. Here, we are supplying the text hello world and this is promptly printed to the screen.

  • 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. Python receives one statement.
  2. The statement calls print.
  3. The text hello world is supplied to print.
  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.

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

Following one print statement

Explain what the statement print 'hello world' does.

Find the statement: The complete line is one Python statement.

Find the operation: The operation is print.

Find the supplied value: The supplied value is the text hello world.

Observe the result: The text is promptly printed to the screen.

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.

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

  • Treating the displayed text as a second statement.

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

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

  • Expecting repeated print calls to remain on the same line.

    The newline causes repeated calls to print on separate lines.

    Fix: Remember that print normally ends with \n.

  • Assuming this example explains conditional behavior.

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

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

Key Takeaways

  • 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.