home PYTHONJAVA
 

Python3 Operators


What is an operator?

This section mainly describes the Python operators. For a simple example 4 +5 = 9 . In the example, 4 and 5 are called operands, and "+" are called operators.

The Python language supports the following types of operators:

Next let's learn Python operators one by one.


Python Arithmetic Operator

The following hypothesis variable a is 10 and variable b is 21:

OperatorDescriptionInstance
+Plus - Add two objects a + b Output 31
-minus - get a negative number or a number minus another number a - b output result -11
*multiply - multiply two numbers or return a string that is repeated several times a * b output result 210
/Division - x divided by y b / a Output 2.1s
% modulo - returns the remainder of the division b % a Output 1
**power - returns the power of y of x a**b is the 21st power of 10
//Division and division - Take an integer close to the divisor
>>> 9//2
4
>>> -9//2
-5

The following example demonstrates the operation of all Python arithmetic operators.:

Instance(Python 3.0+)

#!/usr/bin/python3 a = 21 b = 10 c = 0 c = a + b print ("1 - c Value:", c) c = a - b print ("2 - c Value:", c) c = a * b print ("3 - c Value:", c) c = a / b print ("4 - c Value:", c) c = a % b print ("5 - c Value:", c) # Modify variable a 、b 、c a = 2 b = 3 c = a**b print ("6 - c Value:", c) a = 10 b = 5 c = a//b print ("7 - c Value:", c)

The above example output:

1 - c Value: 31
2 - c Value: 11
3 - c Value: 210
4 - c Value: 2.1
5 - c Value: 1
6 - c Value: 8
7 - c Value: 2

Python comparison operator

The following hypothesis variable a is 10 and variable b is 20:

OperatorDescriptionInstance
== is equal to - compare objects are equal (a == b) returns False.
!= Not equal to - Compare two objects are not equal (a != b) Returns True.
> Greater than - Returns whether x is greater than y (a > b) Returns False.
< Less than - Returns whether x is less than y. All comparison operators return 1 for true and 0 for false. This is equivalent to the special variables True and False, respectively. Note that the capital names of these variables are capitalized. (a < b) returns True.
>= Greater than or equal to - Returns whether x is greater than or equal to y. (a >= b) returns False.
<= Less than or equal to - Returns whether x is less than or equal to y. (a <= b) returns True.

The following example demonstrates the operation of all Python comparison operators:

Instance(Python 3.0+)

#!/usr/bin/python3 a = 21 b = 10 c = 0 if ( a == b ): print ("1 - a equal b") else: print ("1 - a not equal to b") if ( a != b ): print ("2 - a not equal to b") else: print ("2 - a equal b") if ( a < b ): print ("3 - a Less than b") else: print ("3 - a greater or equal to b") if ( a > b ): print ("4 - a more than the b") else: print ("4 - a Less than or equal to b") # Modify the values of variables a and b a = 5; b = 20; if ( a <= b ): print ("5 - a Less than or equal to b") else: print ("5 - a more than the b") if ( b >= a ): print ("6 - b greater or equal to a") else: print ("6 - b Less than a")

The output of the above example:

1 - a not equal to b
2 - a not equal to b
3 - a greater or equal to b
4 - a more than the b
5 - a Less than or equal to b
6 - b greater or equal to a
a = 21 b = 10 c = 0

Python Assignment Operator

The following hypothesis variable a is 10 and variable b is 20:

OperatorDescriptionInstance
=Simple assignment operator c = a + b Assign the result of a + b to c
+=Additional Assignment Operator c += a is equivalent to c = c + a
-=Subtraction assignment operator c -= a is equivalent to c = c - a
*=Multiplication assignment operator c *= a is equivalent to c = c * a
/=Division assignment operator c /= a is equivalent to c = c / a
%=Modification Assignment Operator c %= a Equivalent to c = c % a
**=Power assignment operator c **= a is equivalent to c = c ** a
//= Rounding the assignment operator c //= a is equivalent to c = c // a

The following example demonstrates the operation of all Python assignment operators:

Instance(Python 3.0+)

#!/usr/bin/python3 c = a + b print ("1 - c Value:", c) c += a print ("2 - c Value:", c) c *= a print ("3 - c Value:", c) c /= a print ("4 - c Value:", c) c = 2 c %= a print ("5 - c Value:", c) c **= a print ("6 - c Value:", c) c //= a print ("7 - c Value:", c)

The above example output:

1 - c Value: 31
2 - c Value: 52
3 - c Value: 1092
4 - c Value: 52.0
5 - c Value: 2
6 - c Value: 2097152
7 - c Value: 99864

Python bit operator

The bitwise operator is calculated by treating the number as a binary. The bitwise algorithm in Python is as follows:

The variable a in the table below is 60, and b is 13 in binary format as follows:

a = 0011 1100
    
    
b = 0000 1101

-----------------

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011
< Td> (a & b) output 12, binary interpretation: 0000 1100
OperatorDescriptionInstance
&Bitwise AND Operator: Two values participating in the operation. If both corresponding bits are 1, the result of this bit is 1, otherwise 0
| Bitwise OR Operator: As long as one of the corresponding two binary digits is 1, the result bit is 1. (a | b) Output 61, binary interpretation: 0011 1101
^bitwise XOR operator: When the two corresponding binary digits are different, the result is 1 (a ^ b) Output 49, binary interpretation: 0011 0001
~ Bitwise negation operator: Inverts each binary bit of data, that is, changes 1 to 0 and 0 to 1. ~x is similar to -x-1 (~a ) output -61 , binary interpretation : 1100 0011, in the complement form of a signed binary number.
<<Left movement operator: Each binary of the operand is shifted left by several digits, and the number of digits moved by the number on the right side of "<<" is discarded. The low position is 0. a << 2 output 240, binary interpretation: 1111 0000
>>Right movement operator: shift all the binary digits of the operand to the left of ">>" to the right by several digits, and the number on the right side of ">>" Number of bits moved a >> 2 Output result 15, binary interpretation: 0000 1111

