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

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. 

The print() function

Three important questions have to be answered as soon as possible:

  1. What is the effect the print() function causes?
    The effect is very useful and very spectacular. The function:
  • takes its arguments (it may accept more than one argument and may also accept less than one argument)
  • converts them into human-readable form if needed (as you may suspect, strings don't require this action, as the string is already readable)
  • and sends the resulting data to the output device (usually the console); in other words, anything you put into the print() function will appear on your screen. 

            No wonder then, that from now on, you'll utilize print() very intensively to see the results of your operations and evaluations. 

      2. What arguments does print() expect? 
           
Any. We'll show you soon that print() is able to operate with virtually all types of data offered by Python. Strings, Numbers, characters, logical values, objects - any of these may be successfully passed to print(). 

      3. What value does the print() function return? 
           None its effect is enough.