Variables

Storing and Reusing Information

What it is

The Variables category contains blocks for storing information under a name so that the program can use or change that information later.

A variable can be thought of as a labeled storage space.

For example:

temperature

could store a sensor reading, while:

counter

could store how many times an event has occurred.

Variables allow information to move between different parts of a program:

Sensor → Variable → Logic → Control → Output

Think of a variable as a labeled box. The variable name identifies the box, and the value is the information currently stored inside it.


Variables Blocks

BlockBlock ImageWhat It DoesConfirmed Generation / Options
Variable Value
Provides the value currently associated with the selected variable.x
Set Variable
Assigns a new value to the selected variable.x = 0
Convert Type
Converts the supplied value using the selected data-type operation.Confirmed: int, float, bool, str, bytes.
Global Variable
Generates the global-variable declaration operation provided by Clu-Blocks Pro.Confirmed example: global None.

Python Variables and Type Conversion Blocks

BlockBlock ImageWhat It DoesWhat the Options Mean
Variable
Reads the current value stored in the variable x. A variable is a named space used to store information.x: The name of the variable whose value Python will use.
Variable Assignment
Creates or updates the variable x and stores the value 0 in it.x: Variable name. 0: Initial value assigned to the variable.
Integer Conversion
Converts a value into a whole number (integer), without decimal places.0: The value to convert. Example: int("25") produces 25.
Float Conversion Converts a value into a decimal number (floating-point number).0: The value to convert. Example: float("3.5") produces 3.5.
Boolean Conversion Converts a value into a Boolean value: True or False.0: The value to test. 0, None, and empty values become False; most other values become True.
String Conversion Converts a value into text (a string).0: The value to convert. Example: str(25) produces "25".
Bytes Conversion Intended to convert text into bytes using UTF-8 encoding. However, this exact syntax is not valid for converting a string in Python.0: The value shown in the block. "UTF-8": The character encoding. For text, use "Clu-Bots".encode("UTF-8").
Global Variable
Declares that a variable used inside a function belongs to the global scope, allowing the function to modify the variable created outside it.None: This appears to be the selected or default field value. It is not a valid variable name for a global declaration. Replace it with a variable name, such as global x.

Important Notes

  • int(), float(), bool() and str() are Python built-in conversion functions.
  • bytes() creates a bytes object; to encode text, use text.encode("UTF-8").
  • global is a declaration used inside a function, not a conversion function. The name after global must be a valid variable name.

1. Variables and Initial Values

The actual generation reference begins with:

x = None

This establishes the variable:

x

with the value:

None

For students, the important concept is:

VARIABLE NAME → VALUE

For example:

x → None

Later, the variable can receive another value:

x = 0
``

The information stored in a variable can therefore change while the program runs.


2. Using a Variable

[ INSERT BLOCK IMAGE HERE: Variable Value ]

The simplest variable block displays:

x

and generates:

