What is Python Syntax? π€
Python syntax refers to the set of rules that define how a Python program is written and formatted. Unlike many languages, Python focuses onreadability and simplicity, making it one of the easiest languages to learn.
Note
1. Python Print Statement π¨οΈ
The print() function is used to display output.
print_example.py
print("Hello, World!")2. Indentation in Python (Very Important) β οΈ
Python uses spaces (not curly braces like C/Java) to indicate code blocks. Missing or incorrect indentation leads to errors.
indentation.py
if True:
print("This is inside the block")
print("This is outside the block")Note
3. Comments in Python π
Comments help explain your code. Python supports single-line and multi-line comments.
Single-Line Comment
single_comment.py
# This is a comment
print("Python Syntax")Multi-Line Comment
multi_comment.py
"""
This is a multi-line comment.
Useful for documentation.
"""
print("Learning Python!")4. Python Variables β¨
Variables are created when you assign a value. Unlike many languages, Python does NOT require declaring types.
variables.py
name = "Sathish"
age = 25
height = 5.8
is_active = True
print(name, age, height, is_active)5. Keywords in Python π
Python reserves certain words that cannot be used as variable names.
| Keyword | Usage |
|---|---|
| if | Conditional statements |
| else | Alternative branch |
| for | Looping construct |
| while | Looping condition |
| def | Define a function |
| class | Define a class |
| return | Return a value |
Note
6. Input From User β¨οΈ
Use input() to get data from the user.
input.py
username = input("Enter your name: ")
print("Welcome,", username)7. Basic Data Types π§
Python supports many built-in data types.
| Type | Example | Description |
|---|---|---|
| str | "hello" | String (text) |
| int | 25 | Integer |
| float | 3.14 | Decimal number |
| bool | True / False | Boolean |
| list | [1, 2, 3] | Ordered collection |
| tuple | (1, 2, 3) | Immutable sequence |
| dict | {"a": 1} | Key-value pair |
8. Operators in Python βββοΈβ
Arithmetic Operators
arithmetic.py
a = 10
b = 3
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
print(a % b) # Modulus
print(a ** b) # PowerComparison Operators
comparison.py
x = 5
y = 10
print(x == y)
print(x != y)
print(x > y)
print(x < y)Logical Operators
logical.py
is_admin = True
is_active = False
print(is_admin and is_active)
print(is_admin or is_active)
print(not is_admin)9. Conditional Statements π§
condition.py
age = 20
if age >= 18:
print("Adult")
else:
print("Minor")10. Loops in Python π
For Loop
for_loop.py
for i in range(5):
print(i)While Loop
while_loop.py
count = 1
while count <= 5:
print(count)
count += 111. Functions in Python π§©
Functions help organize reusable logic.
function.py
def greet(name):
print("Hello,", name)
greet("Sathish")Conclusion π
Great job! You've learned the fundamental syntax rules of Python. Understanding these basics will help you write clean, readable, and professional-grade Python code.
Want a tutorial on the next topic? Just tell me! π