Table of Contents
What is Python Syntax?
Syntax defines the rules that determine how Python code must be written. If you break the rules, Python will throw errors.
Python Indentation
Indentation in Python refers to the whitespace (spaces or tabs) used at the beginning of a statement to define the structure and scope of code blocks.
It is mandatory and unique to Python, serving the role that curly braces ({}) serve in other programming languages. Using improper indentation will give an IndentationError.
Indentation is used to define:
The body of a function (def)
The body of a loop (for, while)
The body of a conditional statement (if, elif, else)
The body of a class (class)
The standard convention is to use four spaces for each level of indentation. Consider the example program below which demonstrates the indentation for the code.
# 1. Function Definition (def)
def check_number(num):
# The lines below are the body of the function
# 2. Conditional Statement (if, else)
if num > 0:
# The line below is the body of the if block
print("Positive number found!")
# 3. Loop (for)
for i in range(num):
# The line below is the body of the for loop
print(f"Loop count: {i + 1}")
else:
# The line below is the body of the else block
print("Number is not positive.")
# 4. Class Definition (class)
class Example:
# The line below is the body of the class
class_name = "Indentation Test"
def display(self):
print(self.class_name)
check_number(2)
Python is Case Sensitive for the Variables we Use
In Python variable names, function names and keywords are all case-sensitive.
Consider the example below.
Name = "Jack" # Capital N
name = "John" # Small n
print(Name) # Jack
print(name) # John
In the above code
Name stores the value “Jack“
name stores the value “John“
Here N ≠ n, Python keeps these values in different variables.
So:
print(Name) prints Jack
print(name) prints John
Keywords like (if, else, def, class) and names that start with a digit (like 10 or 2a) cannot be used as variables. However built-in functions (like len(), sort()) and Python-defined methods can be used as variable names but doing so is strongly discouraged as it overwrites their original functionality.
Declaring a Variable
x = 10 # integer
y = 3.14 # float
text = "Hello" # string
In Python we don’t need to specify the data type while declaring a variable as it automatically detects the data type.
Python Statements
In python we don’t use semicolons at the end of the statement. Semicolons can be used to write multiple statements in the same line.
print("Hello World") # Hello World
a = 5; b = 10; print(a + b) # 15
Python Comments
Comments are mainly used to explain what the code you write does. It is ignored by the Python interpreter but loved by developers as they are useful to help explain their code. The ‘ # ‘ symbol is used to write a comment.
There are two types of comments in Python.
- Single Line Comment: They use the ‘ # ‘ symbol and used to write comments in a single line.
- Multi-Line Comments: Multi-line comments are written by using triple quotes.
# This is a single-line comment
print("Hello Python!")
"""
This is a multi-line comment.
You can use it to explain functions,
logic, or code blocks.
"""
print("Python is awesome!")
FAQ (Frequently Asked Questions)
1) What is syntax in Python?
Syntax defines the rules that determine how Python code must be written. If you break the rules, Python will throw errors.
2) Is there a switch case syntax in Python?
There is no switch case in Python like other programming languages but it uses a similar match case.
match value:
case option1:
pass
case option2:
pass
case _:
pass # default case
3) What is the syntax for nested if in Python?
The syntax for nested if is given below
if condition1:
if condition2:
pass+
4) What is a Python multiline comment?
Multiple line comments are written by using triple quotes. Consider an example below
“””
This is a multi-line comment.
You can use it to explain functions,
logic, or code blocks.
“””
5) How to add a comment in Python?
We can add a comment by using a ‘ # ‘ symbol followed by whatever comment line we want to add.
