Lecture 2.2 · Interactive lab

Variables Lab

Watch values move into named boxes, test every operator, and play with text

Introduction to Data Science and Machine Learning
Md Mahbub E Noor · University of Barishal
All labs

Memory boxes: run code one line at a time

Each variable is a labelled box in memory. Step through the code and watch which box changes, what it now holds, and what type of value it is.

Press Run next line. The orange line is the one Python will run next.

Memory

Output

What to notice

  • = means store: the value on the right goes into the box named on the left.
  • x = x + 1 reads the old value first, then stores the new one.
  • Nothing updates by itself: share keeps its old value until a line recalculates it.

Try this

Operators: one pair of numbers, every operator

Change a and b and watch every result and its type update. Try negative numbers, decimals and zero.

Type whole numbers (like 7) or decimals (like 7.0).

Quick pairs

What to notice

  • / always gives a float, even for 10 / 5.
  • // rounds down, towards minus infinity: -7 // 2 is -4, not -3.
  • One float anywhere makes the whole result a float.

Try this

Text tools: the first building blocks of a spam filter

Type any message. Every box shows exactly what Python returns for it, live.

Positions (indexes): top row counts from the start, bottom row from the end

Your own expression

Use the names message and word, for example message.lower().count("e").

What to notice

  • len() counts every character, spaces included.
  • "free" in message is case-sensitive: FREE does not match until you use .lower().
  • Indexes start at 0, and -1 is always the last character.

Try this

f-strings: put values into sentences

Move the sliders. Each {…} in the f-string is calculated and replaced. After a colon you can choose the format.

Your f-string

Insert a format

Output

How each {…} was filled

What to notice

  • Forgetting the f prints the braces literally.
  • :.0% multiplies by 100 and adds a percent sign; :.2f keeps two decimals.
  • Rounding of exact halves goes to the even digit, as in round().

Try this

Works offline: no internet needed. The Python shown here runs in your browser with the same rules as Python 3, so results and error messages match what you will see in Jupyter.