Python (programming language) syntax refers to the rules that define how Python programs are written and structured. Python is known for its simple and readable syntax, which makes it easy for beginners to learn and use.
Unlike many other programming languages, Python uses indentation instead of braces {} to define blocks of code.
β¨ Example of Python Syntax
A simple Python program:
print("Hello, World!")
Output:
Hello, World!
This program prints the message Hello, World! to the screen.
π Python Indentation
Indentation refers to the spaces at the beginning of a line of code. In Python, indentation is very important because it defines blocks of code.
Example:
age = 18
if age >= 18:
print("You are an adult")
Here the print() statement is indented to show that it belongs to the if block.
If indentation is incorrect, Python will produce an error.
π¬ Python Comments
Comments are used to explain code and make it easier to understand. Python ignores comments during execution.
Single-line comment
# This is a comment
print("Python is easy to learn")
Multi-line comment
"""
This is a
multi-line comment
"""
π€ Case Sensitivity
Python is case-sensitive, which means variable names with different cases are treated as different variables.
Example:
name = "John"
Name = "Mike"print(name)
print(Name)
Output:
John
Mike
π¦ Multiple Statements
You can write multiple statements on separate lines.
x = 5
y = 10
print(x + y)
Python prefers one statement per line to keep the code readable.
β Key Points of Python Syntax
- Python uses indentation instead of braces.
- Code is written in simple and readable format.
- Python is case-sensitive.
- Comments help explain code.
- Statements are usually written one per line.