程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Graphical Python | operator

編輯:Python

author : Han Xinzi @ShowMeAI
Tutorial address :http://www.showmeai.tech/tuto...
This paper addresses :http://www.showmeai.tech/article-detail/68
Statement : copyright , For reprint, please contact the platform and the author and indicate the source


1.Python Operator

Operators are used to perform operations on variables and values . A simple example 5 +6 = 11 . In the example ,5 and 6 go by the name of Operands ,"+" Called operator .

Python The language supports the following types of operators :

  • Arithmetic operator
  • Compare ( Relationship ) Operator
  • Assignment operator
  • Logical operators
  • An operator
  • member operator
  • Identity operator
  • Operator priority

2.Python Arithmetic operator

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

Operator describe example + Add - Add two objects a + b Output results 30- reduce - Get a negative number or a number minus another number a - b Output results -10* ride - Multiply two numbers or return a string repeated several times a * b Output results 200/ except - x Divide yb / a Output results 2% modulus - Returns the remainder of the Division b % a Output results 0** power - return x Of y The next power a**b by 10 Of 20 Power , Output results 100000000000000000000// Divide and conquer - Returns the integer part of the quotient ( Rounding down >>> 9//2 4 >>> -9//2 -5

The following code demonstrates Python Operation of all arithmetic operators ( The code can be in On-line python3 Environmental Science Run in ):

a = 30
b = 10
c = 0
c = a + b
print(" The first 1 After three operations ,c The value of is :", c)
c = a - b
print(" The first 2 After three operations ,c The value of is :", c)
c = a * b
print(" The first 3 After three operations ,c The value of is :", c)
c = a / b
print(" The first 4 After three operations ,c The value of is :", c)
c = a % b
print(" The first 5 After three operations ,c The value of is :", c)
# Modify variables a 、b 、c
a = 2
b = 3
c = a**b
print(" The first 6 After three operations ,c The value of is :", c)
a = 10
b = 5
c = a//b
print(" The first 7 After three operations ,c The value of is :", c)

The output of the above code :

 The first 1 After three operations ,c The value of is : 40
The first 2 After three operations ,c The value of is : 20
The first 3 After three operations ,c The value of is : 300
The first 4 After three operations ,c The value of is : 3.0
The first 5 After three operations ,c The value of is : 0
The first 6 After three operations ,c The value of is : 8
The first 7 After three operations ,c The value of is : 2

3.Python Comparison operator

The following hypothetical variables a by 10, Variable b by 20:

Operator describe example == be equal to - Compare objects for equality (a == b) return False.!= It's not equal to - Compare two objects for inequality (a != b) return true.<> It's not equal to - Compare two objects for inequality .python3 obsolete .(a <> b) return true. This operator is similar to != .> Greater than - return x Is it greater than y(a > b) return False.< Less than - return x Is less than y. All comparison operators return 1 Said really , return 0 Said the false . This is different from special variables True and False Equivalent .(a < b) return true.>= Greater than or equal to - return x Greater than or equal to y.(a >= b) return False.<= Less than or equal to - return x Less than or equal to y.(a <= b) return true.

The following code demonstrates Python Operation of all comparison operators ( The code can be in On-line python3 Environmental Science Run in ):

a = 30
b = 10
c = 0
if a == b :
print("a be equal to b")
else:
print("a It's not equal to b")
if a != b :
print("a It's not equal to b")
else:
print("a be equal to b")
if a < b :
print("a Less than b" )
else:
print("a Greater than or equal to b")
if a > b :
print("a Greater than b")
else:
print("a Less than or equal to b")
# Modify variables a and b Value 
a = 5
b = 20
if a <= b :
print("a Less than or equal to b")
else:
print("a Greater than b")
if b >= a :
print("b Greater than or equal to a")
else:
print("b Less than a")

The output of the above example :

a It's not equal to b
a It's not equal to b
a Greater than or equal to b
a Greater than b
a Less than or equal to b
b Greater than or equal to a

4.Python Assignment operator

The following hypothetical variables a by 10, Variable b by 20:

Operator describe example = Simple assignment operators c = a + b take a + b The operation result of is assigned as 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 Equivalent to c = c ** a//= Integer division assignment operator c //= a Equivalent to c = c // a

The following code demonstrates Python Operation of all assignment operators ( The code can be in On-line python3 Environmental Science Run in ):

a = 30
b = 10
c = 0
c = a + b
print(" The first 1 After three operations ,c The value of is :", c)
c += a
print(" The first 2 After three operations ,c The value of is :", c )
c *= a
print(" The first 3 After three operations ,c The value of is :", c )
c /= a
print(" The first 4 After three operations ,c The value of is :", c )
c = 2
c %= a
print(" The first 5 After three operations ,c The value of is :", c)
c **= a
print(" The first 6 After three operations ,c The value of is :", c)
c //= a
print(" The first 7 After three operations ,c The value of is :", c)

