Python is known for its clean and readable syntax. This guide covers the core syntax concepts you must understand before moving to advanced topics.
Python uses indentation (spaces) to define code blocks instead of curly braces {}.
Indentation is mandatory and part of the syntax.
if 5 > 2:
print("Five is greater than two")
if 5 > 2:
print("This will cause an error")
Rules:
In Python, each line is usually a statement:
You can technically put multiple statements on one line (not recommended):
x = 5; y = 10; print(x + y)
Comments are ignored by Python and used to explain code.
Variables store data values. Python does not require explicit type declarations; it infers the type from the assigned value.
Rules:
_).age and Age are different).my_name, not my name).Python automatically detects the data type based on the value.
| Type | Example |
|---|---|
| int | 5 |
| float | 3.14 |
| str | "Hello" |
| bool | True / False |
| list | [1, 2, 3] |
| tuple | (1, 2, 3) |
| dict | {"a": 1} |
| set | {1, 2, 3} |
Check the type using type():
Strings represent text data and can be defined with single or double quotes.
name = "Python"
greeting = 'Hello'
String operations:
text = "Python"
print(text[0]) # P
print(text[-1]) # n
print(text.upper()) # PYTHON
print(len(text)) # 6
Use print() to display output.
Use input() to get input from the user. It always returns a string.
Convert input to a number:
Arithmetic operators:
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) # Exponentiation
print(a // b) # Floor division
Conditions allow you to execute code based on decisions.
Boolean expressions evaluate to True or False.
Logical operators:
x = True
y = False
print(x and y) # False
print(x or y) # True
print(not x) # False
for LoopUsed to iterate over a sequence (like a list or range).
while LoopRepeats as long as a condition is true.
Functions are reusable blocks of code defined with def.
Use import to bring in external or built-in modules.
Import specific names:
from math import pi
print(pi)
Python is known as a case-sensitive language because it differentiates between uppercase and lowercase characters during execution. Python treats two terms differently if their cases change, even though the characters are the same. If we try to retrieve a value with a different case, we get an error.
name and Name are two different variables.
A scope defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name.
if True:
print("Inside outer block")
if True:
print("Inside nested block")
Keywords are reserved words you cannot use as variable names.
Examples: if, else, for, while, def, class, import, True, False, None, etc.
The if __name__ == "__main__": block ensures that
main() runs only when the file is executed directly, not when imported.
| Mistake | Wrong | Correct |
|---|---|---|
| Indentation | |
|
| Case | Print("Hi") |
print("Hi") |
| Quotes | "Hello |
"Hello" |
| Variable names | 1name |
name1 |
Enter a number to calculate the radius:
This program combines variables, input, functions, math, and formatted printing.