The print Statement

  1. Each print statement is actually a use or call of the print function. The things to be printed are plugged into the function. A function is a series of python statements. For built-in functions like print we do not get to see the statements. A function is called by using its name, followed by parentheses containing its plug-in values. When a function is called its statements are executed.
  2. Format: The format of a print call is the command 'print' followed by parentheses containing zero or more expressions. These are the plug-ins. If there is more than one expression, then the expressions must be separated by commas. Example:

    print(3 + 2, "dog" + "cat")

    The print statement above consists of

    print
    (
    the expression 3 + 2
    a comma
    the expression "dog" + "cat"
    )

    Important: The word print must be typed in lower case letters.

  3. Semantics (What the statement does): Python computes the value of each expression and passes the values to the print function which prints the values from left to right separated by single spaces. The last thing the print function does is it sets up the location for the next print: down one line, all the way to the left of the window. The print statement is the only way programs can print values on the screen. This is different from the shell, where python will automatically print the value of an expression when the enter key is pressed.

For the problems below use the Python Shell:
Each line below consists of a number, a period, then a single Python statement.
Type each statement below into the Python Shell and then press the enter key.
TYPE IN ONLY THE STATEMENT!
Do NOT type in the number and period preceeding the statement.

When you press the enter key Python executes (carries out) the command. For each statement record the result that Python displays.

  1. print(3)
  2. print(3, 2)
  3. print(3 + 2 * 5)
  4. print(8 // 5)
  5. print(8 % 5)

Homework H1. (Homework one). Once you have recorded the results, logon to your blackboard account. Click on Homework, then click on Homework H1. In the submission window enter five lines. The first line should start with a 1, then a space, then the result of the first statement above. The second line should start with a 2, then a space, then the result of the second statement above. Continue with this pattern. Then click Submit.

Homework H2. In the last command above what does the // operator do? What does the % operator do? Logon to blackboard. Click on Homework, then click on Homework H2. Write what // does in the submission window. Write what % does in the submission window. Click on Submit.

Next: Python Values and Types