Introduction to if Statements
When a statement is entered, Python can execute it and provide a result immediately.
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?
Reveal answer
The text is printed to the screen.
The print statement receives the supplied text and promptly displays it as output.
Trace the Statement
- 1Python receives one statement.
- 2The statement calls print.
- 3The text hello world is supplied to print.
- 4Python prints that text to the screen.
hello worldWhat 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.
- 1Find the statement
The complete line is one Python statement.
- 2Find the operation
The operation is print.
- 3Find the supplied value
The supplied value is the text hello world.
- 4Observe 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
Common mistake Treating the displayed text as a second statement.
What it looks likeprint 'hello world' produces the text hello world on the screen.
Why it failsThe source describes the entered line as the statement. The displayed text is the output produced by that statement.
Do this insteadSeparate 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 likeThe source notes that print ends with an invisible newline character.
Why it failsThe newline causes repeated calls to print on separate lines.
Do this insteadRemember that print normally ends with \n.
Common mistake Assuming this example explains conditional behavior.
What it looks likeprint 'hello world'
Why it failsThe supplied material demonstrates a single print statement but does not define if syntax or conditional execution.
Do this insteadUse this example to learn what a statement is and how execution produces output; do not infer additional conditional rules from it.
Check Your Understanding
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
- 1A Python program is composed of statements.
- 2The introductory example contains one statement that calls print.
- 3The text supplied to print is promptly displayed on the screen.
- 4print normally ends with an invisible newline character, so repeated calls appear on separate lines.
- 5The 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.