Python Expressions and operators

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

Expressions and operators

The Python language supports the following types of operators:

    • Arithmetic operators
    • Compare (relational) operators
    • Assignment operators
    • logical operators
    • Bitwise operators
    • Member operators
    • Identity operator
    • Operator Precedence
Arithmetic operators
operator Description
+ Add-Two objects added
- Minus-get negative numbers or one number minus the other
* Multiply by two number or return a string that is repeated several times
/ Except-X divided by Y
% Modulo-Returns the remainder of the division
** Power-Returns the Y power of X
// Divide-Returns the integer part of the quotient
Comparison operators
operator Description
== Equals-compares objects for equality
!= Not equal-compares two objects for unequal
<> Not equal-compares two objects for unequal
> Greater than-returns whether x is greater than Y
< 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.
>= Greater than or equal-returns whether X is greater than or equal to Y.
<= Less than or equal-returns whether X is less than or equal to Y.
Assignment operators
operator Description
= Simple assignment operator
+= Addition assignment operator
-= Subtraction assignment operator
*= Multiplication assignment operator
/= Division assignment operator
%= Modulo assignment operator
**= Power assignment operator
//= Take the divisible assignment operator
Bitwise operators

A bitwise operator computes a number as a binary. The bitwise algorithms in Python are as follows:

logical operators
operator Logical Expressions Description
and X and Y Boolean "and"-if x is False,x and y returns FALSE, otherwise it returns the computed value of y
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.
Not Not X Boolean "Non"-returns False if X is True. If X is False, it returns True
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
Inch Returns False if the value found in the specified sequence returns True.
Not in Returns True if no value is found in the specified sequence, otherwise False.

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

#!/usr/bin/python# -*- coding: UTF-8 -*- a = 10b = 20list = [1, 2, 3, 4, 5 ]; if ( a in list ):   print "1 - 变量 a 在给定的列表中 list 中"else:   print "1 - 变量 a 不在给定的列表中 list 中" if ( b not in list ):   print "2 - 变量 b 不在给定的列表中 list 中"else:   print "2 - 变量 b 在给定的列表中 list 中" # 修改变量 a 的值a = 2if ( a in list ):   print "3 - 变量 a 在给定的列表中 list 中"else:   print "3 - 变量 a 不在给定的列表中 list 中"
Identity operator
operator Description
Is is to determine whether two identifiers are referenced from an object
is not Is does not determine whether two identifiers are referenced from different objects

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

#!/usr/bin/python# -*- coding: UTF-8 -*- a = 20b = 20 if ( a is b ):   print "1 - a 和 b 有相同的标识"else:   print "1 - a 和 b 没有相同的标识" if ( a is not b ):   print "2 - a 和 b 没有相同的标识"else:   print "2 - a 和 b 有相同的标识" # 修改变量 b 的值b = 30if ( a is b ):   print "3 - a 和 b 有相同的标识"else:   print "3 - a 和 b 没有相同的标识" if ( a is not b ):   print "4 - a 和 b 没有相同的标识"else:   print "4 - a 和 b 有相同的标识"

Attention:

    • IS and the == difference:
    • is used to determine whether the two variables refer to the same object, = = is used to determine whether the value of the reference variable is equal.
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

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

#!/usr/bin/python# -*- coding: UTF-8 -*- a = 20b = 10c = 15d = 5e = 0 e = (a + b) * c / d       #( 30 * 15 ) / 5print "(a + b) * c / d 运算结果为:",  e e = ((a + b) * c) / d     # (30 * 15 ) / 5print "((a + b) * c) / d 运算结果为:",  e e = (a + b) * (c / d);    # (30) * (15/5)print "(a + b) * (c / d) 运算结果为:",  e e = a + (b * c) / d;      #  20 + (150/5)print "a + (b * c) / d 运算结果为:",  e

Python Expressions and operators

Related Article

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.