BASIC OUTPUT

You can run Python programs in two ways;

  • By typing commands directly in python shell
  • By running a program stored in a file.

Throughout this course, we will be writing our programs in a file and not line by line.

In Python we use the print function to display content to the console. In order to display text onto the screen, you must use the format:

print("Put your output statement here")

Notice that the output statement has been placed between quotation marks.
Put your output statement here

This can also be done using single quotes
Put your output statement here

The differences between these two will be explained later. This is important as the computer needs to know that this is a string literal, or characters that are to be displayed onto the screen as they were entered.

Traditionally, the first program you create is the Hello, World! program, where you output those words onto the screen. Let’s try this now.

 

You Try

In the console provided below, insert the following code:

print ("Hello, World!")
print ('Hello, World!')

You should see the output

Hello, World!
Hello, World!

Notice that in this case, it does not matter if you use quotation marks or single quotes. Later on you may find reasons to choose one or the other.

Joining Statements

You can also join two statements together with the use of a comma (,). Using a comma will combine two statements together but will separate them by a space. For instance, the following piece of code:

print("Hello","World!")

will result in the output:

Hello World!

Don’t take my word for it thought. Let’s give it a shot!

You Try

In the console provided below, insert the following code:

print ("Hello","Mr. Bond")
print (32,34,36)

You should see the output

Hello Mr. Bond
32 34 36

Notice that the use of the comma will add a space AND output the next statement.
What happens if we don’t want a space and we want to add the two statements together? In that case, you use the plus sign (+).
Read on to learn more!

If you were to type in the following code:

print("Hello"+"Mr. Bond")
print(32+34+36)
 

will result in the output:

HelloMr. Bond
102
[/python]

The use of the plus sign will concatenate, or join, the statement in quotations together BUT will add any numbers together as, when dealing with numbers, the plus sign represents addition.
What happens if you try to concatenate a number to a statement?

print ("Merivale High School opened in "+1964)
 

This results in an error. The computer thinks you are trying to add the two together, as in mathematical addition. It doesn’t like it and it’s letting you know.

You Try

Using the examples listed above, create the code that will join two statements together (using both the comma and the plus sign) and seperate them with a space.

Comments

Comments are short notes placed in different parts of a program, explaining how those parts of the program work. Although comments are a critical part of a program, they are ignored by the Python interpreter. Comments are intended for any person reading a program’s code, not the computer.

In Python you begin a single line comment with the # character. When the Python interpreter sees a # character, it ignores everything from that character to the end of the line. For example,

#This would be a comment in Python
 

Comments that span multiple lines, used to explain things in more detail, are created by using either three quotation marks (“””) or three single quotes (”’) on each end of the comment. For example,

"""
This is an example of a multi-line comment. 
The interpreter will ignore each line in between the delimiters. 
Comment away!!!
"""
 

Since you will more likely be working on programs with other people in the future, this is very useful when you are trying to explain a section of code.

Here is an example of a comment at work.

#This program displays a person's name and address. 
print('Kate Austen') 
print('123 Full Circle Drive') 
print('Asheville, NC 28899')
 

You Try

Copy and paste the code below to see what will happen.

[python] <br />print ("This code will run just fine")<br />#print ("The interpreter will skip this line of code")<br /> [/python]

Escape Characters

An escape character is a special character that is preceded with a backslash (\), appearing inside a string literal (output statement). When a string literal that contains escape characters is printed, the escape characters are treated as special commands that are embedded in the string.

For example, \n is the newline escape character. When the \n escape character is printed, it isn’t displayed on the screen. Instead, it causes output to advance to the next line. For example, look at the following statement:

print('One\nTwo\nThree')

This code will result in the following output:

One
Two
Three

Python recognizes several escape characters, some of which are listed in below.

\\ – Causes a backslash character to be printed.
\’ – Causes a single quote mark to be printed.
\” – Causes a double quote mark to be printed.
\a – Causes an audio bell to sound.
\n – Causes output to be advanced to the next line.
\r – Causes the cursor to return to the beginning the current line.
\t – Causes output to skip over to the next horizontal tab position (Tab).

ASCII Chart

ASCII stands for American Standard Code for Information Interchange. Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as ‘a’ or ‘@’ or an action of some sort.

Not only do symbols and characters receive a numerical representation, but even newline and the warning bell receive a one as well. Have a look at the ASCII chart to see all of the characters that it represents.

Check Your Understanding

Before moving to the next section, let’s see how well you know the current material.

1. Write a program that will output the following statements:
–> Your Name
–> There ain’t no party like a computer science party ’cause a computer science party don’t stop!
–> A cow says “Moo”.

2. Write a program that uses only one print statement to print your first name on one line and your last name on the next.
3. Write a program that uses only one print statement to output the days of the week with a tab between them.
4. Write a program that will output the statement “What the …..?” follows by a *beep* (Note: This one has not been working as of late but give it a try)

When you feel you have completed all of the above problems, check your programs versus the sample solutions.