"Fifth" Python operator

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators logical operators

Python arithmetic operators
Operator Describe Instance
+ Add? Two objects added A + b?
- Reducing? Subtraction of two objects A-B
* By?? Multiply two numbers A * b
/ Except?? Divide two numbers b/a
% Modulo? Returns the remainder of a division B% A
** Classification Returns the Y power of X A**b
// The integer portion of the returned quotient 9//2
#!/usr/bin/env?python#-*-?coding:?utf-8?-*-a?=?21b?=?10c?=?0c?=?a?+?bprint? (The value of 1.c is: ", c) c?=?a?-? bprint? (The value of 2.c is: ", c) c?=?a?*?bprint? (The value of 3.c is: ", c) c?=?a?/?bprint? (The value of 4.c is: ", c) c?=?a?%?? Bprint? ("5.c Value:", c) #? Modify variables? A, B, ca?=?2b?=?3c?=?a**b?print? (The value of 6.C is: ", c) a?=?10b?=?5c?=?a//b?print? (The value of 7.C is: ", c)

The result of the above example output:

The value of 1.c is:? The value of 312.C is:? The value of 113.C is:? The value of 2104.C is:? The value of 2.15.C is:? The value of 16.C is:? 87.C is:? 2


Python comparison operators
Operator Describe Instance
== Compare objects Equal (A = = b)
!= Not equal to?? Compare two objects are not equal (A! = b)
> Greater than??? returns whether X is greater than Y (A > B)
< Less than??? returns whether X is less than Y (A < b)
>= Greater than or equal to? Returns whether x is greater than or equal to Y (A >= B)
<= Less than equals? Returns whether x is less than or equal to Y (A <= B)


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

#!/usr/bin/env?python#-*-?coding:?utf-8?-*-a?=?21b?=?10c?=?0if? (a?==?b):???? Print? ("1.? A? equals B ") Else:???? Print? ("1.? A? Not equal to? B ") if? (? a?! =?B):???? Print? ("2.? A? Not equal to? B ") Else:???? Print? ("2.? A? equals B ") if? (? a?<?b?):???? Print? ("4.? A? Less than B ") Else:???? Print? ("4.? A? greater than? B ") if? (? a?>?b?):???? Print? ("5.? A? greater than? B ") Else:???? Print? ("6.? A? Less than? B ") if? (? a?>=?b?):???? Print? ("7.? A? greater than or equal to? B ") Else:???? Print? ("7.? A? Less than? b? ") If? (? a?<=?b):???? Print? ("8.? A? Less than equals? B ") Else:???? Print? ("8.? A? greater than? B ")


The result of the above example output:

1.?a? Not equal to B2.? A? Not equal to B4.? A? greater than B5.? A? greater than B7.? A? greater than or equal to B8.? A? greater than? b


Python assignment operator
Operator Describe Instance
= Simple assignment operator c = A + B? Assign the result of the operation of A + B to c
+= Addition assignment operator c + = A??? Equivalent to C = C + A
-= Subtraction assignment operator c-= a??? Equivalent to C = c-a
*= Multiplication assignment operator C *= a??? Equivalent to C = c * A
/= Division assignment operator C/= a??? Equivalent to C = c/a
%= Modulo assignment operator C%= a??? Equivalent to C = c% A
**= Power assignment operator C **= A??? is equivalent to C = c * * A
//= Take the divisible assignment operator C//= A??? is equivalent to C = c//A


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

#!/usr/bin/env?python#-*-?coding:?utf-8?-*-a?=?21b?=?10c?=?0c?=?a?+?bprint? (The value of "1?-? C?" is: ",? c) c?+=?aprint? (The value of "2?-? C?" is: ",? c) c?*=?aprint? (The value of "3?-? C?" is: ",? c) c?/=?a?print? (The value of "4?-? C?" is: ",? c) c?=?2c?%=?aprint? (The value of "5?-? C?" is: ",? c) c?**=?aprint? (The value of "6?-? C?" is: ",? c) c?//=?aprint? (The value of "7?-? C?" is: ",? c)


