Basic

Variables and Types

Store and name data using Python variables and understand the core built-in types.

What you will see

False True

About 3 min of reading

Try It

A variable is a label tied to a value, and Python already knows what kind of thing that value is. I create a few, ask each one what type it holds, then watch Python turn one type into another and see how names can share a single mutable object.

1

I start with four values that look plain but each carry a different kind: a name, an age, a height, a yes/no flag. I assign them to names and ask Python what type each one actually is, so the invisible types become visible on screen.

name = "Ada" age = 30 height = 1.75 is_active = True print(age, type(age).__name__) print(height, type(height).__name__) print(name, type(name).__name__) print(is_active, type(is_active).__name__)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
30 int 1.75 float Ada str True bool
type(age) returns a long object, so I print its __name__ to get the short word. The name holds the value; it is not the value itself.
2

Now I want to change the kind of a value on purpose. I turn a number into text, a string of digits into an int, and a whole number into a float, then confirm what Python produced each time.

pi = str(3.14) print(pi, type(pi).__name__) whole = int("42") print(whole, type(whole).__name__) value = float(10) print(value, type(value).__name__)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
3.14 str 42 int 10.0 float
int("42") works but int("42.5") would raise ValueError, because a float-string is not a whole number. Convert to float first.
3

f-strings let a value grow inside text. I put a name and an arithmetic expression straight into one print and watch Python fill them in as it runs the line.

name = "Ada" age = 30 print(f"{name} will turn {age + 1} next year")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Ada will turn 31 next year
The leading f before the quote is what tells Python to evaluate the {…} parts. Without it the braces would print as plain text.
4

A name is a reference, not a box. I make one list, give it a second name, then grow it through the first. I expect the second name to see the change, proving both point to the same object.

a = [1, 2, 3] b = a a.append(4) print(b)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
[1, 2, 3, 4]
This surprises people: changing a also changes b. It is true only for mutable objects like lists and dicts, two names, one value.
5

Equality in Python also cares about type. I compare a number to its own string form and see the mismatch, then compare the converted number to itself and see them agree.

print(1 == "1") print(int(1) == 1)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
False True
1 == "1" is False because an int and a str are different kinds. Always convert before comparing when the value may arrive as text.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

A literal value is created, bound to a name, and later read back through that name.
1 Write a literal (e.g. 30) -> Python allocates an object
↓
2 Bind the name (age =) -> the name points to that object
↓
3 Read the name later -> Python follows the reference
↓
4 Convert or inspect -> type() or int()/float()/str() transform the value
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

name = "Ada" sets nothing on screen
age = 30 sets nothing on screen
height = 1.75 sets nothing on screen
is_active = True sets nothing on screen
print(age, type(age).__name__) 30 int
print(height, type(height).__name__) 1.75 float
print(name, type(name).__name__) Ada str
print(is_active, type(is_active).__name__) True bool
Why this works
What a variable really is

In Python a variable is not a container; it is a reference (a label) bound to an object. When you write age = 30, Python creates an int object 30 and binds the name age to it. The same object can have multiple names:

a = [1, 2, 3]
b = a        # b points to the SAME list
a.append(4)
print(b)     # [1, 2, 3, 4]

This matters for mutable objects like lists and dicts: two names can share one underlying value.

The core built-in types
TypeExampleMutableUse
int10nowhole numbers
float1.5nodecimals
str"hi"notext
boolTruenotrue/false
list[1, 2]yesordered sequence
tuple(1, 2)noimmutable sequence
dict{"a": 1}yeskey/value pairs
set{1, 2}yesunique values

Pick tuple over list when the data should not change; pick dict when you need labelled lookup.

Type coercion and conversion

Python converts types in two ways. Implicit conversion happens automatically in expressions, e.g. 1 + 2.5 becomes 3.5 (the int is promoted to float). Explicit conversion uses constructors: int(), float(), str(), list(), dict().

Mixed-type comparisons like 1 == "1" are False because the types differ. Always convert before comparing when the source may be a string.

Examples

Naming variables and assigning values

Declaring variables

A variable is a named label that points to a value in memory. In Python you assign with =.

name = "Ada"
age = 30
height = 1.75
is_active = True

name holds a string, age an int, height a float, and is_active a bool. Python figures out the type automatically (duck typing).

Why it works: the name lives in the current scope; the = operator binds that name to the object on the right. Reassigning creates a new binding rather than mutating the old one.

Checking and converting types

Inspecting and converting types

Use type() to inspect and the constructor functions to convert.

print(type(age))        # <class 'int'>
pi = str(3.14)          # '3.14' (str)
whole = int("42")       # 42 (int)
value = float(10)       # 10.0 (float)

Watch out: int("42.5") raises ValueError because it is not a whole number. Use float() first, then int().

String interpolation with f-strings

Building strings from variables

f-strings embed expressions directly inside strings.

name = "Ada"
age = 30
print(f"{name} will turn {age + 1} next year")
# Ada will turn 31 next year

How it works: the f prefix makes Python evaluate {...} expressions and insert their str() representation at runtime.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

Next Lesson
Strings and Text Operations
Strings and Text Operations →