Python syntax refers to the set of rules that define how a Python program is written and interpreted. It dictates how you structure code using elements like variables, functions, loops, and conditionals. Here are some key aspects of Python syntax:
1. Case Sensitivity
Python is case-sensitive, meaning that Var
and var
are treated as different identifiers.
2. Indentation
Indentation is used to define the structure of code blocks like functions, loops, and conditionals.
Python does not use curly braces {}
or keywords like begin
/end
; it relies on consistent indentation.
if x > 2:
print("x is greater than 2")
3. Comments
Single-line comments start with a #
symbol.
# This is a single-line comment
Multiline comments can be done using triple quotes '''
or """
:
'''
This is a
multi-line comment
'''
4. Variables
Variables do not need to be declared with a specific type.
Python dynamically assigns types based on the value assigned.
x = 20 # Integer
y = 5.67 # Float
name = "John" # String