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.


Python Syntax Quiz

Q1: What is the output of the print function when given a string "Hello"?

A. Hello
B. “Hello”
C. print("Hello")
D. Error

Q2: What is the main purpose of indentation in Python?

A. To make the code colorful
B. To define blocks of code
C. To comment the code
D. To decorate the function

Q3: Which symbol is used to add comments in Python?

A. //
B. /*
C. #
D. <!--

Q4: Python treats variable names with different cases as:

A. Identical
B. Invalid
C. Different
D. None of the above

Q5: Which of the following is a valid Python print statement?

A. print Hello
B. echo "Hello"
C. print("Hello")
D. printf("Hello")

Q6: What causes a syntax error in Python?

A. Correct indentation
B. Using comments
C. Missing colon after if statement
D. Assigning a variable

Q7: Python source files should be saved with which extension?

A. .py
B. .txt
C. .exe
D. .pyt

Q8: What does the print() function do?

A. Takes user input
B. Stops the program
C. Displays output
D. Declares a variable

Q9: Which of the following is true about Python?

A. It is not case-sensitive
B. Indentation is optional
C. Semicolons are mandatory
D. It uses indentation to structure code

Q10: Which of the following is allowed in Python syntax?

A. Using semicolon to separate statements
B. Using curly braces for indentation
C. Ignoring spaces in code
D. Defining variables with numbers first

Go Back Top