Python Syntax


Syntax refers to the structure and set of rules that define how Python code is written and interpreted.

Python is known for its simple, clean, and readable syntax, which makes it one of the most beginner-friendly programming languages.

Let’s explore the basic rules of Python syntax.


🧱 Basic Python Syntax Rules

✅ 1. Print Statement

To print text or values in Python:

print("Hello, World!")
✅ 2. Indentation

Python uses indentation (whitespace) to define blocks of code.
There are no curly braces {} like other languages.

if 5 > 2:
    print("5 is greater than 2")  # This line is indented

Incorrect indentation will raise an error.


✅ 3. Comments

Use # to write single-line comments:

# This is a comment
print("Hello")

✅ 4. Variables

No need to declare data types:

x = 10
name = "John"

✅ 5. Case-Sensitive

Python is case-sensitive:

a = 10
A = 20
print(a)  # Outputs 10
print(A)  # Outputs 20

✅ 6. End of Line

No need to add a semicolon ; at the end, but it’s allowed:

print("Hello")  # Correct
print("Hi");

✅ 7. Multiple Statements on One Line

Separate them using a semicolon:

x = 5; y = 10; print(x + y)

✅ 8. Multiple Line Statements

Use \ or parentheses:

total = 1 + 2 + \
        3 + 4

Or:

total = (1 + 2 +
         3 + 4)

🧠 Summary Table
Concept Example
Print print("Hello")
Indentation if x > 0:\n print(x)
Variable x = 5
Case-sensitive a ≠ A
Comment # This is a comment

Practice Questions

Q1. Write a Python program that prints your name.

Q2. Write a Python program to assign the number 50 to a variable and print its value.

Q3. Write a Python program using indentation with an if statement to print "Yes" if a condition is true.

Q4. Create a variable name and print a greeting using it (e.g., “Hello, John”).

Q5. Write a Python program that includes a comment above a print statement.

Q6. Use a backslash (\) to break a long line of code into two lines and print a result.

Q7. Assign a float and a string to two different variables and print both.

Q8. Write a Python program to print the sum of two numbers.

Q9. Use one line to declare two variables with values and print both.

Q10. Write a program to show that Python is case-sensitive by creating two variables: a and A, and print both.


Go Back Top