Difference between revisions of "Python:Operators"

From wiki
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[Category:Python]]
 
[[Category:Python]]
Most operators work as usual.
+
Most operators work as you would expect.
  
==Assignment==
+
==Less obvious assignment==
 +
 
 +
;<int> * <string>
 +
:Repeat <string> <int> times. Negative <int>s return an empty string.
  
 
;a / b
 
;a / b
:Divide to floating point (in python2 the result datatype is determined by the arguments datatype i.e. both arguments int => result is int)
+
:Divide to floating point. In python2 the result datatype is determined by the arguments datatype i.e. both arguments int => result is int.
  
 
;a // b
 
;a // b
Line 17: Line 20:
  
 
;a << b
 
;a << b
;bit-shift left
+
:bit-shift left
  
 
;a >> b
 
;a >> b
;bit-shift right
+
:bit-shift right
  
;a & b
+
;undef(a)
:And
+
:Remove variable a (Note: variables are pointers to objects, the object may not be removed)
  
;a | b
+
==Self assign==
:Or
+
;a += b
 +
:Add b to a, works with other operators and on strings too.
  
;a ^ b
+
;a -= b
:Xor
+
:Substract b from a, works on sets too.
  
 
==Compare==
 
==Compare==
Line 35: Line 39:
 
:Just as you would expect
 
:Just as you would expect
  
;and or &
+
==Boolean==
 +
;and, &
 
:Logical and
 
:Logical and
;or or |
+
;or, |
 
:Logical or
 
:Logical or
 
+
; ^
 +
:Logical Xor (exclusive or)
 
;not
 
;not
 
:Negate the result of the next expression
 
:Negate the result of the next expression

Latest revision as of 08:35, 1 February 2024

Most operators work as you would expect.

Less obvious assignment

<int> * <string>
Repeat <string> <int> times. Negative <int>s return an empty string.
a / b
Divide to floating point. In python2 the result datatype is determined by the arguments datatype i.e. both arguments int => result is int.
a // b
Divide to integer (truncated, not rounded)
a % b
Modulo
a ** b
Power
a << b
bit-shift left
a >> b
bit-shift right
undef(a)
Remove variable a (Note: variables are pointers to objects, the object may not be removed)

Self assign

a += b
Add b to a, works with other operators and on strings too.
a -= b
Substract b from a, works on sets too.

Compare

==, >, <, <=, >=, !=
Just as you would expect

Boolean

and, &
Logical and
or, |
Logical or
^
Logical Xor (exclusive or)
not
Negate the result of the next expression