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:
Operator Description Example = Assigns values from right side operands to left side operand c = a + b assigns value of a + b into c += Add AND It adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a -= Subtract AND It subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c - a *= Multiply AND It multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a /= Divide AND It divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / a %= Modulus AND It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a **= Exponent AND Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a //= Floor Division It performs floor division on operators and assign value to the left operand c //= 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 HintsHow 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 isTrue
, the other isFalse
).To demonstrate:
>>> 0^0
0
>>> 1^1
0
>>> 1^0
1
>>> 0^1
1To explain one of your own examples:
>>> 8^3
11Think about it this way:
1000 # 8 (binary)
0011 # 3 (binary)
---- # APPLY XOR ('vertically')
1011 # result = 11 (binary)
Related Topics
Break // in X Axis of Matplotlib
How to Count the Occurrence of a Certain Item in an Ndarray
Windows Cmd Encoding Change Causes Python Crash
Differencebetween 'Same' and 'Valid' Padding in Tf.Nn.Max_Pool of Tensorflow
How to Download a File on a Click Event Using Selenium
How to Make Program Go Back to the Top of the Code Instead of Closing
Pandas: Setting No. of Max Rows
How to Solve a Pair of Nonlinear Equations Using Python
How to Get the Discord.Py Intents to Work
Windows Is Not Passing Command Line Arguments to Python Programs Executed from the Shell
Decode Escaped Characters in Url
How to Find the Location of Python Module Sources
Automatically Import Modules When Entering the Python or Ipython Interpreter
Elegant Python Function to Convert Camelcase to Snake_Case
How to Get First Element in a List of Tuples
Python Split() Without Removing the Delimiter
Python Multiprocessing Safely Writing to a File
Typeerror: a Bytes-Like Object Is Required, Not 'Str' in Python and CSV