Variables and operators¶
Variables¶
A variable is a container that stores a value. You create one by giving it a
name and assigning a value with =:
You can read the value later, change it, or use it in a calculation. The name on the left always receives the value on the right.
Data types¶
A data type describes what kind of value a variable holds. You need four of them:
int_variable = 1 # whole numbers, can be negative
float_variable = 1.0 # decimal numbers, can be negative
string_variable = "1" # text, written in quotes
bool_variable = True # True or False only
| Type | Name in Python | Example |
|---|---|---|
| Integer | int |
1, 0, -45 |
| Float | float |
1.0, 0.5, -12.75 |
| String | str |
"M1", "hello" |
| Boolean | bool |
True, False |
1 and "1" are not the same
1 is a number you can do maths with. "1" is text. Quotes make the
difference.
In MARLIN code you meet all four: port names like "M1" are strings, thruster
duty is a number, and robot.running is a Boolean.
Operators¶
Operators are symbols that perform an operation on values. There are four groups you need.
Arithmetic operators¶
These do maths and give back a number.
a = 5
b = 7
print(a + b) # Addition -> 12
print(a - b) # Subtraction -> -2
print(a * b) # Multiplication -> 35
print(a / b) # Division -> 0.7142857142857143
print(a // b) # Floor division, drops the remainder -> 0
print(a % b) # Modulus, the remainder only -> 5
print(a ** b) # Power, a to the power of b -> 78125
The results above are passed straight to print(). They are not stored, so
a and b do not change.
You can combine int and float values freely. You cannot combine numbers
with strings or Booleans directly.
Print things
print() is your main debugging tool. When you are not sure what a value
is, print it and look.
Assignment operators¶
These change the value of a variable. They are shortcuts.
a = 5
b = 7
a += b # same as a = a + b
a -= b # same as a = a - b
a *= b # same as a = a * b
a /= b # same as a = a / b
a %= b # same as a = a % b
a **= b # same as a = a ** b
Only a changes. b is left alone.
Comparison operators¶
These compare two values and return a Boolean.
a = 5
b = 7
print(a == b) # Equal -> False
print(a != b) # Not equal -> True
print(a > b) # Greater than -> False
print(a < b) # Less than -> True
print(a >= b) # Greater than or equal -> False
print(a <= b) # Less than or equal -> True
= is not ==
= assigns a value. == asks whether two values are the same. Mixing
them up is one of the most common beginner bugs.
Logical operators¶
These combine conditions and return a Boolean. Use them when you have more than one condition to check.
| Operator | Result |
|---|---|
and |
True only if both conditions are true |
or |
True if one or both conditions are true |
not |
Inverts the condition |
You can chain them, and brackets control the order:
This matters on a robot, where you often need two things to be true at once:
Full documentation: https://docs.python.org/3/library/stdtypes.html
What to read next¶
Data structures: how to store many values in one variable.