Page
2.2 Programming Page 2
Completion requirements
The print() Function
What is a function?
A function is a group of related statements that perform a specific task. Python has many built-in functions but you can also create your own, known as defining a function. Functions help us organize our code, especially as we develop longer programs.The print() function is one of these built-in functions in Python. That doesn't mean that whenever the word appears it is always a function name. By placing parenthesis after the word "print", we are telling Python to execute the function.
Running the function print() will produce a blank line. We call this empty because we haven't delivered any arguments to the function.
If we use double-quotations and write text within those quotations (like you did on the previous page), then the Output will print exactly as it appears. This is known as a string.
Running the function print() will produce a blank line. We call this empty because we haven't delivered any arguments to the function.
If we use double-quotations and write text within those quotations (like you did on the previous page), then the Output will print exactly as it appears. This is known as a string.
NOTE: Strings can be enclosed by either double or single quotes. For the sake of this course, we will use double-quotations for strings.
Arguments
are a very important component of functions. The argument is a value
that is passed to the function when it's called, essentially, we are
assigning a value to that function.
If you want to deliver one or more arguments to a function, you place them inside parenthesis.
Practice 2.2:
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. Almost anything you put inside the quotes will be taken, not as code, but as data.
Use the print() function to print the following lines:
- "Hello, Python!"
- Create a blank space
- Your first and last name
- Remove the parentheses and run your code. What kind of error is given?
- Replace the parentheses, indent your line this time and run your code again. What kind of error is given this time? We're going to talk about these errors soon.
- Experiment as much as you can. Change double-quotes to single quotes, use multiple print() functions on the same line, and then on different lines. See what happens.