Gyaan

Variables and Data Types

beginner variables data-types dynamic-typing

In Python, a variable is just a name that points to an object in memory. We don’t need to declare types — Python figures it out at runtime. This is called dynamic typing.

Creating Variables

There’s no let, const, or var keyword. We just assign a value and Python handles the rest.

name = "Manish"       # str
age = 25              # int
height = 5.9          # float
is_active = True      # bool
nothing = None        # NoneType

The same variable can point to different types at different times. Python won’t complain.

x = 10          # x is an int
x = "hello"     # now x is a str — totally fine

Built-in Data Types

Python comes with these core types out of the box:

  • int — whole numbers like 42, -7, 1_000_000 (underscores for readability)
  • float — decimal numbers like 3.14, -0.5
  • complex — numbers with a real and imaginary part like 3 + 4j (rarely used outside math)
  • boolTrue or False (capitalized, and they’re actually subclasses of int)
  • str — text like "hello" or 'world'
  • None — Python’s version of “nothing” or “no value”

Checking Types

We can use type() to see what type something is, and isinstance() to check if it belongs to a type.

x = 42
print(type(x))              # <class 'int'>
print(isinstance(x, int))   # True

# isinstance can check multiple types at once
print(isinstance(x, (int, float)))  # True

The difference? type() gives us the exact type, while isinstance() also respects inheritance. Since bool is a subclass of int, isinstance(True, int) returns True.

Naming Conventions

Python has strong conventions (from PEP 8):

  • Variables and functionssnake_case (e.g., user_name, total_count)
  • ConstantsUPPER_SNAKE_CASE (e.g., MAX_RETRIES, API_KEY)
  • ClassesPascalCase (e.g., UserProfile)
  • Names starting with _ are “private by convention” (not enforced)
  • Names starting with __ trigger name mangling in classes
# Good
user_age = 25
MAX_CONNECTIONS = 100

# Avoid
userAge = 25       # camelCase is not Pythonic
x = 25             # too vague

In simple language, Python variables are just labels we stick on objects. The object knows its own type — the variable doesn’t care.