The output of the above code :

 The first 1 After three operations ,c The value of is : 40
The first 2 After three operations ,c The value of is : 70
The first 3 After three operations ,c The value of is : 2100
The first 4 After three operations ,c The value of is : 70.0
The first 5 After three operations ,c The value of is : 2
The first 6 After three operations ,c The value of is : 1073741824
The first 7 After three operations ,c The value of is : 35791394

5.Python An operator

Bitwise operators calculate numbers as binary .Python The bitwise algorithm in is as follows :

Variables in the following table a by 60,b by 13, The binary format is as follows :

a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
Operator describe example & Bitwise and operator : Two values involved in the operation , If both corresponding bits are 1, The result of this bit is 1, Otherwise 0(a & b) Output results 12 , Binary interpretation : 0000 1100\ bitwise or operator : As long as one of the two corresponding binary bits is 1 when , The result bit is 1.(a \b) Output results 61 , Binary interpretation : 0011 1101^ bitwise exclusive or operator : When two corresponding bits are different , The result is 1(a ^ b) Output results 49 , Binary interpretation : 0011 0001~ Bitwise negation operator : Invert each binary bit of data , Namely the 1 Turn into 0, hold 0 Turn into 1 .~x Be similar to -x-1(~a ) Output results -61 , Binary interpretation : 1100 0011, In the complement form of a signed binary number .<< Left move operator : All the binary bits of the operand are shifted to the left by several bits , from << The number on the right specifies the number of digits to move , High level discard , Low complement 0.a << 2 Output results 240 , Binary interpretation : 1111 0000>> Move right operator : hold ">>" Each binary of the left operand is shifted several bits to the right ,>> The number on the right specifies the number of digits to move a >> 2 Output results 15 , Binary interpretation : 0000 1111

The following code demonstrates Python Operation of all bit operators ( The code can be in On-line python3 Environmental Science Run in ):

a = 60 # 60 = 0011 1100 
b = 13 # 13 = 0000 1101 
c = 0
c = a & b; # 12 = 0000 1100
print(" The first 1 After three operations ,c The value of is :", c)
c = a | b; # 61 = 0011 1101 
print(" The first 2 After three operations ,c The value of is :", c)
c = a ^ b; # 49 = 0011 0001
print(" The first 3 After three operations ,c The value of is :", c)
c = ~a; # -61 = 1100 0011
print(" The first 4 After three operations ,c The value of is :", c)
c = a << 2; # 240 = 1111 0000
print(" The first 5 After three operations ,c The value of is :", c)
c = a >> 2; # 15 = 0000 1111
print(" The first 6 After three operations ,c The value of is :", c)

The output of the above code :

 The first 1 After three operations ,c The value of is : 12
The first 2 After three operations ,c The value of is : 61
The first 3 After three operations ,c The value of is : 49
The first 4 After three operations ,c The value of is : -61
The first 5 After three operations ,c The value of is : 240
The first 6 After three operations ,c The value of is : 15

6.Python Logical operators

Python Language supports logical operators , The following hypothetical variables a by 10, b by 20:

Operator Logical expression describe example andx and y Boolean " And " - If x by False,x and y return False, Otherwise it returns y Calculated value .(a and b) return 20.orx or y Boolean " or " - If x Right and wrong 0, It returns x Calculated value , Otherwise it returns y Calculated value .(a or b) return 10.notnot x Boolean " Not " - If x by True, return False . If x by False, It returns True.not(a and b) return False

The following code demonstrates Python Operation of all logical operators ( The code can be in On-line python3 Environmental Science Run in ):

a = 10
b = 20
if a and b :
print("1. Variable a and b All for true")
else:
print("1. Variable a and b One didn't do it true")
if a or b :
print("2. Variable a and b All for true, Or one of the variables is true")
else:
print("2. Variable a and b Not for true")
# Modify variables a Value 
a = 0
if a and b :
print("3. Variable a and b All for true")
else:
print("3. Variable a and b One didn't do it true")
if a or b :
print("4. Variable a and b All for true, Or one of the variables is true")
else:
print("4. Variable a and b Not for true")
if not( a and b ):
print("5. Variable a and b All for false, Or one of the variables is false")
else:
print("5. Variable a and b All for true")

The output of the above code :

1. Variable a and b All for true
2. Variable a and b All for true, Or one of the variables is true
3. Variable a and b One didn't do it true
4. Variable a and b All for true, Or one of the variables is true
5. Variable a and b All for false, Or one of the variables is false

7.Python member operator

In addition to some of the above operators ,Python Member operators are also supported , The test case contains a series of members , Including strings , List or tuple .

Operator describe example in Returns if a value is found in the specified sequence True, Otherwise return to False.x stay y In sequence , If x stay y Return in sequence True.not in If no value is found in the specified sequence, return True, Otherwise return to False.x be not in y In sequence , If x be not in y Return in sequence True.

