python-Data Operations

Source: Internet
Author: User
Tags arithmetic

Arithmetic operations

Example:ArithmeticOperator.py

#!/bin/env python#-*-coding:utf-8-*-__author__ = ' Stephen ' # set the initial value of A and Bprint ("%s%s%s"% ("=", "s Tart "," = "*)) A = 3b = 2print (" a=%s "% a) print (" b=%s "% B) # start Calculateprint ("%s%s%s "% (" = "," calculate "," = ") # A+bprint ("a+b=%s"% (a+b)) # A-bprint ("a-b=%s"% (a)) # A*bprint ("a*b=%s"% (a*b)) # A/bprint ("a/b=%s"% (A/b)) # A%bprint (" a%%b=%s "% (a% b)) # A**bprint (" a**b=%s "% (a**b)) # End of the Testprint ("%s%s%s "% (" = "," End "," = ")

Operation Result:

====start====a=3b=2====calculate====a+b=5a-b=1a*b=6a/b=1a%b=1a**b=9====end====
Comparison operation

Example:ComparisonOperator.py

#!/bin/env python#-*-coding:utf-8-*-__author__ = ' Stephen ' def num_compare (A, B): Print ("%s%s%s"% ("=", "Start", " = "*)" Print ("a=%s"% a) print ("b=%s"% b) # Start compare print ("%s%s%s"% ("=" *10, "compare", "=" *10)) #    If a=b? If a = = B: # Print (U "a equals B") print ("A is equal to B") Else: # Print (U "a not equals B") print ("A is no        T equal to B ") # if a!=b?, Type 1 if a! = B: # Print (U" a not equals B ") print (" A is not equal to B ") Else: # Print (U "a equals B") print ("A is equal to B") # if a!=b?, Type 2--and (deprecated) # This writing is about to be discarded if a <> B: # Print (U "a not equals B") print ("A is not equal to B") Else: # Print (U "a equals B") print ("A    is equal to B ") # if a<b?  If a < b: # print (U "a less than B") print ("A smaller than B") Else: # Print (U "A is not less than B") print ("A is    Not smaller than B ") # if a>b? If a > B: # Print (U "a greater than B") print ("A isLarger than B ") Else: # Print (U" a not less than B ") print (" A is not larger than B ") print ("%s%s%s "% (" = "*20," end "," = "*20)) # Round 1num_compare (3, 2) # Round 2num_compare (2, 3) # Round 3num_compare (2, 2)

Run results

====start====a=3b=2==========compare==========a isn't equal to BA are not equal to BA are not equal to BA are not smaller th An BA was larger than B====================end========================start====a=2b=3==========compare==========a is Not equal to BA isn't equal to BA are not equal to BA smaller than BA are not larger than b====================end========= ===============start====a=2b=2==========compare==========a is equal to BA are equal to BA are equal to BA are not smaller tha n BA is not larger than b====================end====================

 

Assignment operations

Example:AssignmentOperator.py

#!/bin/env python#-*-coding:utf-8-*-__author__ = ' Stephen ' def Set_initial_value (): # Set the initial value of the Nu    Mbers. Print ("%s%s%s"% ("=", "Initial value start", "=")) Global a Global B global C1 global C2 a = 3 B = 2 C1 = C2 = ten print ("a=%s"% a) print ("b=%s"% b) print ("c1=%s"% c1) print ("c2=%s"% C2) # Print  ("%s%s%s"% ("=", "Initial value End", "=") # Assignment Operator test# Simple assignment Operation Set_initial_value () C1 = a + bprint ("C1 = A + b ====>%s "% C1) # addition operator Set_initial_value () C1 + = AC2 = C2 +aprint (" C1 + = a ====>%s "% C1) Print (" C2 = C2 +a = = = = =%s "% C2) # subtraction operator Set_initial_value () C1-= AC2 = C2-aprint (" C1-= a ====>%s "% C1) Print (" C2 = c2-a ====>%s " % C2) # multiplication operator Set_initial_value () c1 *= AC2 = C2 * Aprint ("C1 *= a ====>%s"% C1) Print ("C2 = C2 * A ====>%s"% C2) # except Method operator Set_initial_value () c1/= AC2 = C2/aprint ("C1/= a ====>%s"% C1) Print ("C2 = c2/a ====>%s"% C2) # modulo operator set_ Initial_value () C1%= AC2 = c2% aprint ("C1%%= a ====>%s"% C1) Print ("C2 = C2 percent a ====>%s"% C2) # Power assignment operator Set_initial_value () C1 * *= AC2 = C2 * * Aprint ("C1 **= a ====>%s"% C1) Print ("C2 = C2 * * A ====>%s"% C2) # take divisible operator set_initial_value () C1//= AC2 = C2//Aprint ("C1//= a ====>%s"% C1) Print ("C2 = C2//A ====>%s"% C2)

