Skip to content
ComputerCS
Start learning

Python Programming · Lesson 2.2.1.1–2.2.1.3

Variables, Values & Data Types

Names that hold values: assignment, the four core types (int, float, str, bool) and how a name can be pointed at a different type any time.

01

Learning objectives

  • Create variables with = and explain assignment (right side evaluated first, then stored).
  • State the naming rules: letters/digits/underscore, no digit first, case-sensitive, no keywords.
  • Identify int, float, str and bool values and use type() to confirm them.
  • Explain dynamic typing — rebinding a name to a new type is legal.
  • Swap two variables with tuple assignment a, b = b, a.
02

Lesson notes

A variable is a name your program gives to a value so the value can be used later. Writing marks = 88 is an assignment statement, and it works in a strict order that beginners should memorise: Python first evaluates the right-hand side (here, just the number 88), then binds the name on the left to that value. From that line onward, every mention of marks fetches 88 — until some later line rebinds the name to something else. The humble = is therefore not mathematical equality but an instruction: make this name refer to this value.

Names must obey four rules. They may contain letters, digits and underscores — nothing else, so no spaces or symbols. They cannot begin with a digit (marks2 is legal, 2marks is not). Case matters: total and TOTAL are different variables. And they cannot be keywords — the 35 reserved words like if, for, while, class that form Python's grammar itself. Beyond these hard rules, lesson 1.3's soft rule applies: choose names that explain themselves.

Every value in Python has a type, and four of them carry almost the whole ICS course. int holds whole numbers (88, -3, 0). float holds decimals (88.5, 0.5, 3.0 — a decimal point makes it a float even if the fraction is zero). str holds text in quotes ("Ayesha", '88' — quotes make it text, not a number). bool holds exactly two values, True and False, and powers every condition in chapter 4. The built-in type() reveals any value's type — exam papers love asking what type(3.0), type("3") or type(True) return.

Python is dynamically typed: a variable is a flexible label, not a rigidly typed box. The sequence x = 5 then x = "five" is perfectly legal — the name x simply stops referring to the int and starts referring to the str. This is different from languages like C++ or Java, where a variable's type is fixed when declared, and it explains why Python never asks you to write a type next to a variable name. Freedom comes with responsibility: because nothing warns you, a name silently changing type mid-program is a classic source of logic bugs.

One idiom from this lesson appears in almost every exam paper: swapping two variables. The naive attempt a = b followed by b = a destroys the original a — after the first line both names hold b's value. Python's tuple assignment a, b = b, a fixes it elegantly: the right side builds a pair from the OLD values first, then unpacks them into the names. Memorise it as a unit, trace it once on paper with a = 3, b = 7, and it will never cost you marks again.

A named variable box holding a value.
A variable stores a named value
03

Worked code examples

Assignment, reassignment and reading types

Four variables, four types, one print each. type() confirms int, float and bool; the final line rebinds marks to 91, and every later use sees the new value.

student = "Ayesha"
marks = 88
attendance = 93.5
present = True

print(student, marks, attendance, present)
print(type(marks), type(attendance), type(present))

marks = 91   # reassignment: marks now refers to a new value
print(marks)
Output
Ayesha 88 93.5 True
<class 'int'> <class 'float'> <class 'bool'>
91

Quotes create text — not numbers

2 + 3 adds numbers to give 5, but "2" + "3" glues text together to give "23". The quotes decide everything: str + str concatenates, int + int adds.

a = 2 + 3
b = "2" + "3"
print(a)
print(b)
print(type(a), type(b))
Output
5
23
<class 'int'> <class 'str'>

The swap — naive attempt vs Python idiom

The naive swap loses a's value because a = b overwrites it before it can be copied. Tuple assignment evaluates b, a first and then rebinds both names — a perfect swap.

a = 3
b = 7
a = b      # a becomes 7; the 3 is lost
b = a      # b is still 7 — swap failed
print(a, b)

a, b = 3, 7
a, b = b, a   # right side evaluated first!
print(a, b)
Output
7 7
7 3
04

Key terminology

variable

A name bound to a value; the value can be read and replaced while the program runs.

assignment

The statement name = value: evaluate the right side, then bind the name on the left to it.

keyword

A reserved word that forms Python's grammar (if, while, def…) and cannot be used as a variable name.

dynamic typing

Python's rule that a variable may be rebound to a value of a different type at any time.

bool

The type with exactly two values, True and False — the result of every comparison.

05

Common misconceptions

Common mistake

In x = 5, the = means 'x equals 5' as in mathematics.

Correct understanding

= is assignment: evaluate the right side, then bind the name. x = x + 1 makes perfect sense as 'rebind x to its old value plus one'.

Common mistake

Python variables must have their type declared, like int marks;

Correct understanding

Python is dynamically typed — the type travels with the value, and no declaration is ever written.

Common mistake

2.0 and 2 are the same type of value.

Correct understanding

2 is an int, 2.0 is a float — the decimal point decides. They compare equal numerically, but type() reports different classes.

06

Check your understanding

1. Which of these is NOT a valid Python variable name?

2. What does type(3.0) return?

3. After x = 5 and then x = "five", what is x?

NERDLYY · FEEDBACK

Help shape Nerdlyy

Your feedback goes directly to the team. We read every submission.

No reply needed — you can send feedback without sharing contact details.