The following code demonstrates Python Operation of all member operators ( The code can be in On-line python3 Environmental Science Run in ):

a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
if ( a in list ):
print("1. Variable a In the given list list in ")
else:
print("1. Variable a Not in the given list list in ")
if ( b not in list ):
print("2. Variable b Not in the given list list in ")
else:
print("2. Variable b In the given list list in ")
# Modify variables a Value 
a = 2
if ( a in list ):
print("3. Variable a In the given list list in ")
else:
print("3. Variable a Not in the given list list in ")

The output of the above code :

1. Variable a Not in the given list list in
2. Variable b Not in the given list list in
3. Variable a In the given list list in 

8.Python Identity operator

Identity operators are used to compare the storage units of two objects

Operator describe example isis Is to determine whether two identifiers are referenced from an object x is y, similar id(x) == id(y) , If it refers to the same object, it returns True, Otherwise return to Falseis notis not Is to determine whether two identifiers are referenced from different objects x is not y , similar id(x) != id(y). If the reference is not the same object, return the result True, Otherwise return to False.

notes : id() Function to get the memory address of an object .

The following code demonstrates Python Operation of all identity operators ( The code can be in On-line python3 Environmental Science Run in ):

a = 20
b = 20
if ( a is b ):
print("1.a and b Have the same logo ")
else:
print("1.a and b There is no identical logo ")
if ( a is not b ):
print("2.a and b There is no identical logo ")
else:
print("2.a and b Have the same logo ")
# Modify variables b Value 
b = 30
if ( a is b ):
print("3.a and b Have the same logo ")
else:
print("3.a and b There is no identical logo ")
if ( a is not b ):
print("4.a and b There is no identical logo ")
else:
print("4.a and b Have the same logo ")

The output of the above example :

1.a and b Have the same logo
2.a and b Have the same logo
3.a and b There is no identical logo
4.a and b There is no identical logo 
is And == difference :
is Used to determine whether two variable reference objects are the same ( The same memory space ), == Used to determine whether the values of reference variables are equal .
>>> a = [1, 2, 3]
>>> b = a
>>> b is a
True
>>> b == a
True
>>> b = a[:]
>>> b is a
False
>>> b == a
True

9.Python Operator priority

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

Operator describe ** Index ( Highest priority )~ + - Flip by bit , One yuan plus sign and minus sign ( The last two methods are called [email protected] and [email protected])* / % // ride , except , Mold and division + - addition and subtraction >> << Move right , Shift left operator & position 'AND'^ \ An operator <= < > >= Comparison operator <> == != Equals operator = %= /= //= -= += = *= Assignment operator is is not Identity operator in not in member operator not and or Logical operators

The following code demonstrates Python All operator priority operations ( The code can be in On-line python3 Environmental Science Run in ):

a = 20
b = 10
c = 15
d = 5
e = 0
e = (a + b) * c / d #( 30 * 15 ) / 5
print("(a + b) * c / d The result of operation is :", e)
e = ((a + b) * c) / d # (30 * 15 ) / 5
print("((a + b) * c) / d The result of operation is :", e)
e = (a + b) * (c / d); # (30) * (15/5)
print("(a + b) * (c / d) The result of operation is :", e)
e = a + (b * c) / d; # 20 + (150/5)
print("a + (b * c) / d The result of operation is :", e)

The output of the above example :

(a + b) * c / d The result of operation is : 90.0
((a + b) * c) / d The result of operation is : 90.0
(a + b) * (c / d) The result of operation is : 90.0
a + (b * c) / d The result of operation is : 50.0

Data and code download

The code for this tutorial series can be found in ShowMeAI Corresponding github Download , Can be local python Environment is running , Babies who can surf the Internet scientifically can also use google colab One click operation and interactive operation learning Oh !

This tutorial series covers Python The quick look-up table can be downloaded and obtained at the following address :

  • Python Quick reference table

Expand references

  • Python course —Python3 file
  • Python course - Liao Xuefeng's official website

ShowMeAI Recommended articles

  • python Introduce
  • python Installation and environment configuration
  • python Basic grammar
  • python Basic data type
  • python Operator
  • python Condition control and if sentence
  • python Loop statement
  • python while loop
  • python for loop
  • python break sentence
  • python continue sentence
  • python pass sentence
  • python String and operation
  • python list
  • python Tuples
  • python Dictionaries
  • python aggregate
  • python function
  • python Iterators and generators
  • python data structure
  • python modular
  • python File read and write
  • python File and directory operations
  • python Error and exception handling
  • python object-oriented programming
  • python Namespace and scope
  • python Time and date

ShowMeAI A series of tutorials are recommended

  • The illustration Python Programming : From introduction to mastery
  • Graphical data analysis : From introduction to mastery
  • The illustration AI Mathematical basis : From introduction to mastery
  • Illustrate big data technology : From introduction to mastery


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved