Part 3: Python Programming

2. Variables, Values & Dynamic Typing

Variables, Values & Dynamic Typing

A program is not very useful if it can only work with fixed numbers and text. We need a way to store information and refer to it later. That is what variables are for. A variable is a name that points to a value stored in the computer's memory.

Assignment

You create a variable by writing a name, an equals sign, and a value. This is called assignment:

PYTHON
age = 25
name = "Ada"
price = 9.99
print(age)
print(name)
print(price)
▸ OUTPUT
25
Ada
9.99

The equals sign does not mean "equal" as in mathematics. It means "take the value on the right and store it under the name on the left." You can read age = 25 as "age becomes 25."

Changing a variable

A variable can be reassigned at any time. Each new assignment replaces the old value:

PYTHON
score = 10
score = score + 5
print(score)
▸ OUTPUT
15

Here Python first works out score + 5 (which is 15) and then stores that result back into score.

Naming rules

Variable names must follow a few rules, and some conventions make your code easier to read:

  • Names may contain letters, digits, and underscores, but cannot start with a digit. total2 is fine; 2total is an error.
  • Names are case-sensitive: Age and age are two different variables.
  • You cannot use Python's reserved keywords (such as if, for, class) as names.
  • By convention, use lowercase words joined by underscores, like first_name or total_price. This style is called snake_case.
  • Choose meaningful names. n tells a reader little; number_of_students tells them a lot.

Dynamic typing and the type() function

Every value in Python has a type, which describes what kind of data it is. You can ask Python for the type of any value using the built-in type() function:

PYTHON
print(type(25))
print(type(9.99))
print(type("Ada"))
print(type(True))
print(type(None))
▸ OUTPUT
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'NoneType'>

Python is dynamically typed. This means you never have to declare what type a variable will hold; Python figures it out from the value you assign. It also means the same variable can hold different types at different times:

PYTHON
x = 42
print(type(x))
x = "now I am text"
print(type(x))
▸ OUTPUT
<class 'int'>
<class 'str'>

This flexibility is convenient, but be careful: a variable you think holds a number might have been changed to text elsewhere.

The core built-in types

You will meet these five fundamental types constantly. Here they are with example values.

TypeNameExample valueMeaning
intInteger25, -3, 0Whole numbers, positive or negative
floatFloating point9.99, -0.5, 3.0Numbers with a decimal point
strString"Ada", 'hi'Text, written inside quotes
boolBooleanTrue, FalseA truth value, used for decisions
NoneTypeNoneNone"Nothing here" / absence of a value

Notice that True and False start with capital letters, and None is written with a capital N. These are special values built into the language.

With variables you can now label and reshape data as your program runs. Practice by storing your own name, age, and favourite number in variables, then printing each with its type.