Python · Basic Syntax

Complete Guide to Python Basic Syntax

Python is known for its clean and readable syntax. This guide covers the core syntax concepts you must understand before moving to advanced topics.

1. Indentation (Blocks)

Python uses indentation (spaces) to define code blocks instead of curly braces {}. Indentation is mandatory and part of the syntax.

✅ Correct:

if 5 > 2:
    print("Five is greater than two")

❌ Wrong:

if 5 > 2:
print("This will cause an error")

Rules:

2. Python Statements

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)

3. Comments

Comments are ignored by Python and used to explain code.

3.1 Single-line Comments

3.2 Multi-line Comments

4. Variables

Variables store data values. Python does not require explicit type declarations; it infers the type from the assigned value.

Rules:

5. Basic Data Types

Python automatically detects the data type based on the value.

TypeExample
int5
float3.14
str"Hello"
boolTrue / False
list[1, 2, 3]
tuple(1, 2, 3)
dict{"a": 1}
set{1, 2, 3}

Check the type using type():

6. Strings

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

7. Printing Output

Use print() to display output.

f-strings (recommended)

8. User Input

Use input() to get input from the user. It always returns a string.

Convert input to a number:

9. Python Operators (Basics)

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

10. Conditions (if / elif / else)

Conditions allow you to execute code based on decisions.

11. Boolean Logic

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

12. Loops

12.1 for Loop

Used to iterate over a sequence (like a list or range).

12.2 while Loop

Repeats as long as a condition is true.

13. Functions (Basic Syntax)

Functions are reusable blocks of code defined with def.

14. Importing Modules

Use import to bring in external or built-in modules.

Import specific names:

from math import pi
print(pi)

15. Python is Case-Sensitive

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.

16. Blocks and Basic Scope

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")

17. Python Keywords

Keywords are reserved words you cannot use as variable names.

Examples: if, else, for, while, def, class, import, True, False, None, etc.

18. Basic Python File Structure

The if __name__ == "__main__": block ensures that main() runs only when the file is executed directly, not when imported.

19. Common Beginner Mistakes

MistakeWrongCorrect
Indentation
if True:\nprint("Hi")
if True:\n    print("Hi")
Case Print("Hi") print("Hi")
Quotes "Hello "Hello"
Variable names 1name name1

20. Final Example: Small Python Program

Enter a number to calculate the radius:

This program combines variables, input, functions, math, and formatted printing.