Operators¶
The usual comparators are supported:
if 2 == 4:
print("something is wrong!")
if 1 != 1:
print("something is wrong!")
if 1 < 2:
print("that's what I thought")
Symbol |
Meaning |
---|---|
== |
Is equal to |
!= |
Is not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
The usual math operators are supported:
y = m * x + b
z = 5 % 4 # z is now 1
result = 14 / 8 # result is now 1 -- integer math only
Symbol |
Meaning |
---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulo |
The usual bitwise operators are supported:
a = 0xAAAA & 0x5555 # a is now 0
o = 0xAAAA | 0x5555 # o is now 0xFFFF
x = 0xAFAF ^ 0xFAFA # X is now 0x5555
n = ~ 0xAAAA # n is now 0x5555
l = 0x0A0A << 3 # l is now 0x5050
rp = 0x50A0 >> 3 # rp is now 0x0A14
rn = 0xA050 >> 3 # rn is now 0xF40A
Symbol |
Meaning |
---|---|
& |
Boolean And |
| |
Boolean Or |
^ |
Boolean Xor |
<< |
Shift Left |
>> |
Shift Right |
~ |
Not (Complement) |
Note
If the high bit is set when you shift right (indicating a negative number), then the high bit is imposed on
the number after each shift occurs. This results in at least the number of bits you’ve shifted all being set at the
high end. If you need to shift without preserving the high (negative) bit, you must clear the bit yourself (e.g.,
number &= 0x7FFF
) after the first bit shift.
The usual Boolean functions are supported:
Result = True and True # Result is True
Result = True and False # Result is False
Result = True and not False # Result is True
Result = True and not True # Result is False
Result = False and True # Result is False
Result = False and False # Result is False
Result = True or True # Result is True
Result = True or False # Result is True
Result = not True or not False # Result is True
Result = not (True or not False) # Result is False
Result = False or True # Result is True
Result = False or False # Result is False
Symbol |
Meaning |
---|---|
and |
Both must be True |
or |
Either can be True |
not |
Boolean inversion (not True == False) |
Ternary if
is supported:
x = "A is bigger" if a > b else "B is bigger"
Note
SNAPpy does not support the floor (//
) and power (**
) Python operators.
Slicing is supported for byte list, string and tuple data types. For example:
x = "ABCDE"
x[1:4] # returns "BCD"
Concatenation is supported for string data types. For example:
x = "Hello"
y = ", world"
x + y # "Hello, world"
String multiplication is supported:
3 * "Hello! " # "Hello! Hello! Hello! "
Subscripting is supported for byte list, string, and tuple data types. For example:
x = ('A', 'B', 'C')
x[1] # 'B'