Python Syntax Basics

Print Statement

Use print() to output data.

print("Welcome to Python!")

Variable Naming Rules

  • Must start with a letter or underscore _
  • Cannot start with a number
  • Can only include letters, numbers, and underscores
  • Case-sensitive (e.g., myVarMyVar)
name = "Alice"
_name = "Valid"
1name = "Invalid"

Indentation Is Mandatory

Python uses indentation (typically 4 spaces) to define blocks of code.

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

Incorrect:

if 5 > 2:
print("This will cause an IndentationError")

Comments in Python

Python uses the # symbol for single-line comments. Multi-line comments can be written using triple quotes.

# This is a single-line comment
print("Hello, world!")  # inline comment
"""
This is a multi-line string.
It can also act like a comment.
"""
print("This will still run.")

Statements and Line Endings

No semicolons required, but allowed if needed:

x = 1; y = 2; print(x + y)

Practice Question

Which of the following will cause a syntax error?

Loading...
Output:

Hint: Check the indentation of the if statement.

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic