What Is the '@=' Symbol for in Python

What does the at (@) symbol do in Python?

An @ symbol at the beginning of a line is used for class and function decorators:

  • PEP 318: Decorators
  • Python Decorators

The most common Python decorators are:

  • @property
  • @classmethod
  • @staticmethod

An @ in the middle of a line is probably matrix multiplication:

  • @ as a binary operator.

What is the '@=' symbol for in Python?

From the documentation:

The @ (at) operator is intended to be used for matrix multiplication. No builtin Python types implement this operator.

The @ operator was introduced in Python 3.5. @= is matrix multiplication followed by assignment, as you would expect. They map to __matmul__, __rmatmul__ or __imatmul__ similar to how + and += map to __add__, __radd__ or __iadd__.

The operator and the rationale behind it are discussed in detail in PEP 465.

^=, -= and += symbols in Python

As almost any modern language, Python has assignment operators
so they can use them every time you want to assign a value to a variable after doing some arithmetic or logical operation, both (assignment and operation) are expressed in a compact way in one statement...

Table from Tutorials Point:




















































OperatorDescriptionExample
=Assigns values from right side operands to left side operandc = a + b assigns value of a + b into c
+= Add ANDIt adds right operand to the left operand and assign the result to left operandc += a is equivalent to c = c + a
-= Subtract ANDIt subtracts right operand from the left operand and assign the result to left operandc -= a is equivalent to c = c - a
*= Multiply ANDIt multiplies right operand with the left operand and assign the result to left operandc *= a is equivalent to c = c * a
/= Divide ANDIt divides left operand with the right operand and assign the result to left operandc /= a is equivalent to c = c / a
%= Modulus ANDIt takes modulus using two operands and assign the result to left operandc %= a is equivalent to c = c % a
**= Exponent ANDPerforms exponential (power) calculation on operators and assign value to the left operandc **= a is equivalent to c = c ** a
//= Floor DivisionIt performs floor division on operators and assign value to the left operandc //= a is equivalent to c = c // a

What does the - (dash-greater-than arrow symbol) mean in a Python method signature?

This is function annotations. It can be use to attach additional information to the arguments or a return values of functions. It is a useful way to say how a function must be used.
Functions annotations are stored in a function's __annotations__ attribute.

Use Cases (From documentation)

  • Providing typing information

    • Type checking
    • Let IDEs show what types a function expects and returns
    • Function overloading / generic functions
    • Foreign-language bridges
    • Adaptation
    • Predicate logic functions
    • Database query mapping
    • RPC parameter marshaling
  • Other information

    • Documentation for parameters and return values

From python-3.5 it can be used for Type Hints

How to print symbols vertically in python?

def draw_symbol():
for num in range(8):
print("*")
return
draw_symbol()

would result in:

*
*
*
*
*
*
*
*

What does the caret (^) operator do?

It's a bitwise XOR (exclusive OR).

It evaluates to True if and only if its arguments differ (one is True, the other is False).

To demonstrate:

>>> 0^0
0
>>> 1^1
0
>>> 1^0
1
>>> 0^1
1

To explain one of your own examples:

>>> 8^3
11

Think about it this way:


1000 # 8 (binary)
0011 # 3 (binary)
---- # APPLY XOR ('vertically')
1011 # result = 11 (binary)


Related Topics



Leave a reply



Submit