Variables are one of the first concepts you must understand to write real programs. In Python, a variable is simply a name that points to a value in memory. You can think of it as a label you attach to data so you can reuse it later.
This lesson walks you through how to create variables, the rules for naming them, and the most common Python data types you’ll use right away.
In Python, you create a variable using the assignment operator: =
x = 5Here, = means: store the value ** in the variable named **.
In a Jupyter Notebook, you can run a cell by pressing Shift + Enter.

You can display a variable in two common ways:
print():x = 5
print(x)x = 5
xIn Jupyter, the last expression of a cell is displayed automatically.

Python also lets you assign multiple values in a single line. This is useful when you’re working with related pieces of data.
a, b, c = 1, 2, 3
print(a)
print(b)
print(c)Each variable on the left matches the value in the same position on the right.
This sets x, y, and z to 0.
x = y = z = 0A very common pattern is swapping two variables without a temporary variable:
a = 10
b = 20
a, b = b, aAfter this, a becomes 20 and b becomes 10.

So far, we’ve been creating variables at the top level of the notebook (outside of any function). In Python, those are often called global variables—meaning they’re available throughout your notebook (as long as the cell that defines them has been run).
Once you start writing functions, Python introduces an important idea called scope:
Here’s a simple example that shows the difference:
x = 5
def demo():
x = 10 # local to the function
print(x)
demo()
print(x)This prints 10 from inside the function, and then prints 5 outside—because the x inside the function is a different, local variable.
Output :

If you truly need to update a global variable from inside a function, you can use the global keyword:
counter = 0
def increment():
global counter
counter = counter + 1
increment()
counterUsing global variables too much can make programs harder to understand and debug. As you progress, you’ll usually prefer passing values into functions and returning results.

Python is strict about variable names. If you break the rules, Python won’t understand your code and will raise a SyntaxError.
x1 = 10
user_name = "Ryan"
age = 25A variable name cannot start with a number:
1x = 10That will cause a syntax error because Python doesn’t treat something like 1x as a valid name.
Output :

Variable names also can’t contain most special characters (like @, #, !, -). The underscore _ is the common exception.
Python allows uppercase letters in names, but by convention, most variables start with lowercase:
name = "Ryan"Starting names with uppercase can be confusing later because uppercase names are often used for things like classes (you’ll learn those later). Following the convention keeps your code readable and consistent.
Every value in Python has a type. The type tells Python what kind of data it is and what operations are allowed.
type()Python gives you a built-in function to check a value’s type:
x = 5
type(x)Try it with a string:
name = "Ryan"
type(name)[ Using type() in a notebook]
int, float, complexint) — whole numbersIntegers are whole numbers without a decimal point.
count = 3
price = 100
type(count)float) — decimal numbersFloats are numbers with decimals.
rating = 4.5
pi = 3.14159You might also see floats written like .9 (which is the same as 0.9).
x = 0.9
y = .9
x, yThe term “float” comes from how computers store decimal numbers—the decimal point position can effectively “float.”
[ Examples of int and float values]
complex) — advanced math valuesPython also supports complex numbers, which include an imaginary part. In many math textbooks the imaginary unit is written as i, but Python uses j.
z = 2j
type(z)You can do arithmetic with complex numbers too:
1j * 1jThis results in -1+0j (which is basically -1).
[ Complex number examples]
strText in Python is stored as a string (a sequence of characters). Strings are written inside quotes.
name = "Ryan"
message = "Hello, Python!"You can use single quotes too, but throughout this tutorial series, we’ll prefer double quotes for consistency.
You can join strings using +:
first = "Hello"
second = "World"
first + " " + secondIf you add two string numbers, Python still treats them as text:
"1" + "1"This gives "11", not 2, because Python is concatenating characters.
If you try to add a string and an integer:
"1" + 1Python raises a TypeError, explaining that it can’t concatenate a string and an integer. When you get errors in Python, it’s important to read them—most of the time, Python tells you what types it expected.
[ Example TypeError for string + int]
boolBooleans represent logical values: True and False.
is_active = True
is_admin = FalseNotice the capital letters. If you write true or false in lowercase, Python treats them as variable names and throws an error because those variables don’t exist.
true # NameError (not defined)[ True/False capitalization example]
list, tuple, rangeA sequence stores values in order.
A list is the most common sequence. It’s mutable, meaning you can change it after creating it:
nums = [1, 2, 3]
nums.append(4)
nums
A tuple is like a list, but it’s immutable (great for fixed collections like coordinates):
point = (10, 20)
type(point)A range represents a sequence of numbers and is often used in loops. It’s memory-efficient because it doesn’t store every number upfront:
r = range(5)
list(r) # convert to a list to see the values[ list vs tuple vs range examples]
dictA dict (dictionary) stores key → value pairs, which is perfect when you want to look up information by a label (like a name, id, or code).
student = {"name": "Ryan", "age": 25}
student["name"][Accessing values from a dict]
set, frozensetA set stores unique items (duplicates are automatically removed). This is useful for removing duplicates or checking membership.
tags = {"python", "data", "python"}
tagsA frozenset is an immutable set.
fixed_tags = frozenset(["python", "ml"])
type(fixed_tags)[Screenshot: set vs frozenset example]
NoneTypePython has a special value called None that represents “no value” or “not set yet.” It’s commonly used as a default value, a placeholder, or a function return when there’s nothing meaningful to return.
result = None
type(result)When you check the type, you’ll see NoneType.
[None and NoneType example]
bytes, bytearray, memoryviewThese types are used when working with raw binary data (common in file handling, images, audio, and network requests).
bytes is an immutable sequence of bytes.bytearray is a mutable version of bytes.memoryview lets you view and slice binary data efficiently without copying it.data = b"ABC" # bytes
mutable = bytearray(b"ABC") # bytearray
view = memoryview(data) # memoryview
data, mutable, view[0][bytes, bytearray, and memoryview example]
=) vs Comparison (==)This is one of the most important early distinctions.
= is assignmentIt stores a value in a variable:
x = 5== is comparisonIt checks whether two values are equal and returns a boolean:
1 == 1This returns True.
1 == 2This returns False.
So remember:
= puts a value into a variable== compares two values[ Comparing values using ==]
Type these into a notebook and run them one by one. Don’t rush—focus on understanding the output.
x = 10
print(x)name = "Asha"
print("Hello, " + name)a = "2"
b = "3"
print(a + b) # what do you expect?n = 2
m = 3
print(n + m) # compare with the previous resultprint(type(3))
print(type(3.0))
print(type("3"))print(5 == 5)
print(5 == 6)