Syntax refers to the set of rules that define how a Python program is written and interpreted. Python’s syntax is known for being clean, readable, and simple. Unlike many other programming languages, Python uses indentation to define code blocks, which makes it easier to read and maintain.
Let’s look at a simple example:
example:
print("Hello, Python!")
This line of code prints text to the screen. There’s no need to use semicolons (;
) or curly braces ({}
) like in other languages.
In Python, indentation is not optional. It defines the structure of your code. For example, blocks of code within an if
statement or a loop must be indented.
example:
x = 10
if x > 5:
print("x is greater than 5")
print("Still inside the if block")
print("Outside the if block")
If you forget to indent properly, Python will show an IndentationError
.
Incorrect example:
if x > 5:
print("This will cause an error")
Comments are used to explain code and are ignored by Python when the program runs.
python
# This is a comment
print("Hello") # This prints Hello
python
"""
This is a multi-line comment
explaining what this code does.
"""
print("Welcome")
In Python, a statement is a line of code that performs an action.
python
name = "Alice"
print(name)
\
to split long lines.python
total = 1 + 2 + 3 + \
4 + 5 + 6
print(total)
Or use parentheses for cleaner syntax:
total = (
1 + 2 + 3 +
4 + 5 + 6
)
print(total)
Variables store data that your program can use and manipulate.
example:
x = 5
name = "Ravi"
is_active = True
You can also assign multiple values in one line:
a, b, c = 1, 2, 3
Or assign the same value to multiple variables:
x = y = z = 10
Python automatically detects the type of data stored in a variable.
example:
num = 10 # Integer
price = 10.5 # Float
name = "Ravi" # String
is_valid = True # Boolean
Use type()
to check the type of a variable:
print(type(name))
python
name = input("What is your name? ")
print("Hello,", name)
python
print("The total is", 10 + 5)
You can also use f-strings (formatted strings):
age = 25
print(f"You are {age} years old")
Identifiers are names used for variables, functions, classes, etc.
Must start with a letter or _
example:
my_var = 100
_user_name = "Ravi"
Examples of keywords: if
, else
, while
, for
, class
, def
, True
, False
, None
To see all keywords:
import keyword
print(keyword.kwlist)
Python is case-sensitive. This means:
name = "Ravi"
Name = "Raj"
print(name) # Ravi
print(Name) # Raj
These are treated as two different variables.
Unlike some other languages, Python does not require semicolons (;
) to end a line. Each new line is treated as a new statement.
However, you can write multiple statements on one line using ;
(though it's not recommended):
x = 5; y = 10; print(x + y)
Stick to one statement per line for better readability.
Python’s syntax is designed to be easy to read and beginner-friendly. Understanding the basics — such as indentation, variables, comments, and statements — sets the stage for everything else you'll learn in Python.
Next, we’ll look at how to work with variables and data types in more detail, including strings, numbers, and booleans.