← All chapters

Python curriculum / Chapter 01

Your First Program

Printing, comments, program headings, escape characters, and four first assignments.

Python - 1. Your First Program

1. Your First Program

1.1 Write your first program

1.2 Add more lines to your program (A1.1 - A1.3)

1.3 Comments

1.4 Program Headings

1.5 Escape Characters (A1.4)

Chapter 1: Assignments

A1.1 - Favorites

A1.2 - Knowledge is Power

A1.3 - Initials

A1.4 - Tricky Printing

Key Terms

1. Your First Program

1.1 Write your first program

print(“This is my first program!!!”)

There is no input for this program. The computer simply reads this line, using Python as the language to interpret it, and sends the output to the monitor. This should be your output:

This is my first program!!!

Whatever you put in between the quotes gets printed to the screen. Change the text between the quotes to whatever you want and have it printed to the screen.

1.2 Add more lines to your program (A1.1 - A1.3)

print(“This is my first program!!!”)

print(“It has more than one line.”)

print()

print(“Now the program is finished!”)

Output:

This is my first program!!!

It has more than one line.

Now the program is finished!

1.3 Comments

# - The pound sign, or hash-tag, indicates that everything to the right of it should not be interpreted by the computer as code.

’’’ - Triple single quotes are used to begin the comment. Everything is ignored by the computer until another set of triple single quotes is found.

print(“This is my first program!!!”) # I’m printing something here

This is the same program that we wrote originally, but there is more information in it that is supplied to someone reading the code. The code itself has not been changed. Everything after the # is ignored.

print(“This is my first program!!!”)

# print(“It has more than one line.”)

print(“Now the program is finished!”)

This would produce the following output:

This is my first program!!!

Now the program is finished!

1.4 Program Headings

# Program 1.2 First Program

# 10/04/20

# Josh Finkel

# This program demonstrates a print statement

It could also look like this:

################################################

### Program 1.2 First Program ###

### 10/04/20 ###

### Josh Finkel ###

### This program demonstrates a print statement ###

################################################

It could also look like this:

’’’

Program 1.2 First Program

10/04/20

Josh Finkel

This program demonstrates a print statement

’’’

Each of these headings are ignored by the computer when it is running the program. These three examples all get the job done but use different styles. The style you choose is up to you.

1.5 Escape Characters (A1.4)

He said “wow!”

If you use the following, you will get an error:

print(“He said “wow!”)

The problem is that the computer will see “He said “ as the quotes around the text to print and the rest of the line will confuse it. By using the escape character \ (a backslash), Python will understand that the next character should not have its usual action. To generate the desired output, you could write:

print(“He said \“wow!\””)

Notice that the first backslash allows the quote at the beginning of wow to be printed as a quote and not end the print statement text. The second quote is printed using the same technique.

  • Types of escape characters

\’ - single quote - Allows you to print a single quote

\” - double quote - Allows you to print a double quote

\\ - backslash - Allows you to print a backslash

\n - newline - Moves to a new line

\t - tab - Inserts a tab

Examples:

Output Code

  1. This is a backslash: \ print(“This is a backslash: \\”)
  2. “hi” print(“\”hi\””)
  3. Backslash in quotes: “\” print(“Backslash in quotes: \”\\\”)

Chapter 1: Assignments

A1.1 - Favorites

Write a program that prints, on separate lines, your name, your birthday, your hobbies, your favorite book, and your favorite movie. Label each piece of information as shown in the example. Match the sample as closely as possible, but with your own information.

Make sure you are putting a commented heading for your program.

PROGRAM STRUCTURE:

’’’

Program title, author, date and description here

’’’

# print the line with the name

# print the line with the birthday

# continue printing the lines with the hobbies, book and movie

SAMPLE RUN:

Name: Josh Finkel

Birthday: March 30, 1972

Hobbies: Spending time with annoying students

Favorite book: East of Eden

Favorite movie: Princess Bride

A1.2 - Knowledge is Power

Write a program that prints the phrase “Knowledge is Power inside a box made up of the characters “ = “ and “ | “. The last character is known as a pipe and can usually be found above the backslash on your keyboard:

Make sure you are putting a commented heading for your program.

PROGRAM STRUCTURE:

### Program title, author, date and description here

### Sample description: Displays a “Knowledge is Power” message in a box

###

# Add the code here to create the desired output.

# This may require multiple lines of code involving print statements.

DESIRED OUTPUT:

=============================

| |

| Knowledge is Power |

| |

=============================

A1.3 - Initials

This may take a little time and as always with programming trial and error is your friend here.

Write an application (program) that displays your initials in large block letters. Make each large letter out of the corresponding regular character. You can use a print statement for each line and make the final product as big as you want with a minimum of 6 lines high per letter.

SAMPLE OUTPUT: If your name is John Long, this is what your initials should look like.

JJJJJJJJJJJJJJJ LLLL

JJJJJJJJJJJJJJJ LLLL

JJJJ LLLL

JJJJ LLLL

JJJJ LLLL

JJ JJJJ LLLL

JJ JJJJ LLLL

JJJJJJJJJJJ LLLLLLLLLLLLLL

JJJJJJJJJ LLLLLLLLLLLLLL

A1.4 - Tricky Printing

Write a program with all three parts that prints the following. Don’t forget your heading with all relevant information:

Part A) His last words were “Don’t do it!”

Part B) \\

“Hi”

Part C) Use only one print statement to print the following two lines:

This is a backslash: \

And this is a backslash in quotes: “\”

Key Terms

code - the instructions that are written in a particular programming language.

comments - programmer-readable explanation in the code of a computer program. They are added with the purpose of making the code easier for humans to understand and are ignored by the computer.

# - The ampersand, or hash-tag, indicates that everything to the right of it should not be interpreted by the computer as code.

’’’ - Triple single quotes are used to begin the comment. Everything is ignored by the computer until another set of triple single quotes is found.

in-line comments - this type of comment is used to describe (in English) what is happening on that line of code.

commenting out - describes the action of taking a line of code out of the execution of a program without deleting it.

escape characters - also known as escape sequences, are used to help format printing in a print statement. Basic escape characters include:

\’ - single quote - Allows you to print a single quote

\” - double quote - Allows you to print a double quote

\\ - backslash - Allows you to print a backslash

\n - newline - Moves to a new line

\t - tab - Inserts a tab