See All Titles |
![]() ![]() OperatorsNext, I list the available Python operators in their precedence order. I also provide some specific details about some of them.
Tip
Keep in mind that x = y doesn't create a new copy of y. Instead, it makes a reference to it. However, if later you define x=x+1, a new reference for x is created, and then they become different because the operator has created a new object. Note that x.append(5) doesn't create a new reference to x because x changes itself without using a = operator.
As a good programmer, you need to know that logical operations can also be emulated by using if statements. Note that the return values are not limited to zeros and ones. The operation a and b can be written as the following: >>> def newand(a,b): … if not a: #If a is false … return a … else: … return b … The operation a or b can be written as the following: >>> def newor(a,b): … if a: #If a is true … return a … else: … return b … The operation not a can be written as the following: >>> def newnot(a): … if not a: #If a is false … return 0 … else: … return 1 … Augmented AssignmentStarting with Python 2.0, the language also implements a full set of augmented assignment operators. That includes: +=, -=, *=, /=, %=, **=, &=, |=, ^=, »=, and «= For example, instead of saying x = x+1, you can choose to say x += 1
|
Index terms contained in this section% (modulo) operator< < operator . (dot) operator = (equal) sign == operator 2nd AND operator augmented assignment operators bitwise operators 2nd copying objects dot (.) operator duplicating objects equal (=) sign exclusive OR (XOR) operator if statements in operator is not operator is operator left shifting modulo (%) operator not in operator objects copying operator operators augmented assignment OR operator right shifting shifting statements if XOR (exclusive OR) operator |
© 2002, O'Reilly & Associates, Inc. |