The Grammar of Python
Every spoken language has rules. If you say in English, "The dog jumped fence over the," a person might still guess what you mean, even though it is grammatically incorrect.
Programming languages have grammar rules too. In the programming world, we call these rules Syntax.
The difference between a human and a computer is that a human can guess your intent. The Python translator cannot. If you break a syntax rule, the computer immediately throws its hands up and says, "I don't know what this means!"
When this happens, Python gives you a SyntaxError. It is not trying to be annoying; it is just incredibly literal and needs you to fix the grammar before it can proceed.
For example, look at this broken code. It is missing the parentheses that the print function requires:
print "Hello, World!"
If you tried to run that, Python would stop and point out the error. Learning Python is really just learning its syntax rules!
Case Sensitivity
Because Python is so literal, it pays attention to every single character, including whether a letter is uppercase or lowercase. We call this being case-sensitive.
To you and me, "Print" and "print" mean exactly the same thing. To Python, they are two completely different words. If you capitalize a built-in command that is supposed to be lowercase, Python simply will not recognize it.
Try running the code below. The first line is written with the correct syntax. The second line has a capital "P". Notice how Python handles it:
When you run this, you will see the first line prints out just fine, but then the program crashes with a NameError (meaning it doesn't know what the name Print is).
If you ever get an error in your code, the very first thing you should check is your capitalization and spelling. One wrong keystroke is all it takes!
Leaving Notes for Yourself
Sometimes you need to write something in your digital notebook that isn't meant for the computer to read. You might want to leave a reminder for yourself, explain a complex idea to another programmer, or temporarily disable a line of code while you are testing things out.
We do this using comments.
A comment is text in your code that the Python translator completely ignores. In Python, you create a single-line comment using the hash symbol (#). As soon as Python is reading from left to right and sees a #, it immediately skips the rest of that line and moves on to the next one.
Run the code below. Notice how the computer completely ignores the human instructions and only executes the valid Python command:
Using the # symbol is incredibly helpful when you are trying to figure out why your code is broken. You can "comment out" certain lines to prevent them from running until you isolate the problem.
Multi-Line Comments & Best Practices
If you need to leave a longer note like a paragraph explaining how a complex feature works you can simply put a # at the beginning of each new line.
Alternatively, Python allows you to use three quotation marks (""") to wrap multiple lines of text. While technically a multi-line string, developers frequently use this as a multi-line comment:
""" This is a multi-line comment. You can write as much as you want here. Python will ignore all of it until it sees the closing quotes. """ print("Back to writing code!")
The Golden Rule of Commenting
As you start writing more code, it is tempting to comment on every single line. But professional developers follow a specific rule: Code tells you WHAT is happening. Comments tell you WHY.
Here is an example of a bad comment because it just states the obvious:
print("Game Over") # This prints Game Over
Here is a good comment because it explains the reasoning behind the code:
print("Game Over") # Player health reached zero, stopping the game loop.
Save your comments for explaining the intent behind your blueprints!
Lesson Summary
In this lesson, you learned the foundational ground rules for writing Python code:
- Syntax: This is the grammar of programming. If you break the rules, Python will stop and give you a
SyntaxError. - Case Sensitivity: Python treats uppercase and lowercase letters as completely different characters.
Printis not the same asprint. - Comments: Using the
#symbol allows you to leave notes for yourself and other developers. The computer completely ignores anything written after the hash symbol. - Best Practices: Good comments explain why the code was written, not what the code is doing.
Now that you understand the basic grammar rules, it is time to start writing your own original sentences. In the next module, we will learn how to store information using variables!
Lesson Quiz
Multiple Choice Questions (MCQs)
Fill-in-the-Gap Questions
Tasks
Print("Learning Python is fun!")
print("This line is missing something"
a. Fix the capitalization error on the first line.
b. Fix the missing syntax (a closing parenthesis) on the second line.
c. Add a single-line comment above the first line explaining exactly what you fixed.
d. Run the code using the Play button or the terminal to make sure it prints both lines without any errors!