Comments are lines of code that are not actually executed by the computer. They may be used to:
To create a comment in Python, use the #
symbol. Anything
after the #
will be ignored by the Python interpreter. For
example:
# This program calculates the area of a circle...
To output information in Python, you can use the
print()
function. Whatever is in between
the parentheses will be displayed in the console.
Example:
# Printing a string
print("SDEV 120 is the best class.")
# Printing a number
x = 1
print(x)
Common Mistakes:
Forgetting Quotes
Remember to wrap any strings (text) in single or double quotes.
# Will not work
print(SDEV 120 is the best class.)
Forgetting Parentheses
The print()
function requires parentheses. If you forget
them, you will get a syntax error.
# Will not work
print "SDEV 120 is the best class."
To get input from the user, you can use the
input()
function. This function will
display a prompt to the user and wait for them to press the
Enter
key.
Example:
# Get the user's name
name = input("Enter your name: ")
Common Mistakes:
Forgetting the Assignment
The input()
function will return the user's input, but
you need to store it in a variable.
# Will not give an error, but doesn't do anything useful
input("Enter your name: ")
Sloppy Prompts
The prompt - the text that the user sees in the console - will be displayed exactly as it is written inside of the quotes.
It is good practice to include a space at the end of the prompt (before the closing quote) to separate it from the user's input.
Run the following two commands and write your name in the console. Note the difference:
name = input("Enter your name:")
name = input("Enter your name: ")