Operation Result:

====initial value start====a=3b=2c1=10c2=10c1 = a + b ====> 5====initial value start====a=3b=2c1=10c2=10c1 + = a ====> ; 13c2 = C2 +a ====> 13====initial value start====a=3b=2c1=10c2=10c1 = a ====> 7c2 = c2-a ====> 7====initial valu E start====a=3b=2c1=10c2=10c1 *= a ====> 30c2 = C2 * A ====> 30====initial value start====a=3b=2c1=10c2=10c1/= A = = ==> 3c2 = c2/a ====> 3====initial value start====a=3b=2c1=10c2=10c1%= a ====> 1c2 = c2% a ====> 1====initi Al value Start====a=3b=2c1=10c2=10c1 **= a ====> 1000c2 = C2 * * A ====> 1000====initial value start====a=3b=2c1=10c2 =10C1//= a ====> 3c2 = c2//a ====> 3

  

Logical operations

Example:LogicalOperator.py

#!/bin/env python#-*-coding:utf-8-*-__author__ = ' Stephen ' # Definitiona = Trueb = Falsec = trued = Falseprint ("%s%s%s" % ("-" *10, "basic Test Start", "-" *10)) # Test and Operatorprint ("%s%s%s ====>%s"% (A, "and", B, A and b)) print ("%s  %s%s ====>%s "% (A," and ", C, A and C)) print ("%s%s%s ====>%s "% (b," and ", C, B and C)) print ("%s%s%s ====> %s "% (b," and ", D, b and D)) # Test or Operatorprint ("%s%s%s ====>%s "% (A," or ", B, A or b)) print ("%s%s%s ====&G T  %s "% (A," or ", C, a or c)) print ("%s%s%s ====>%s "% (b," or ", C, b or C)) print ("%s%s%s ====>%s "% (b," or ", D, B or D) # test not Operatorprint ("%s%s ====>%s"% ("no", a, not a)) print ('%s%s ====>%s '% ("not", B, not B)) Print ("%s%s%s"% ("-" *10, "basic Test End", "-" *10)) print ("%s%s%s"% ("-" *10, "number Test start", "-" *10)) # Test number C alculation# 0 ==> false# non 0 ==> truew = 0x = 2y = -1z = 0#print ("%s%s%s ====>%s"% (x, "and", Y, x and y) prin T ("%s%s%s ====>%s "% (x," and ", Z, X and z)) print ("%s%s%s ====>%s "% (x," or ", y, x or y)) print ("%s%s%s ====>%s "% (x," or ", Z, x or z) print ("%s%s%s ====>%s"% (W, "and", Z, W and z)) print ("%s%s%s ====>%s"% (W, "and", X, W and X) PRI NT ("%s%s%s ====>%s"% (W, "or", Z, W or z)) print ("%s%s%s"% ("-" *10, "number Test End", "-" *10))

Operation Result:

----------Basic Test Start----------true and False ====> Falsetrue and True ====> TrueFalse and True ====> Falsef Alse and False ====> Falsetrue or false ====> Truetrue or True ====> TrueFalse or True ====> TrueFalse or Fals E ====> falsenot  true ====> falsenot  False ====> true----------Basic test End--------------------Number Test start----------2 and-1 ====> -12 and 0 ====> or-1 ====> or 0 ====> 0 ====> and 2 ====> or 0 ====> 0----------number test end----------

  

Member operations

Example:MembershipOperator.py

#!/bin/env python#-*-coding:utf-8-*-__author__ = ' Stephen ' # Define a list.test_list = [1, 2, 3, 4, 5, 6]def test_in (item)  :    If item in Test_list:        print ('%s is a member of test_list%s '% (item, test_list) '    else:        ' Print ('%s ' is ' not '  A member of test_list%s "% (item, test_list)) def test_not_in (item):    If Item not in Test_list:        print ('%s ' is not a Member of Test_list%s "% (item, test_list))    else:        print ("%s is a member of test_list%s "% (item, test_list)) # T Estprint ("%s%s%s"% ("-" *10, "in Test", "-" *10)) test_in (4) test_in (7) print ("%s%s%s"% ("-" *10, "not in Test", "-" *10)) test _not_in (4) test_not_in (7)

 Operation Result:

----------in Test----------4 is a member of Test_list [1, 2, 3, 4, 5, 6]7 are not a member of Test_list [1, 2, 3, 4, 5, 6]- ---------not in Test----------4 is a member of Test_list [1, 2, 3, 4, 5, 6]7 are not a member of Test_list [1, 2, 3, 4, 5, 6]

  

