Python Syntax Basics

In the last lesson, we learned how to write our first Python program. Now let’s learn the basic syntax rules Python expects us to follow.

After learning Your First Python Program, we wrote our first print() program and saw how Python runs lines from top to bottom. Now let’s learn the basic rules of Python.

What Is Python Syntax?

Syntax means the rules of a language.

In English, we cannot say “I apple eat” and expect it to make sense. The words are right, but the order is wrong. The rules matter.

Python also has rules. If we break them, Python gets confused and gives us an error.

The good news? Python’s rules are simple. Much simpler than English, honestly. Let me show.

1 Rule 1: One Statement Per Line

Each instruction goes on its own line.

print("Hello")
print("World")

Do not do this:

print("Hello") print("World")

That will cause an error. Python expects one thing per line. Think of it like writing a to-do list. One task per line. Clean and clear.

Rule 2: Python Is Case Sensitive

This one is important. Let’s read carefully. print and Print are not the same thing.

print("Hello") # This works
Print("Hello") # This gives an error

Same with variable names. name and Name are different. age and Age are different. myVariable and myvariable are different. So let’s be careful with capital letters. Python notices everything.

Rule 3: Indentation Matters in Python

This is the big one. Let’s pay attention.

In many languages, indentation — that means spaces at the start of a line — is just for looks. It makes code pretty. But it does not change anything.

In Python, indentation is part of the code. It changes what the code does.

Look at this:

if 5 > 2:
print("Five is greater than two")

The print line is indented. It is pushed to the right. That indentation is required.

If we write:

if 5 > 2:
print("Five is greater than two")

Python will give an error. Because the indentation is missing.

How much to indent? The standard is 4 spaces. Some people use the Tab key. But 4 spaces is the common choice. We will learn more about if statements later. For now, let’s just remember — indentation is not optional in Python. It is part of the syntax.

Rule 4: Strings Need Quotes in Python

Text must be inside quotes.

print("Hello") # Works
print('Hello') # Works
print(Hello) # Error

We can use single quotes or double quotes. Both work. Just be consistent. If we start with a double quote, end with a double quote. If we start with a single quote, end with a single quote. Do not mix them.

# Rule 5: Comments Start with # in Python

A comment is a note for humans. Python ignores it.

# This is a comment
print("Hello") # This is also a comment

We will learn more about comments in the next article. For now, let’s just know that # is how we write a comment.

Rule 6: Use the Right Brackets in Python

Python uses three types of brackets:

  • ( ) — parentheses
  • [ ] — square brackets
  • { } — curly braces

Each has a purpose. And here is the rule — if we open one, we must close it.

print("Hello") # Good
print("Hello" # Error - missing closing bracket

This is a very common mistake. We type the opening bracket. Then we get distracted. Then we forget the closing one.

Most code editors will help us with this. They close the bracket automatically. But still, let’s keep an eye on it.

Rule 7: No Semicolons Needed in Python

Some languages require a semicolon ; at the end of every line. Like this:

System.out.println("Hello");

Notice the semicolon at the end? Java requires it. Python does not.

print("Hello") # Good
print("Hello"); # Also works, but not needed

We can put a semicolon if we want. But it is not Python style. Let’s not do it. It looks weird to Python programmers.

Rule 8: Blank Lines Are Fine in Python

We can leave blank lines in our code. Python ignores them.

print("Hello")
print("World")

This is fine. The blank line does nothing. But it makes the code easier to read. Let’s use blank lines to separate sections of our code. Like paragraphs in a book.

Python Syntax: A Real-Life Example

Think of Python syntax like driving rules.

  • Drive on the correct side of the road
  • Stop at red lights
  • Use our turn signals

If we follow the rules, everything works. If we break them, we get in trouble. Python is the same. Follow the rules, and Python is happy. Break them, and Python gives us an error.

And just like driving, the rules become automatic after a while. We do not think about them. We just do them.

Let’s Put It All Together

Here is a clean Python program that follows all the rules:

# A simple program
print("Hello") # Print a greeting
print("My name is Sam") # Print a name
# Some math
print(5 + 3)
# Done
print("Goodbye")

Clean, right? That is good Python style.

Notice:

  • One statement per line
  • Comments start with #
  • Strings are in quotes
  • Brackets are closed
  • No semicolons
  • Blank lines separate sections

That is proper Python. That is what good code looks like.

Common Python Syntax Mistakes

Let me list the ones we see most often.

Mistake 1: Forgetting Indentation

if 5 > 2:
print("Hello")

Error. The second line needs indentation.

So what do we do? We add indentation.

if 5 > 2:
print("Hello")

Mistake 2: Using the Wrong Case

Print("Hello")

Error. It is print, not Print.

So what do we do? We use the correct case.

print("Hello")

Mistake 3: Unclosed Brackets

print("Hello"

Error. Missing the closing parenthesis.

So what do we do? We add the closing bracket.

print("Hello")

Mistake 4: Mixing Quotes

print("Hello')

Error. Starts with double quote, ends with single quote.

So what do we do? We match the quotes.

print("Hello")

Or:

print('Hello')

Both work. Just do not mix them.

Mistake 5: Adding Semicolons

print("Hello");

Not an error. But not Python style. Let’s remove the semicolon.

print("Hello")

Next, we will learn about Python Comments so we can continue step by step.