x
`

This block does not assign a new value.

Instead, it allows another block to use the value currently associated with x.

Conceptually:

Variable → Get Stored Value → Use It

The value can then be connected to another operation.

For example:

Variable → Math

Variable → Logic

Variable → Serial Port

Variable → LCD / OLED


3. Assigning a Value

[ INSERT BLOCK IMAGE HERE: Set Variable ]

The visible example is:

x = 0

and Clu-Blocks Pro generates:

x = 0
``

This stores the value:

0

in:

x

A simple way for students to read the operation is:

Put 0 into x.

Conceptually:

VALUE → VARIABLE

or:

0 → x


4. SET vs. GET / USE

This distinction is essential for beginners.

SET

Assign information:

x = 25

Conceptually:

25 → x

GET / USE

Use:

x

Conceptually:

x → Stored Value

A useful memory rule is:

SET puts information into a variable. Using the variable block takes the stored information wherever it is needed.


5. Variables Can Change

A variable does not have to keep the same value forever.

For example:

counter = 0

after an event:

counter = 1

then:

counter = 2

then:

counter = 3

The conceptual progression is:

0 → 1 → 2 → 3 → ...

This is why variables are especially useful for:

  • Sensor readings
  • Counters
  • Timers
  • Program states
  • Received communication values
  • Calculated results

6. Converting a Value's Type

[ INSERT BLOCK IMAGE HERE: Type Conversion ]

This is the largest improvement from our previous Variables documentation.

The screenshot originally showed only:

int

but the actual Clu-Blocks Pro generation confirms five selectable operations:

int(0)
float(0)
bool(0)
str(0)
bytes(0, "UTF-8")

Therefore, the selector contains these confirmed conversions:

SelectionGenerated OperationMain Idea
intint(0)Convert the supplied value using int().
floatfloat(0)Convert the supplied value using float().
boolbool(0)Convert the supplied value using bool().
strstr(0)Convert the supplied value using str().
bytesbytes(0, "UTF-8")Use the bytes conversion generated by Clu-Blocks Pro with UTF-8.

7. int

Selecting:

int

generates:

int(0)

int() performs the integer conversion supplied by MicroPython.

Conceptually:

VALUE → int → INTEGER VALUE

For students, an integer is a whole-number value such as:

0

5

25

100

This is useful for things such as:

  • Counters
  • Whole-number calculations
  • Integer sensor values
  • Number selections

8. float

Selecting:

float

generates:

float(0)

This applies floating-point conversion.

Conceptually:

VALUE → float → FLOATING-POINT VALUE

For introductory purposes, students can think of a float as a numerical representation that can include a decimal part.

For example:

2.5

or:

18.75

The important initial distinction is:

int → whole-number representation

float → floating-point numerical representation


9. bool

Selecting:

bool

generates:

bool(0)
``

This applies Boolean conversion to the supplied value.

Conceptually:

VALUE → bool → BOOLEAN RESULT

This creates a natural connection with the Logic category.

A Boolean value is used in logical operations involving:

True

and:

False

Detailed truth-value conversion rules can be introduced later rather than overwhelming beginners here.


10. str

Selecting:

str

generates:

str(0)

This converts the supplied value into a string representation.

Conceptually:

VALUE → str → TEXT

This is particularly useful for the teaching approach used throughout the course because values frequently need to become readable messages.

For example:

Sensor Value

↓

str

↓

Text

↓

Serial Port

↓

PC

This connects Variables → Text → Serial Port.


11. bytes

Selecting:

bytes

generates:

bytes(0, "UTF-8")

The important documentation rule here is to preserve what Clu-Blocks Pro actually generates.

The confirmed operation uses:

UTF-8

as part of the generated expression.

Advanced Concept: Students do not need to understand byte representation when first learning Variables.


Type Conversion Is Not Assignment

This distinction is important.

Consider:

x = 0

This is an assignment.

But:

int(0)

is a conversion operation.

Therefore:

Assignment

WHERE should this information be stored?

Conversion

HOW should this value be represented?

Students should not treat the type selector as creating a fundamentally different kind of variable.

The confirmed generated operations are functions applied to values:

int(...)
float(...)
bool(...)
str(...)
bytes(...)

12. Understanding None

The supplied generation contains:

x = None

Students already encountered None in the Logic category, where the visible null block generates:

None

Here, None appears as the initial value associated with:

x

For beginner documentation:

None represents the absence of a normal value.

The conceptual relationship is:

Variable exists → No normal value assigned yet → None


13. Global Variable

[ INSERT BLOCK IMAGE HERE: Global ]

The last Variables block relates to:

Python

global

The reference you supplied includes:

global None

The Functions generation also demonstrates the practical form more clearly:

def my_function():
global x
pass

For the beginner-facing Variables section, the concept should remain simple:

global is used when a function needs to work with a variable declared in the broader program scope.

This is an advanced concept and should be fully explained when students reach the Functions category.


14. Local Thinking vs. Global Thinking

Students do not need variable-scope terminology during their first Variables exercise.

Initially:

Create Variable → Store Value → Use Value

Later, when Functions are introduced:

Program Variable

↓

Function

↓

global variable reference

This creates a natural teaching bridge:

Variables first → Functions later → Global when needed


Variables and Sensors

Variables become extremely useful when information comes from sensors.

Conceptually:

Sensor

↓

READ

↓

Variable

↓

Serial Port

↓

PC

For example:

DHT11 Temperature → temperature variable → Serial Port

The variable gives the measurement a meaningful name and allows the same information to be reused elsewhere.


Variables and Math

Variables can store calculated information.

For example, Math provides operations such as:

a += 1

Conceptually:

Read current value

↓

Calculate

↓

Store new value

This makes Variables and Math especially useful for counters:

Event → Counter + 1 → Store Counter → Serial Port


Variables and Logic

A variable can provide information to a logical comparison.

Conceptually:

Variable

↓

Comparison

↓

TRUE / FALSE

↓

Control

For example:

distance

↓

distance < limit

↓

IF

↓

Action

Variables therefore help connect sensor information with program decisions.


Variables and Communications

Received information can also be stored.

Conceptually:

Bluetooth / IR / Radio / Network

↓

Received Information

↓

Variable

↓

Process / Compare / Display

This becomes increasingly important as programs become larger.


Variables and Serial Port

For beginners, variables can feel invisible because a stored value exists inside the program.

Serial Port solves this problem.

The recommended first pattern is:

SET VARIABLE

↓

SEND VARIABLE THROUGH SERIAL

↓

OBSERVE ON PC

For example:

x = 25

then conceptually:

Serial → x

The student sees:

25

This makes the variable immediately concrete.


A Variable as a Program Memory

A useful beginner mental model is:

Name

temperature

Stored information

23

Use

Send it, display it, compare it, or calculate with it.

So:

NAME

↓

VALUE

↓

USE

This is simpler than beginning with abstract concepts such as memory addresses or variable scope.


Quick Guide for Students

  • Do I need to store information? Use: Set Variable
  • Do I need the stored information somewhere else? Use: Variable Value
  • Do I need an integer conversion? Select: int
  • Do I need floating-point conversion? Select: float
  • Do I need Boolean conversion? Select: bool
  • Do I need text? Select: str
  • Do I need the bytes operation generated by Clu-Blocks Pro? Select:  bytes
  • Do I need global? Usually not when first learning Variables.

Use global later when working with Functions and a function needs access to the corresponding broader-scope variable.


Quick Reference

ConceptConfirmed GenerationMain Idea
Initial Variablex = NoneVariable begins with the supplied initial no-value state.
Variable ValuexUse the value associated with x.
Set Variablex = 0Assign a value.
Integer Conversionint(0)Convert using int().
Float Conversionfloat(0)Convert using float().
Boolean Conversionbool(0)Convert using bool().
String Conversionstr(0)Convert using str().
Bytes Conversionbytes(0, "UTF-8")Use Clu-Blocks Pro's confirmed bytes conversion.
GlobalglobalGlobal-variable declaration used with broader program scope.