Standard type operators in python

4.5. Standard Type Operators

4.5.1. Value Comparison

Comparison operators are used to determine equality of two data values between members of the same type. These comparison operators are supported for all built-in types. Comparisons yield true or false values, based on the validity of the comparison expression. Python chooses to interpret these values as the plain integers 0 and 1 for false and true, respectively, meaning that each comparison will result in one of those two possible values. A list of Python's value comparison operators is given in Table 4.1.

Table 4.1. Standard Type Value Comparison Operatorsoperatorfunction
expr1 < expr2 expr1 is less than expr2
expr1 > expr2 expr1 is greater than expr2
expr1 >> a = [ 5, 'hat', -9.3] >>> b = a >>> a is b True >>> a is not b False >>> >>> b = 2.5e-5 >>> b 2.5e-005 >>> a [5, 'hat', -9.3] >>> a is b False >>> a is not b True

Both the is and not identifiers are Python keywords.

Core Note: Interning

In the above examples with the foo1 and foo2 objects, you will notice that we use floating point values rather than integers. The reason for this is although integers and strings are immutable objects, Python sometimes caches them to be more efficient. This would have caused the examples to appear that Python is not creating a new object when it should have. For example:

>>> a = 1 >>> id[a] 8402824 >>> b = 1 >>> id[b] 8402824 >>> >>> c = 1.0 >>> id[c] 8651220 >>> d = 1.0 >>> id[d] 8651204

In the above example, a and b reference the same integer object, but c and d do not reference the same float object. If we were purists, we would want a and b to work just like c and d because we really did ask to create a new integer object rather than an alias, as in b = a.

Python caches or interns only simple integers that it believes will be used frequently in any Python application. At the time of this writing, Python interns integers in the range[-1, 100] but this is subject to change, so do not code your application to expect this.

In Python 2.3, the decision was made to no longer intern strings that do not have at least one reference outside of the "interned strings table." This means that without that reference, interned strings are no longer immortal and subject to garbage collection like everything else. A BIF introduced in 1.5 to request interning of strings, intern[], has now been deprecated as a result.


4.5.3. Boolean

Expressions may be linked together or negated using the Boolean logical operators and, or, and not, all of which are Python keywords. These Boolean operations are in highest-to-lowest order of precedence in Table 4.3. The not operator has the highest precedence and is immediately one level below all the comparison operators. The and and or operators follow, respectively.

Table 4.3. Standard Type Boolean Operators

Operator

Function

not expr

Logical NOT of expr [negation]

expr1 and expr2

Logical AND of expr1 and expr2 [conjunction]

expr1 orexpr2

Logical OR of expr1 and expr2 [disjunction]


>>> x, y = 3.1415926536, -1024 >>> x < 5.0 True >>> not [x < 5.0] False >>> [x < 5.0] or [y > 2.718281828] True >>> [x < 5.0] and [y > 2.718281828] False >>> not [x is y] True

Earlier, we introduced the notion that Python supports multiple comparisons within one expression. These expressions have an implicit and operator joining them together.

>>> 3 < 4 < 7    # same as "[ 3 < 4 ] and [ 4 < 7 ]" True

What are standard type built in functions in Python?

Python Built-in Functions.
print[ ] function..
type[ ] function..
input[ ] function..
abs[ ] function..
pow[ ] function..
dir[ ] function..
sorted[ ] function..
max[ ] function..

What are the 4 types of operators?

Operators.
arithmetic operators..
relational operators..
logical operators..

What are the basic operators in Python?

Basic Operators in Python With Examples.
Arithmetic Operators..
Relational Operators..
Bitwise Operators..
Assignment Operators..
Logical Operators..
Membership Operators..
Identity Operators..

How many types of common operators in Python?

Python has 7 types of operators. In this Python Operators article, we will discuss all of them in detail with examples.

Chủ Đề