Reputation: 115
In Learn Python the Hard Way by Zed Shaw There is a list of operators that weren't defined given as an exercise, and I've had trouble trying to find their definition / purpose around the web.
Here's what I have so far:
+ : addition/concatenation
- : subtraction
* : multiplication
** : exponenttiation
/ : division
// : floor division :: digits after the decimal place are removed
% : string type flag?
< : less than
> : greater than
<= : less than / equal
>= : greater than / equal
== : absolute equality
!= : not equal
<> : old not equal -- apparently phased out, use above != now
() : ________________________
[] : ________________________
{} : ________________________
@ : decorators, maybe matrix multiplication in 3.5 future release
' : ________________________
: : ________________________
. : this is generally used as a modifying/linking element to property/subproperty
= : equality
; : ________________________
+= : duality in operation, successively :: x = x + 1 >> x += 1
-= : " " :: x = x - 1 >> x -= 1
*= : " " :: x = x * 1 >> x *= 1
/= : " " :: x = x / 1 >> x /= 1
//= : Floor division and assigns a value, performs floor division on operators and assign value to the left operand
%= : Modulus AND assignment operator, it takes modulus using two operands and assign the result to the left operand : c%=a == c = c % a
**= : Exponent AND assignment operator: Performs exponential (power) calculation on operators and assigns value to the left operand : c **= a == c ** a , so c to the exponent of a
I'm sure these definitions aren't complete and they're probably poorly worded, so if you want to make corrections to the previous definitions, by all means, however I'm mainly trying to figure out the ones that I have yet to complete--There are blank lines above
Links I've attempted with: Python Operators 1 || Python Operators 2 || Python 3.0 Operators || 2.7.7 Operator Precedence ||
Upvotes: 0
Views: 404
Reputation: 2795
%
is modulus. 3 % 2 == 1
. You can also use it when formatting strings though, but as an operator, it is modulus. For formatting strings, myString.format() is preferred.
()
is either a function call, or order of precedence in general expressions. f(x)
, for example, or (3 * (1 + 2))
. It calls the __call__
method of an object. It also creates a tuple literal, but only if there is at least one comma in the tuple. (4,)
for example.
[]
is indexing - it allows you to select a container by index via the __getitem__
and __setitem__
methods. [1,2,3,4,5,6][2] == 3
. It also creates a list.
{}
constructs either a set or a dictionary, depending on the context.
@
is used for decorators, not for matrix multiplication.
'
is a quote and equivalent to "
:
is used for list slicing, and to denote a code block. (Such as in a function, an error handling context or loop)
;
is not used unless you want to put multiple statements on one line, but that isn't encouraged for readability purposes.
Other than that, I think you have your definitions more-or-less correct.
Upvotes: 2