Python arithmetic operators

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

Python arithmetic operators

The following hypothetical variables: a=10,b=20:

operator Description Example
+ Add-Two objects added A + B output result 30
- Minus-get negative numbers or one number minus the other -B Output Result-10
* Multiply by two number or return a string that is repeated several times A * b output result 200
/ Except-X divided by Y B/A Output Results 2
% Modulo-Returns the remainder of the division B% A output result 0
** Power-Returns the Y power of X A**b is 10 of 20, output 100000000000000000000
// Divide-Returns the integer part of the quotient 9//2 output result 4, 9.0//2.0 output 4.0
Example:#Python算术运算符a = 21b = 10c = 0 #两个对象相加c = a + bprint (' a+b value: ', c) #两个对象相见c = A-bprint (' A-B value: ', c) #两个对象相乘, or return a word that has been repeated several times String c = A * Bprint (' a*b value: ', c) #两个对象相除c = A/bprint (' A/b value: ', c) #取模, return the remainder of departure c = a% bprint (' a%b value: ', c) #修改变量a, B, C # exponentiation, return x The y power a = 2b = 3c = A * * bprint (' a**b value: ', c) #取整除, returns the integer part of the quotient a = 10b = 5c = A//bprint (' a//b value: ', c) result of the instance output:A+b Value: Value of 31a-b: Value of 11a*b: 210a/b value: 2.1a%b value: 1a**b value: 8a//b value: 2 python comparison operator

The following assumes that the variable A is 10 and the variable B is 20:

operator Description Example
== Equals-compares objects for equality (A = = B) returns FALSE.
!= Not equal-compares two objects for unequal (A! = B) returns TRUE.
> Greater than-returns whether x is greater than Y (A > B) returns FALSE.
< Less-Returns whether x is less than Y. All comparison operators return 1 for true, and return 0 for false. This distinction is equivalent to the special variable true and false. Note that these variable names are capitalized. (A < B) returns TRUE.
>= Greater than or equal-returns whether X is greater than or equal to Y. (a >= B) returns FALSE.
<= Less than or equal-returns whether X is less than or equal to Y. (a <= B) returns True.
Example:#python比较运算符 # All comparison operators return 1 for true, and return 0 for false a = 21b = 10c = 0  #比较对象是否相等if (a == &NBSP;B):     print (' a  equals  b ') else:    print (' a  not equal to   B ')   #比较两个对象是否不相等if (a != b):     print (' a  not equal to  b ') else:     print (' a  equals  b ')   #大于, returns whether X is greater than Yif (A&NBSP;&GT;&NBSP;B):     print (' A   Greater than  b ') else:   print (' a  greater than equals  b ')   #小于, returns whether X is less than yif  (a < &NBSP;B):    print (' a  less than  b ') else:   print (' a  greater than equals  b ')  #   Modify the values of variables  a  and  b  a = 5b = 20  #大于等于, returns whether x is greater than or equal to yif  ( a  <= b ):    print ("a  less than equals  b") else:   print ("a  greater than   b ")   #小于等于, returns whether x is less than or equal to yif  ( b >= a ):    print (" b   greater than or equal to  a ") ELSE:&NBSp;  print ("b  less than  a") result of the instance output:A not equal to BA is not equal to BA greater than BA greater than or equal to BA less than or equal to a python assignment operator

The following assumes that the variable A is 10 and the variable B is 20:

operator Description Example
= Simple assignment operator c = A + B assigns the result of the operation of A + B to c
+= Addition assignment operator c + = A is equivalent to C = C + A
-= Subtraction assignment operator C-= A is equivalent to C = c-a
*= Multiplication assignment operator C *= A is equivalent to C = c * A
/= Division assignment operator C/= A is equivalent to C = c/a
%= Modulo assignment operator C%= A is 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
Python logical operators

The Python language supports logical operators, with the following assumption that the variable A is ten and B is 20:

operator Logical Expressions Description Example
and X and Y Boolean "and"-if x is False,x and y returns FALSE, otherwise it returns the computed value of Y. (A and B) returns 20.
Or X or Y Boolean "or"-if X is non-0, it returns the value of x, otherwise it returns the computed value of Y. (A or B) returns 10.
Not Not X Boolean "Non"-returns False if X is True. If X is False, it returns TRUE. Not (A and B) returns False
Python member operators

In addition to some of the above operators, Python also supports member operators, which contain a series of members, including strings, lists, or tuples.

operator Description Example
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.
Example:#python成员运算符a = 10b = 20lists = [1, 2, 3, 4, 5] #如果在指定的序列中找到值返回True, otherwise returns Falseif (a in lists): print (' variable A in the given list lists ') El Se:print (' variable A ' does not exist in the given list lists ') #如果在指定的序列中没有找到值返回True, otherwise returns Falseif (b not in lists): print (' variable B does not exist in the given list lists ') Else:p Rint (' variable B ' in the given list lists ') #修改变量a的值 # returns True if the value is found in the specified sequence, otherwise falsea = 2if (A in lists): print (' variable A in the given list ') Else:print (' variable A is not in the given list ') result of the instance output:Variable A is not in the given list lists variable B is not in the given list lists variable A in the given list the Python identity operator

The identity operator is used to compare the storage units of two objects

operator Description Example
Is is to determine whether two identifiers are referenced from an object x is y, similar to ID (x) = = ID (y) , returns True if the same object is referenced, otherwise False
is not Is does not determine whether two identifiers are referenced from different objects x is not y , similar to ID (a)! = ID (b). Returns True if the reference is not the same object, otherwise False is returned.
Example:#Python身份运算符a = 20b = #is是判断两个标识符是否是引用同一个对象if (A is B): print (' A and B have the same identity ') Else:print (' A and b do not have the same identity ') #is not to determine two identities Characters are not referenced from different objects if (A is not B): print (' A and b do not have the same identity ') Else:print (' A and B have the same identity ') #修改变量b的值b = if (A is B): print (' A and B have the same label Else:print (' A and b do not have the same identity ') if (A is not B): print (' A and b do not have the same identity ') Else:print (' A and B have the same identity ') result of the instance output:A and B have the same identity A and B have the same identity A and b do not have the same identity A and b do not have the same identity as the Python operator precedence

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

operator Description
** 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

Python arithmetic operators

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.