The result of the above example output:

1?-? c? The value is:? 312?-? c? The value is:? The value of 523?-? C? is:? The value of 10924?-? c? is:? The value of the 52.05?-? c? is:? The value of?-? C? is:? The value of the 20971527?-? c?



Python logical operators
Operator Logical Expressions Describe
and X and Y Boolean "and"
Or X or Y Boolean "or"
Not Not X Boolean "Non"

The following example shows the operation of all the logical operators of Python:

#!/usr/bin/env?python#-*-?coding:?utf-8?-*-a?=?21b?=?10if? (? a?and?b?):??? Print? ("1?-? variable?" a "and" B "are all true") Else:??? Print? ("1?-? variable?" a "and" B "have a" not "true") if? (? a?or?b?):??? Print? ("2?-? variable?" a "and" B "are all true, or one of the variables is true") Else:??? Print? ("2?-? variable?" a "and" B "are not true")??? # Change the value of the variable? A? a?=?0if? (? a?and?b?):??? Print? ("3?-? variable?" a "and" B "are all true") Else:??? Print? ("3?-? variable?" a "and" B "have a" not "true") if? (? a?or?b?):??? Print? ("4?-? variable?" a "and" B "are all true, or one of the variables is true") Else:??? Print? ("4?-? variable?" a "and" B "are not" true ") If?not (? A?and?b?):??? Print? ("5?-? variable?" a "and" B "are all false, or one of the variables is" false ") Else:??? Print? ("5?-? variable?" a "and" B "are All" true ")

The result of the above example output:

1?-? variables? A, and B? are all True2?-? variables? True, or one of the variables is? true3?-? variable? A? and B? Have a not for the true4?-? variable? A? and B? Are all true, or one of the variables is? True5?-? variables? A? and B? False, or one of the variables is? false



Python member operators
Operator Describe Instance
Inch Returns False if the value found in the specified sequence returns True. 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 False. X is not in the Y sequence if x does not return True in the y sequence.

The following example shows the operation of all the member operators of Python

#!/usr/bin/env?python#-*-?coding:?utf-8?-*-a?=?10b?=?20list?=? [1,?2,?3,?4,?5?]; If? (? a?in?list?):??? Print? ("1?-? variable?" A "in the given list?") Else:??? Print? ("1?-? variable? A? Not in the given list?" list? ") if? (? b?not?in?list?):??? Print? ("2?-? variable? b. Not in the given list? list") Else:??? Print? ("2?-? variable?" B? In the given list????????????? a?=?2if. (? a?in?list?):??? Print? ("3?-? variable?" A "in the given list?") Else:??? Print? ("3?-? variable?" A? is not in the given list? list ")

The result of the above example output:

1?-? variable? A? Not in the given list? List 2?-? variable? b? is not in the given list? list 3?-? variable? A? In the given list?



Python operator Precedence

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

Operator Describe
** Index (highest priority)
~ + - Bitwise flip, unary Plus and minus (the last two methods are called [email protected] and [email protected])
* / % // Multiply, divide, modulo, and divide
+ - Addition subtraction
>> << Shift right, left shift operator
& Bit ' and '
^ | Bitwise operators
<= < > >= Comparison operators
<> = = = equals operator
= %= /= //= -= += *= **= Assignment operators
Is isn't Identity operator
In No in Member operators
Not OR and logical operators

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

#!/usr/bin/env?python#-*-?coding:?utf-8?-*-a?=?20b?=?10c?=?15d?=?5e?=?0e?=? (a?+?b)? *?c?/?d??????? # (? 30?*?15?)? /?5print? (a?+?b) *?c?/?d? The result of the operation is: ",?? e) e?=? (a?+?b) *?c)/?d????? #? (30?*?15?)? /?5print? ((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 result of the above example output:

(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.0a?+? (b?*?c)/?d? The result of the operation is:? 50.0


"Fifth" Python operator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.