Skip to content

A1 - The print Statement and Expressions

Setup

Create a subfolder called Python inside a pre-existing folder you have for this course, either in your Google Drive (if you have Google Drive Desktop installed) or in your T: drive.

Open Visual Studio Code:

  • Install the Python extension (I'll show you how to do this)
  • Go to File -> Open Folder... and select the folder you just created
  • Within that folder, create a new file called a1.py

Getting Started in Python

Read through the introductory Python program below. If you see the symbol within any of the example code on this website, you can click on it for more information. You'll find three of these in the example below.

A Simple Python Program
# This line is a comment, because it starts with the hashtag symbol.
# Anything that follows this symbol on a particular line is ignored
# by Python. We use comments to explain the code we are writing.

# Here is how we print a greeting in Python:
print("Hello, World!")  # (1)!

# Here is how we can get Python to do some math for us:
print(6+4)  # (2)!

# This is an example of Python evaluating a Boolean expression:
print(1+1 < 5)  # (3)!
  1. Anything that is in quotes will be printed as-is. In computer programming, we call quoted text a string.
  2. Notice there are no quotes this time. Anything not in quotes will be evaluated as a mathematical expression.
  3. Unlike arithmetic expressions, such as 6+4, that evaluate to a number, a Boolean expression evaluates to either True or False. This expression will evaluate to True, since 1+1 is indeed less than 5.

The following are some of the most common mathematical operations supported in Python:

  • + adds two numbers
  • - subtracts two numbers
  • * multiplies two numbers
  • / divides two numbers
  • ** takes the first number to the power of the second number (eg, 5**2 is equal to 25)
  • % takes the mod or modulo of two numbers. It is what the remainder would be if the two numbers were divided. For instances, 8 % 3 is equal to 2, since three goes evenly into six, and then we have an extra two left over to bring us up to the 8.

All mathematical expressions follow BEDMAS rules. You can override these using brackets, just as you would in normal math. For instance:

  • print(2 + 3 * 4) will print 14, since it does multiplication first
  • print((2 + 3) * 4) will print 20, since it does the brackets first

Python, as with most programming languages, also includes comparison operators, which we use in Boolean expressions. Here are the most common ones.

Warning

If you're checking to see if two values are equal, be sure to use a double == sign. In Python, the single = sign is used for something different which we will see soon.

  • < checks if the left side is less than the right side
  • > checks if the left side is greater than the right side
  • <= checks if the left side is less than or equal to the right side
  • >= checks if the left side is greater than or equal to the right side
  • == checks if the left side and right side are equal
  • != checks if the left side and right side are not equal

Check Your Understanding

See if you can determine if the following Boolean expressions evaluate to True or False. The answers can be revealed by clicking on the icons.

print(2 < 3)  # (1)!
print(5 < 1)  # (2)!
print(1 + 1 == 2)  # (3)!
  1. True - Two is less than three
  2. False - Five is not less than one
  3. True - 1+1 does equal two

Your Task

Inside the a1.py file you created, paste in this sample program. It is the same as the program at the top of the page, but with some of the comments left out:

# Here is how we print a greeting in Python:
print("Hello, World!")  

# Here is how we can get Python to do some math for us:
print(6+4)  

# This is an example of Python evaluating a Boolean expression:
print(1+1 < 5)  

You should then add to this file:

  • One more print statement that outputs a string.
  • Ten more print statements that output arithmetic expressions that evaluate to 10. Each of the arithmetic operators shown above should be used at least once, and at least one statement should include brackets.
  • Ten more print statements that output Boolean expressions that evaluate to True. Each of the comparison operators shown above should be used at least once.