Home/Learn/Python A–Z/Syntax, Variables & Dynamic Typing

Syntax, Variables & Dynamic Typing

Beginner
Fundamentals

Python variables are names bound to objects. Indentation defines blocks, snake_case is the convention, and everything — numbers, strings, functions — is an object.

Overview

The mental model that unlocks Python: a variable is a NAME stuck on an OBJECT, like a label on a box. Assignment (=) never copies data; it points a name at an object. Two names can point at the same object — which is why understanding mutability (later) matters so much. Blocks are defined by indentation (4 spaces, no braces), statements end at end-of-line (no semicolons), and naming follows snake_case for variables/functions, PascalCase for classes, UPPER_CASE for constants.

Names, Objects & Assignment

id() shows an object's identity. When two names point to the same object, they have the same id. The is operator compares identity; == compares value — a classic interview trap.

is vs == — identity vs equality
a = [1, 2, 3]
b = a               # b now points to the SAME list (no copy!)
b.append(4)
print(a)            # [1, 2, 3, 4]  — a sees the change

c = [1, 2, 3, 4]
print(a == c)       # True  — same VALUE
print(a is c)       # False — different OBJECTS
print(a is b)       # True  — same object

# Multiple assignment & swap (no temp variable needed)
x, y = 10, 20
x, y = y, x         # swap in one line
print(x, y)         # 20 10

Indentation Is the Syntax

A colon (:) opens a block; the indented lines below belong to it. Mixed or inconsistent indentation is a syntax error, not a style issue. This forces readable code — the language will not run ugly nesting.

Blocks by indentation — 4 spaces, consistently
marks = 82

if marks >= 75:
    grade = "Distinction"      # 4 spaces — inside if
    print("Great job!")        # same block
else:
    grade = "Pass"

print(grade)                   # back at top level — runs always

# IndentationError examples:
# if marks > 40:
# print("pass")        <- not indented: SyntaxError
#     print("a")
#       print("b")     <- inconsistent: IndentationError

Key Points to Remember

  • 1Assignment binds a name to an object — it never copies
  • 2== compares values; is compares identity (same object in memory)
  • 3snake_case functions/variables, PascalCase classes, UPPER_CASE constants (PEP 8)
  • 4Use 4 spaces per indent level; never mix tabs and spaces

Interview Questions

Sign in to ask Aria
1

What is the difference between == and is in Python? Give an example where they differ.

EasyAccenture
2

b = a for a list, then b.append(x) — why does a change too? How do you actually copy?

MediumFlipkart

Ask Aria about Syntax, Variables & Dynamic Typing

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…