Table of Contents
Python is one of the easiest programming languages to learn, thanks to its clean and readable syntax. If you're new to coding, understanding Python’s basic syntax will help you write efficient and error-free programs.
In this article, we’ll cover the fundamental syntax rules that every Python programmer should know.
1. Python Indentation
Unlike many other programming languages that use curly braces {}
to define code blocks, Python relies on indentation. This means that spaces or tabs are used to structure the code.
Example:
if True:
print("Hello, Python!") # This is correctly indented
If you don’t indent properly, Python will throw an error:
if True:
print("This will cause an IndentationError")
2. Comments in Python
Comments are used to make code more readable. Python supports single-line and multi-line comments.
-
Single-line comment: Use
#
before the comment.# This is a single-line comment print("Hello, World!")
-
Multi-line comment: Use triple quotes
'''
or"""
.""" This is a multi-line comment. It spans multiple lines. """ print("Multi-line comment example")
3. Variables and Data Types
In Python, you don’t need to specify a variable type explicitly. Python automatically detects the data type.
Example:
name = "Alice" # String
age = 25 # Integer
height = 5.6 # Float
is_student = True # Boolean
You can check the type of a variable using the type()
function:
print(type(name)) # Output: <class 'str'>
4. Printing Output
To display output in Python, use the print()
function:
print("Welcome to Python!")
You can also print multiple values:
name = "Alice"
print("Hello,", name)
5. Taking User Input
You can take input from the user using the input()
function:
name = input("Enter your name: ")
print("Hello,", name)
By default, input()
returns a string, so if you need an integer, you must convert it:
age = int(input("Enter your age: "))
print("You are", age, "years old")
6. Basic Operators
Python supports various operators, such as arithmetic, comparison, and logical operators.
-
Arithmetic Operators:
+
,-
,*
,/
,//
(floor division),%
(modulus),**
(exponentiation)x = 10 y = 3 print(x + y) # Addition print(x ** y) # Exponentiation (10^3)
-
Comparison Operators:
==
,!=
,>
,<
,>=
,<=
print(5 > 3) # True
-
Logical Operators:
and
,or
,not
print(True and False) # False print(not True) # False
7. Conditional Statements
Python supports conditional statements using if
, elif
, and else
.
age = 18
if age >= 18:
print("You are an adult.")
elif age == 17:
print("You are almost an adult.")
else:
print("You are a minor.")
8. Loops in Python
Loops are used to execute a block of code multiple times.
-
For Loop:
for i in range(5): print(i) # Prints 0 to 4
-
While Loop:
x = 0 while x < 5: print(x) x += 1
9. Functions in Python
Functions help to reuse code and keep programs organized.
def greet(name):
print("Hello,", name)
greet("Alice")

Zeyan Rhys is a Python developer and content writer at Syntax Notes, where he turns complex coding concepts into simple, beginner-friendly tutorials. He’s passionate about helping others understand Python in a way that actually clicks.