Summary of Operators in Python

HanHan
3 min read

Types of Arithmetic Operators in Python

SymbolDescriptionReturn Value
+AdditionVaries depending on the data types
-SubtractionVaries depending on the data types
*MultiplicationVaries depending on the data types
/DivisionReturns a floating-point value
//Floor DivisionReturns an integer value
%ModulusReturns a floating-point value
**ExponentiationVaries depending on the data types
andLogical ANDReturns True or False
orLogical ORReturns True or False
<Less thanReturns True or False
\>Greater thanReturns True or False
<=Less than or equal toReturns True or False
\>=Greater than or equal toReturns True or False
\==Equal toReturns True or False

+, -, , /, //, %, *, <, >, <=, >= Table

ABResult
intintint A + B result
intfloatfloat A + B result
intbool-Trueint A + 1 result
intbool-Falseint A + 0 result
intNoneTypeError
intstringTypeError
floatbool-Truefloat A + 1 result
floatbool-Falsefloat A + 0 result
floatNoneTypeError
floatstringTypeError
boolNoneTypeError
boolstringTypeError
NonestringTypeError

In the early days of computer generation and the inception of programming languages, 1 represented True, and 0 represented False.

\== Table

ABResult
intintBoolean result
intfloatBoolean result
intboolBoolean result
intNoneBoolean result
intstringBoolean result
floatboolBoolean result
floatNoneBoolean result
floatstringBoolean result
boolNoneBoolean result
boolstringBoolean result
NonestringBoolean result

Unlike Java, this doesn't compare classes. It compares values. Since all are objects, these comparisons are possible.

and, or Truth Tables

ABandor
FalseFalseFalseFalse
FalseTrueFalseTrue
TrueFalseFalseTrue
TrueTrueTrueTrue

As shown in the table above, "and" and "or" are operations on True and False.

When different data types are used, the "or" operation returns the value of B, while the "and" operation returns the value of A.

0
Subscribe to my newsletter

Read articles from Han directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Han
Han