| This operator switches true
(anything except 0) to false (0) and vice versa. You can use '!' or 'not', whichever you prefer.
a = 3
b = 0
Print (!a)
0
Print (not b)
1
Here are some examples for the six operators with the second highest priority. Note that the modulo operator
'%' requires an integer on the left side. The '//' operator is mainly known to Python programmers,
it is a division with truncation of the result's fractional part. The normal division operator
'/' rounds the result to the closest integer if the requested result data type is an integer.
a = 2
b = 3.
Print (a*b)
6
Print (b*a)
6.0000000000e0
Print (a/b)
1
Print (a//b)
0
Print (b/a)
1.5000000000e0
Print (a%b)
2
Print (b%a)
Error - unsupported operator
The '//' operator is additionally helpful to truncate floating point numbers without rounding:
The shift operators '<<' and '>>' do bitwise binary shifts to the left or right,
they require an integer on the left side. A left shift corresponds to a multiplication with
2, a right shift to a division by 2. That's why they have the same priority as '*' and
'/'.
a = 2
b = 3.000
Print (a<<1)
4
Print (a<<b)
16
Print (a>>1)
1
Print (b>>a)
Error - unsupported operator
a = 2
b = 3.000
Print (a+b)
5
Print (b+a)
5.000
These operators do a bitwise 'and', 'or' and 'xor', there must be an integer on the left side. They are rarely if ever needed in Yanaconda. Unlike in the C programming language,
their priority is higher than the comparisons, just like in Python. All comparison operators have the third lowest priority. They return
1 if the comparison is true, 0 otherwise. Note that '!=' stands for 'is not equal to'.
a = 2
b = 3.000
Print (a>b)
0
Print (a<b)
1
Print (a==b)
0
Print (a!=b)
1
The 'in' operator determines if a variable is in a list:
anglelist='Alpha','Beta','Gamma'
angle='Beta'
if angle in anglelist
Print 'Yes'
Yes
Logical operators are used most of the time in if/else statements to combine the results of comparisons.
a = 2
b = 3
if a==1 or b==3
Print "Hello World!"
Hello World!
|