Introduction to the usage of Python operation symbols: 1. Addition operator (), adds two numbers, such as: a b; 2. Subtraction operator (-), adds the second number from the first number Subtract, such as: a - b; 3. Multiplication operator (*), multiply two numbers, such as: a * b; 4. Division operator (/), divide the first number by the second Number, such as: a / b; 5. Integer division operator (//), returns the integer part of the first number divided by the second number, such as: a // b, etc.
The operating system for this tutorial: Windows 10 system, Python version 3.11.4, DELL G3 computer.
The usage of Python operators is slightly different depending on the specific operator type. Below I will explain their usage one by one according to the operator type.
1. Arithmetic operators:
Addition operator ( ): Add two numbers, such as: a b.
Subtraction operator (-): Subtract the second number from the first number, such as: a - b.
Multiplication operator (*): Multiply two numbers, such as: a * b.
Division operator (/): Divide the first number by the second number, such as: a / b.
Integer division operator (//): Returns the integer part of the first number divided by the second number, such as: a // b.
Remainder operator (%): Returns the remainder of the first number divided by the second number, such as: a % b.
Power operator (**): Returns the second power of the first number, such as: a ** b.
2. Comparison operator:
Equal operator (==): Check whether two numbers are equal , returns a Boolean value True or False, such as: a == b.
Inequality operator (!=): Checks whether two numbers are not equal and returns a Boolean value True or False, such as: a != b.
Greater than operator (>): Checks whether the first number is greater than the second number and returns a Boolean value True or False, such as: a > b.
Less than operator (<): Checks whether the first number is less than the second number and returns a Boolean value True or False, such as: a < b.
Greater than or equal to operator (>=): Checks whether the first number is greater than or equal to the second number, and returns a Boolean value True or False, such as: a >= b.
Less than or equal to operator (<=): Checks whether the first number is less than or equal to the second number, and returns a Boolean value True or False, such as: a <= b.
3. Logical operators:
And operator (and): Check whether multiple conditions are met at the same time , returns a Boolean value True or False, such as: condition1 and condition2.
Or operator (or): Checks whether at least one of multiple conditions is satisfied, returning a Boolean value True or False, such as: condition1 or condition2.
Non operator (not): reverses the result of the condition and returns a Boolean value True or False, such as: not condition.
4. Bitwise operators:
Bitwise AND operator (&): performs operations on two numbers Bitwise AND operation returns the result of bitwise AND, such as: a & b.
Bitwise OR operator (|): performs a bitwise OR operation on two numbers and returns a bitwise OR result, such as: a | b.
Bitwise XOR operator (^): performs bitwise XOR operation on two numbers and returns the result of bitwise XOR, such as: a ^ b.
Bitwise negation operator (~): performs a bitwise negation operation on a number and returns the bitwise negation result, such as: ~a.
Left shift operator (<<): Shifts the binary representation of a number to the left by the specified number of digits and returns the shifted result, such as: a << ;n.
Right shift operator (>>): Shifts the binary representation of a number to the right by the specified number of digits and returns the shifted result, such as: a >> ;n.
5. Assignment operator:
Simple assignment operator (=): Change the value of the right operand Assign to the left operand, such as: a = b.
Additional assignment operator (=): Adds the left operand to the right operand and assigns the result to the left operand, such as: a = b.
Subtraction assignment operator (-=): Subtract the right operand from the left operand and assign the result to the left operand, such as: a -= b.
Multiplication assignment operator (*=): Multiply the left operand by the right operand and assign the result to the left operand, such as: a *= b.
Division assignment operator (/=): Divide the left operand by the right operand and assign the result to the left operand, such as: a /= b.
Remainder assignment operator (%=): Take the remainder of the left operand and the right operand, and assign the result to the left operand, such as: a %= b.
Integer division assignment operator (//=): divides the left operand into the right operand and assigns the result to the left operand, such as: a //= b.
Power assignment operator (**=): Assign the power of the left operand to the left operand, such as: a ** = b.
Bitwise AND assignment operator (&=): Perform a bitwise AND operation on the left operand and the right operand, and assign the result to the left operand, such as: a & = b.
Bitwise OR assignment operator (|=): performs a bitwise OR operation on the left operand and the right operand, and assigns the result to the left operand, such as: a |= b.
Bitwise XOR assignment operator (^=): Perform a bitwise XOR operation on the left operand and the right operand, and assign the result to the left operand, such as: a^=b.
Left shift assignment operator (<<=): Shifts the binary representation of the left operand to the left by the specified number of digits, and assigns the result to the left operand, such as :a <<= n.
Right shift assignment operator (>>=): Shifts the binary representation of the left operand to the right by the specified number of digits and assigns the result to the left operand, such as :a>>= n.
These are the common operators in Python and their usage. You can choose the appropriate operator to perform the corresponding operation according to your specific needs.
The above is the detailed content of How to use Python arithmetic symbols. For more information, please follow other related articles on the PHP Chinese website!