Identity operations

Example:IdentityOperator.py

#!/bin/env python#-*-coding:utf-8-*-__author__ = ' Stephen ' Print ("%s%s%s"% ("-" *10, "number Test", "-" *10)) x = 2y = 2z = 3print ("x =%s"% x) print ("y =%s"% y) print ("z =%s"% z) if x is Y:print "x is Y" print ("ID (x) ====>%s"% ID (x ) Print ("id (y) ====>%s"% ID (y)) Else:print "x is not Y" print ("ID (x) ====>%s"% ID (x)) print ("id (y) ====>%s "% ID (y)) if x is Z:print" x is Z "print (" ID (x) ====>%s "% ID (x)) print (" id (z) ====>%s "% ID (  z)) Else:print "x is not Z" print ("ID (x) ====>%s"% ID (x)) print ("id (z) ====>%s"% ID (z)) print ("%s%s%s"% ("-" *10, "string test", "-" *10))    A = "Hello" b = "Hello" c = "World" Print ("a =%s"% a) print ("b =%s"% b) print ("c =%s"% c) if a was B:print "A is B" Print ("ID (a) ====>%s"% ID (a)) print ("id (b) ====>%s"% ID (b)) Else:print "A is not B" print ("ID (a) ====&G T %s "% ID (a)) print (" id (b) ====>%s "% ID (b)) If A is C:print" A is C "print (" ID (a) ====>%s "%ID (a)) print ("ID (c) ====>%s"% ID (c)) Else:print "A is not C" print ("ID (a) ====>%s"% ID (a)) print ("id (c) ====>%s "% ID (c))

Operation Result:

----------number Test----------x = 2y = 2z = 3x is Yid (x) ====> 5045844id (y) ====> 5045844x are not Zid (x) ====> 5 045844id (z) ====> 5045832----------string Test----------a = Hellob = Helloc = Worlda is bid (a) ====> 36823552id (b) = ===> 36823552a is not CID (a) ====> 36823552id (c) ====> 36823616

  

Bit arithmetic

Example:BitwiseOperator.py

#!/bin/env python#-*-coding:utf-8-*-__author__ = ' Stephen ' # set the initial value of A and Bprint ("%s%s%s"% ("=", "s Tart "," = ")) A = 254            # 254 = 1111 1110b = 192            # 192 = 1100 0000c = 0print (" a=%s,binary:%s "% (A, bin (a))) print (" b =%s,binary:%s "% (b, bin (b))) # Testprint ("%s%s%s "% ("-"*20," Test Start ","-"*20)) C = A & Bprint (" C = A & B ====& Gt %s, binary:%s "% (c, bin (c))) C = A | Bprint ("C = a |  b ====>%s, binary:%s "% (c, bin (c))) C = a ^ bprint (" C = a ^ b ====>%s, binary:%s "% (c, bin (c))) C = ~aprint (" C = ~a ====>%s, binary:%s "% (c, bin (c))) C = a << 2print (" C = a << 2 ====>%s, binary:%s "% (c, Bin ( c))) C = a >> 2print ("C = a >> 2 ====>%s, binary:%s"% (c, bin (c))) print ("%s%s%s"% ("-" *20, "Test End", "-" *20))

 Operation Result:

====start====a=254,binary:0b11111110b=192,binary:0b11000000--------------------Test start--------------------c = A & B ====> 192, binary:0b11000000c = a |  b ====> 254, binary:0b11111110c = a ^ b ====>, binary:0b111110c = ~a ====> -255, binary: -0b11111111c = a << 2 ====> 1016, binary:0b1111111000c = a >> 2 ====>, binary:0b111111--------------------Test En D--------------------

  

Operator Precedence

Example:ArithmeticOperatorPriority.py

#!/bin/env python#-*-coding:utf-8-*-__author__ = ' Stephen ' a = 12b = 3c = 4d = 2res = 0res = (A + b) * C/dprint ("(%d +  %d) *%d/%d ====>%d "% (a, B, C, D, res)) E = ((A + b) * c)/Dprint (" ((%d +%d) *%d)/%d ====>%d "% (a, B, C,  D, res)) E = (A + b) * (C/D) print ("(%d +%d) * (%d/%d) ====>%d"% (a, B, C, D, res)) E = a + (b * c)/dprint ("%d + (%d *%d)/%d ====>%d "% (a, B, C, D, RES))

 Operation Result:

(3) * 4/2 ====> ((+ 3) * 4)/2 ====> (+ 3) * (4/2) ====> 3012 + (3 * 4)/2 ====> 30

  

python-Data Operations

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.