SoFunction
Updated on 2024-11-15

Basic arithmetic operators for python tutorials

I. Arithmetic operators

operator (computing)
+
-
*
/
%
** (power)square a number
// (divide, round down) e.g. 9//2 = 4

II. Comparison operators

operator (computing)
==
!=
<> (not equal, similar! =)
<
>
>=
<=
#Examples:
x = 10
y = 20

print(x > y) # False
print(x < y) # True
print(x >= y) # False
print(x <= y) # True

print(x == y) # False
print(x != y) # True

III. Assignment operators

operator (computing)
=
+=
-=
*=
/=
%=
**=
//=
x = 10 # (unary assignment operator)
y = 20

# x = x+y # x += y
x += y # (binary assignment operator)
x += 10
print(x)

x -= y
print(x)
x *= y
print(x)
x /= y
print(x)
x **= y # x = x**y
print(x)
y //= x # x = x//y
print(y)
x %= y # x = x%y
print(x)

IV. Logical operators

Mainly used for logical judgment, bool, loop, etc., return True or False (binary operator)

operator (computing)
and
or
not

and(and), true if both conditions are true, otherwise both are False.

print(1>1 and 2>3) # False
print(10>1 and False) # False

or, as long as one of them is true, it is true, otherwise it is False.

age = 18
inp_age= input('age:')
print(age==inp_age or True) # True
print(True or False) # True
print(False or False) # False

not

print(not True) # False
print(not False) # True

V. Identity operators

Identity operator is used to compare the storage units of two elemental objects, by comparing the two id returns a boolean value

Note that.ids with the same value are not necessarily the same, ids with the same value are necessarily the same

operator (computing) descriptive an actual example
is is is to determine whether the two identifiers refer to a single object, similar to ==. x is y (returns a bool value)
is not is not is to determine whether the two identifiers refer to different objects, similar to! = x is not y (returns a bool value)
x=1000
y=1000

print(id(x))
print(id(y))

print(x is y) # False
print(x is not y) # True
print(not x is y) #False

VI. Bitwise operators

The per-position operator that treats numbers as binary to perform calculations is the following per-position algorithm in python:

Here is the variable a is 60 and b is 13.The binary format is as follows:

a = 0011 1100
b = 0000 1101

a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011

operator (computing) descriptive an actual example
& Bitwise and operator: two values are involved in the operation, if both corresponding bits are 1, the result of the bit is 1, otherwise it is 0. (a&b) The output result is 12, which is interpreted in binary as: 0000 1100|
| Bitwise or operator: Whenever one of the corresponding binary bits is 1, the result bit is 1. (a|b) Output result 61, binary interpretation: 0011 1101
^ Bitwise different or operator: when the two corresponding binary bits are different, the result is 1 (The output of (a^b) is 49, which is interpreted in binary as: 0011 0001
~ Bitwise inversion operator: inverts each binary bit of data, i.e., turns a 0 into a 1 and a 1 into a 0.~x is analogous to -x-1. (~a) Output result bit -61, binary interpretation: 1100 0011, in a signed binary complement form.
<< Left Shift Operator: Each binary bit of the operator is shifted to the left by a number of bits, the number of bits shifted is specified by the number to the right of << the higher bit is discarded and the lower bit is filled with zeros. a<<2, the output result is 240, the binary interpretation is: 1111 0000
>> Right-shift operator: shifts each binary digit of the operator to the left of >> all to the right by a number of bits; the number to the right of >> specifies the number of bits shifted. a>>2 The output result is 15, which is interpreted in binary as: 0000 1111

VII. Membership operators

In addition to some of the above operators, python supports membership operators, and the test examples contain a range of members, including strings, lists, or tuples.

operator (computing) descriptive an actual example
in True if the value meal is found in the specified sequence, otherwise returns False. x is in the sequence of y, x in y returns True
not in Returns True if no value is found in the specified sequence, False otherwise. x is not in the sequence of y, x not iny Returns True

VIII. Prioritization of python operators

operator (computing) descriptive
** Index (highest priority)
~ +- Flip by bit, one-dollar plus and minus signs (the methods for the last two are named +@ and -@)
*/%// Multiplying, dividing, modulo (remainder), taking integer divisions
+- addition and subtraction
>> << Right shift and left shift operators
& Bit 'AND'
^| bitwise operator (computing)
<=,<,>,>= comparison operator
<>,==,!= equality operator
=,%=,/=,//=,**=,*=,+=,-= equality operator
is,is not identity operator
in,not in membership operator
not ,and,or logical operator

to this article on python tutorials on basic arithmetic operators to this article, more related to basic arithmetic operators python content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!