Learn more about Python syntax and Python coding conventions.
A comment is a human-readable description of the instructions in a program.
Good programmers add comments to make their code easier to understand by other programmers. In Python, comments begin at the #
sign - everything after the #
is ignored by the computer.
Run the code below to see how comments are ignored by the computer.
In this example you can see how comments can help explain code to a different programmer. Even if this is the first time you had seen this puzzle, the comments explain what the functions are doing.
You can also create multiple line comments that will allow you to explain your code over multiple lines. To do this, use triple quotation marks """
The same rules apply, the computer will not try and perform the tasks given inside of the comment block. Comments can be useful for debugging as well.
You've probably already seen how careful you need to be when you're writing Python code. If you misspell one word or forget a parenthesis, your code won't run at all!
Programmers call these rules naming conventions. Using these conventions will make your code run without errors—and keep your code readable, too.
Did you notice how all the commands you've written so far are all lower-case letters?
When you are writing your own Python code, variable and function names always start with lowercase letters.
If a variable or function name has several words, you'll still want to make everything lowercase. And instead of using spaces, separate words with underscores.
These variables are not following the snake_case naming convention. Try to fix this code so it follows the naming rules above.
Let's learn one more tricky rule for naming your own variables and functions. You cannot use reserved words.
Reserved words are special commands that are built into the Python language. Because they already have special meanings to Python, you can't use them to name things.
You can't use def as a variable name because def is a reserved word.
Give the variable a different name, then print the variable on the next line.
Some common reserved words and their uses are:
def
: Declare a functionfor
: Create "for" loopsif
: Create "if" statementselse
: Create "if-else" statementswhile
: Create "while" loopsbreak
: Break out of loopsclass
: Declare a class definitionFix the code below so that it follows the naming rules we described in this lesson. Make sure you follow snake_case naming convention and rename any variables that are using reserved words!
Let's review what you've learned so far.
Just like with English, Python has its own grammar called syntax. Code that does not follow Python's syntax will produce a syntax error.
Spaces between values and in function calls does not not affect how the computer interprets the instruction.
A comment is a human-readable description of the instructions in a program.
Good programmers add comments to make their code easier to understand by other programmers. In Python, comments begin at the #
sign - everything after the #
is ignored by the computer.
You've probably already seen how careful you need to be when you're writing Python code. If you misspell one word or forget a parenthesis, your code won't run at all!
Programmers call these rules naming conventions. Using these conventions will make your code run without errors—and keep your code readable, too.