0

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 ||

black_bird
  • 105
  • 1
  • 3
  • 12
  • I don't think `@` does matrix multiplication... http://stackoverflow.com/questions/6392739/what-does-the-symbol-do-in-python Unless I am mistaken, and I have been in the past. – Dair Jun 18 '14 at 02:44
  • @user667648 I was referencing the second link: "Python Operators 2" where they say it is equivalent to matmul() – black_bird Jun 18 '14 at 03:46
  • Ooh, yeah, I don't believe 3.5 is considered a "stable release" yet, although that is cool!! – Dair Jun 18 '14 at 03:53
  • @user667648 my apologies! I noted the future aspect above! – black_bird Jun 20 '14 at 05:36
  • No need to be sorry! I should be thanking you! It gives me the heads up for a really cool feature that is about to be released! – Dair Jun 20 '14 at 05:41

1 Answers1

2

% 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.

James
  • 2,155
  • 5
  • 19
  • 29
  • Note that `[]` and `()` are also used to create `list` and `tuple` literals. (although, as far as tuples are concerned, the `()` are more or less customary and only required when not having them would introduce ambiguity in the parser. – mgilson Jun 18 '14 at 03:08
  • @James did the use change? I guess in 3.50a0 they say it is the same as `matmul()` aka matrix multiplication. [Python Operators 2](https://docs.python.org/3.5/library/operator.html) – black_bird Jun 18 '14 at 03:49
  • 1
    `@` is not yet the matrix multiplication operator, but will be added in Python 3.5. See [PEP 465](http://legacy.python.org/dev/peps/pep-0465/). – BrenBarn Jun 18 '14 at 03:59
  • @BrenBarn -- I'm not sure that I like the choice of `@`, but I do like the idea of having builtin `__matmul__` ... – mgilson Jun 18 '14 at 06:51