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.
# 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)!
- Anything that is in quotes will be printed as-is. In computer programming, we call quoted text a string.
- Notice there are no quotes this time. Anything not in quotes will be evaluated as a mathematical expression.
- Unlike arithmetic expressions, such as
6+4, that evaluate to a number, a Boolean expression evaluates to eitherTrueorFalse. This expression will evaluate toTrue, since1+1is 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**2is 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 % 3is equal to2, 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 firstprint((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.
True- Two is less than threeFalse- Five is not less than oneTrue-1+1does 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
printstatement that outputs a string. - Ten more
printstatements 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
printstatements that output Boolean expressions that evaluate toTrue. Each of the comparison operators shown above should be used at least once.