Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

The print() function

The function name (print in this case) along with the parentheses and argument(s), forms the function invocation. 

We'll discuss this in more depth soon, but we should just shed a little light on it right now. 

print("Hello, World!")

What happens when Python encounters an invocation like this one below? 

function_name(argument)

Let's see:

  • First, Python Checks if the name specified is legal (it browses its internal data in order to find an existing function of the name; if this search fails, Python aborts the code)
  • Second, Python checks if the function's requirements for the number of arguments allows you to invoke the function in this way (e.g., if a specific function demands exactly two arguments, any invocation delivering only one argument will be considered erroneous, and will abort the code's execution)
  • Third, Python leaves your coe for a moment and jumps into the function you want to invoke; of course, it takes your argument(s) too and passes it/them to the function;
  • Fourth, the function executes its code, causes the desired effect (if any), evaluates the desired result(s) (if any) and finishes its task
  • Finally, Python returns to your code (to the place just after the invocation) and resumes its execution
  •  

LAB

Estimated time

5-10 minutes

Level of difficulty

Very Easy

Objectives

  • becoming familiar with the print() function and its formatting capabilities
  • experimenting with Python code.

Scenario

The print() command, which is one of the easiest directives in Python, simply prints out a line to the screen. 

In your first lab:

  • use the print() function to print the line Hello, Python! to the screen. use double quotes around the string
  • having done that, use the print() function again, but this time print your First name;
  • remove the double quotes and run your code. Watch Python's reaction. What kind of error is thrown? 
    • SyntaxError: invalid syntax
  • then, remove the parentheses, put back the double quotes, - and run your code again. What kind of error is thrown this time? 
    • No Error, Hello, Q David Khieu
  • experiment as much as you can. Change double quotes to single quotes, use multiple print() functions on different lines. See what happens.