NotesPythonGetting Started

Variables & Data Types

How Python stores values, and the built-in types you'll use every day.

What a variable actually is

Think of a variable as a name tag you stick onto a value, not a box you pour a value into. When you write:

channel = "CodeWithMunnaX"

Python does two separate things: it creates the string value "CodeWithMunnaX" somewhere in memory, and then it ties the name channel to it, like tying a name tag onto a balloon. From then on, whenever you write channel in your code, Python looks up "what's this name tag currently attached to?" and uses that value.

This picture matters because it explains a question every beginner eventually asks: "if I change one variable, does it affect another?" Keep reading — you'll see the answer with actual code below.

Creating a variable — no declaration needed

In many languages you must announce a variable's type before using it (int x = 5;). Python skips this entirely. You just assign a value, and Python figures out the type from the value itself — this is what "dynamically typed" means.

channel = "CodeWithMunnaX"   # a piece of text -> Python treats this as a str
subscribers = 250000          # a whole number -> int
rating = 4.9                  # a number with a decimal point -> float
is_verified = True             # a yes/no value -> bool

Run the example below and look at what type() prints for each one — that's Python telling you exactly what it decided.

Why int and float are different, even though both are "numbers"

This trips up a lot of beginners: 250000 and 4.9 are both numbers, so why does Python care that one has a decimal point and the other doesn't?

Because they behave differently under the hood, and that difference shows up the moment you do math with them. Dividing two whole numbers with / in Python always gives you a float back, specifically so that information isn't lost:

print(10 / 2)    # 5.0, not 5 -- still a float, even though it divides evenly
print(type(10 / 2))

You'll use this constantly once you reach the Operators topic — for now, just remember: whole numbers are int, decimal numbers are float, and Python keeps them as separate types on purpose.

Strings and booleans

A string (str) is text, and it must be wrapped in quotes — either "double" or 'single', Python treats them the same way. Forgetting the quotes is one of the most common first-week mistakes:

channel = CodeWithMunnaX   # NameError! Python thinks CodeWithMunnaX is a variable name, not text
channel = "CodeWithMunnaX" # correct -- the quotes tell Python "this is text"

A boolean (bool) can only ever be True or False — capitalized exactly like that, no quotes. Booleans are what every comparison (5 > 3) and every if condition ultimately boils down to; you'll see this properly in the Conditional Statements topic.

The core built-in types, all in one place

TypeExampleWhat it's for
int42Whole numbers — counts, ages, IDs
float3.14Numbers with a decimal point — prices, measurements
str"hello"Text
boolTrue / FalseYes/no, on/off, true/false logic
list[1, 2, 3]An ordered, editable collection
tuple(1, 2, 3)An ordered collection that can't change
dict{"a": 1}Labeled data — a key paired with a value
set{1, 2, 3}A collection of unique items, no duplicates
NoneTypeNoneRepresents "nothing" or "no value yet"

Don't worry about memorizing list/tuple/dict/set right now — they each get their own full topic later in the Data Structures module. For this topic, focus on int, float, str, bool, and None.

Two functions you'll use constantly to work with types:

print(type(subscribers))            # tells you the type: <class 'int'>
print(isinstance(subscribers, int)) # True -- asks "is this an int?", used inside if-statements

None deserves a special mention: it means "no value" — it's not zero, not an empty string, not False, it's the deliberate absence of a value. You'll see it as the automatic return of any function that doesn't explicitly return something, and it's commonly used as a starting placeholder before a variable has a real value yet.

Reassignment — and why it doesn't affect other variables

Now back to that question from the top: does changing one variable affect another that was set equal to it? Walk through this line by line:

x = 10       # Python creates the value 10, and ties the name "x" to it
y = x        # y is now tied to the SAME value 10 -- not a separate copy, the same one
x = 20       # x is now tied to a brand-new value, 20 -- it does NOT overwrite the old 10
print(x, y)  # 20 10

The key insight: x = 20 doesn't reach into the old value and change it — it makes x point at a different value entirely. The old 10 still exists, and y is still tied to it. This is safe and predictable specifically because numbers, strings, and booleans are immutable — they can never be changed in place, only replaced. (Lists and dictionaries behave differently here — that's covered in their own topics, because it's important enough to deserve full attention there.)

Naming rules and conventions

A variable name can contain letters, digits, and underscores, but can't start with a digit, and it's case-sensitiveage and Age are two completely different variables to Python. Beyond what's legal, there's also what's conventional (agreed-upon style that makes code easier for other people — including future you — to read):

  • snake_case for variables and functions: subscriber_count
  • PascalCase for classes: YouTuber
  • ALL_CAPS for constants (values that never change): MAX_UPLOADS

Common first-week mistakes, so you can recognize them instantly

  1. Forgetting quotes around textname = Kabir raises NameError, because Python looks for a variable literally called Kabir. Fix: name = "Kabir".
  2. Mixing up = and == — a single = assigns a value; a double == compares two values. if x = 5: is a syntax error on purpose, to stop this exact mistake.
  3. Reassigning a variable to a completely different type — Python allows it (x = 5 then later x = "five"), because dynamic typing doesn't restrict you. It's legal, but it can make code confusing to read, so most style guides discourage it unless there's a good reason.
Key points to remember
  • A variable is a name tied to a value, not a box holding it — think 'name tag on a balloon', not 'container'.
  • You never declare a type — Python infers int / float / str / bool from the value you assign.
  • int and float are kept separate on purpose: dividing with / always returns a float, so precision isn't silently lost.
  • Reassigning one variable (x = 20) never affects another variable (y) that was previously set equal to it — each assignment points the name at a value, it doesn't overwrite the old one.
  • None means 'no value' — it's distinct from 0, '', or False, and it's what a function returns if it has no explicit return statement.
  • Convention: snake_case for variables/functions, PascalCase for classes, ALL_CAPS for constants — not enforced by Python, but expected by every other Python developer reading your code.

Every core type, and what type() says about each

Reassignment doesn't affect other variables

Revision Flashcards

1 / 5

Tap a card to flip it and see the answer.