Python Logic Operators

The Python language supports logical operators. The following assumes that the variable a is 10 and b is 20:

OperatorLogical ExpressionDescriptionInstance
andx and y Boolean "and" - If x is False, x and y return False, otherwise it returns the calculated value of y. (a and b) returns 20.
orx or yBoolean "or" - If x is True, it returns the value of x, otherwise it returns the computed value of y. (a or b) returns 10.
notnot xBoolean "Non" - Returns 0 if x is True. If x is False, it returns True. not(a and b) returns False

The output of the above example:

Instance (Python 3.0+)

#!/usr/bin/python3 a = 10 b = 20 if ( a and b ): print ("1 - The variables a and b are both true") else: print ("1 - Variables a and b have one not true") if ( a or b ): print ("2 -The variables a and b are both true, or one of the variables is true") else: The variables a and b are both true, or print ("2 - variables a and b are not true") One variable is # Modify the value of the variable a a = 0 if ( a and b ): print ("3 - The variables a and b are both true") else: print ("3 - Variables a and b have one not true") if ( a or b ): print ("4 - The variables a and b are both true, or one of the variables is true") else: print ("4 - The variables a and b are not true") if not( a and b ): print ("5 - The variables a and b are both false,Or one of the variables is false") else: print ("5 - The variables a and b are both true")

The output of the above example:

1 - Variables a and b are both true
2 - Variables a and b are both true, or one of the variables is true
3 - Variables a and b have one not true
4 - Variables a and b are both true, or one of the variables is true
5 - Variables a and b are both false, or one of the variables is false

Python member operator

In addition to some of the above operators, Python also supports member operators, which contain a series of members, including strings, lists, or tuples.

OperatorDescriptionInstance
in Returns True if the value is found in the specified sequence, otherwise returns False. x In the y sequence, if x returns True in the y sequence.
not in Returns True if no value is found in the specified sequence, otherwise returns False. x is not in the y sequence, if x does not return True in the y sequence.

The following example demonstrates the operation of all Python member operators:

Instance (Python 3.0+)

#!/usr/bin/python3 a = 10 b = 20 list = [1, 2, 3, 4, 5 ]; if ( a in list ): print ("1 - Variable a in the given list list in") else: print ("1 - The variable a is not in the list in the given list") if ( b not in list ): print ("2 - The variable b is not in the list in the given list") else: print ("2 - The variable b is in the list in the given list") # Modify the value of the variable a a = 2 if ( a in list ): print ("3 - The variable a is in the list in the given list") else: print ("3 - The variable a is not in the list in the given list")

The output of the above example:

1 - variable a is not in the list in the given list
2 - variable b is not in the list in the given list
3 - variable a in the list in the given list

Python Identity Operator

The identity operator is used to compare the storage units of two objects

OperatorDescriptionInstance
is Is is to determine whether the two identifiers are referenced from an object x is y, similar to id(x) == id(y) , Returns True if the same object is referenced, otherwise returns False
is notis not is to determine if two identifiers are referenced from different objects x is not y , similar to id (a) != id(b). Returns True if the reference is not the same object, otherwise returns False.

The following example demonstrates the operation of all Python identity operators:

Instance (Python 3.0+)

#!/usr/bin/python3 a = 20 b = 20 if ( a is b ): print ("1 - a Same as b") else: print ("1 - a And b do not have the same identifier") if ( id(a) == id(b) ): print ("2 - a Same as b") else: print ("2 - a And b do not have the same identifier") # Modify the value of the variable b b = 30 if ( a is b ): print ("3 - a Same as b") else: print ("3 - a And b do not have the same identifier") if ( a is not b ): print ("4 - a And b do not have the same identifier") else: print ("4 - a Same as b")

The output of the above example:

1 - a and b have the same logo
2 - a and b have the same logo
3 - a and b do not have the same logo
4 - a and b do not have the same logo

is and == difference:

is is used to determine whether two variable reference objects are the same, and == is used to determine whether the values of the reference variables are equal.

>>>a = [1, 2, 3] >>> b = a >>> b is a True >>> b == a True >>> b = a[:] >>> b is a False >>> b == a True

Python Operator Priority

The following table lists all operators from highest to lowest priority:

OperatorDescription
** index (highest priority)
~ + - Flip by bit, unary plus and minus (the last two methods are named +@ and -@)
* / % // multiply, divide, modulo and divide by
+ - addition subtraction
>> << Shift right, left shift operator
& bit 'AND'
^ | bit operator
<= < > >= Compare operator
<> == != is equal to operator
= %= /= //= -= += *= **= Assignment Operator
is is not identity operator
in not in member operator
and or not logical operator

The following example demonstrates the operation of all Python operator precedence:

Instance(Python 3.0+)

#!/usr/bin/python3 a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c / d #( 30 * 15 ) / 5 print ("(a + b) * c / d The result of the operation is:", e) e = ((a + b) * c) / d # (30 * 15 ) / 5 print ("((a + b) * c) / d The result of the operation is:", e) e = (a + b) * (c / d); # (30) * (15/5) print ("(a + b) * (c / d) The result of the operation is:", e) e = a + (b * c) / d; # 20 + (150/5) print ("a + (b * c) / d The result of the operation is:", e)

The output of the above example:

(a + b) * c / d The result of the operation is: 90.0
((a + b) * c) / d The result of the operation is: 90.0
(a + b) * (c / d) The result of the operation is: 90.0
The result of a + (b * c) / d is: 50.0






javacodegeeks is optimized for learning.© javacodegeeks. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.