Home  >  Article  >  Backend Development  >  what are operators in python

what are operators in python

coldplay.xixi
coldplay.xixiOriginal
2020-08-27 13:36:448466browse

Python operators are: 1. Arithmetic operations, used for mathematical operations such as addition, subtraction, multiplication and division; 2. Assignment operations, used to receive results returned by operators or method calls; 3. Comparison operations, used to do Large or equal comparison operations; 4. Logical operations, used for AND, OR, and NOT operations; 5. Bit operations, used for binary operations.

what are operators in python

Related learning recommendations: python tutorial

Python operators are:

After talking about commonly used data types, let’s talk about operators. Operators are used to perform operations on various types of data to make static data run.

Operations in programming languages ​​are roughly divided into the following categories:

  • Arithmetic operations, used for mathematical operations such as addition, subtraction, multiplication and division

  • Assignment operations, used to receive results returned by operators or method calls

  • Comparison operations, used to perform size or equality comparison operations

  • Logical operations, used for AND, OR, NOT operations

  • Bit operations, used for binary operations

Each type The symbols included in the operation are called corresponding operators, such as arithmetic operators, comparison operators, etc.

1. Arithmetic operations

what are operators in python

2. Assignment operations

what are operators in python

3. Comparison operations

Python has 8 comparison operations, and they have the same priority. Comparison operations can be chained arbitrarily, for example x

what are operators in python

Explanation:

a) Objects of different types are compared and never equal (except for different numeric types);

b) When using the and >= operators, a TypeError exception will be thrown in the following situations: (1) When used to compare complex numbers with other built-in numeric types; (2) When the compared objects are of different types and cannot be compared; (3) In other undefined cases;

c) Different instances of a class are usually not equal unless the class defines __eq__() Method;

d) Instances of a class cannot be sorted relative to other instances of the same class or other classes, unless the class defines sufficient methods __lt__(), __le__(), __gt__(), __ge__(). If you want the conventional meaning of the comparison operators, __lt__() and __eq__() are sufficient;

e) The behavior of the is and is not operators cannot be customized; otherwise, they can be applied If two objects of different types are detected, no exception will be thrown.

f) Two other operations with the same syntactic precedence are in and not in, which support objects of sequence, set, and map types.

g) The result of the comparison operation is a Boolean value: True or False

4. Logical operation

Truth Value Testing )

Before explaining "Boolean operations", let's first talk about a special operation in Python-"true" value testing.

Any object in Python can be tested for a "true" value. The "true" value test mentioned here can be understood like this: any object in Python can be converted into a Boolean value, and this "true" value test is the process of obtaining the Boolean value corresponding to an object.

In Python, only the following values ​​correspond to Boolean values ​​that are False:

NoneFalse 0 in the numeric type, such as: 0, 0.0, 0j Any empty sequence, such as: '', () , [] Any empty mapping, such as: {} An instance of a user-defined class - a __bool__() or __len__() method is defined in the user-defined class, and the instance returns the integer 0 when calling the method. Or the Boolean value False

In addition, all other values ​​corresponding to the Boolean value are True, so many types of objects are always True.

"True" value testing can be used in if or while conditions, or as the operand of a Boolean operation.

Boolean Operations

The logical operations in Python are called "Boolean Operations". The operators include: and (and), or (or), not ( No).

The following are explained in ascending order of their priority:

what are operators in python

Explanation:

a) or is a short-circuit operator, also That is to say, the second parameter will be evaluated only when the evaluation result of the first parameter is False;

b) and is also a short-circuit operator, that is, only when the evaluation result of the first parameter is True, the second parameter will be evaluated;

c) The not operator is better than the non Boolean operators have low precedence, so not a == b is interpreted as not (a == b); if written as a == not b, a syntax error will occur.

5. Bitwise operations

Bitwise operations refer to converting numbers into binary for calculation. Bitwise operators include the following:

Assume:

a = 60, the corresponding binary format is 0011 1100

b = 13, the corresponding binary format is 0000 1101

what are operators in python

## If you want to know more about related learning, please pay attention to the

php training column!

The above is the detailed content of what are operators in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn