首页 > 代码库 > Python-数据运算

Python-数据运算

算数运算

技术分享

Example:ArithmeticOperator.py

#!/bin/env python# -*- coding:utf-8 -*-__author__ = ‘Stephen‘# set the initial value of a and bprint("%s%s%s" % ("="*4, "start", "="*4))a = 3b = 2print("a=%s" % a)print("b=%s" % b)# start calculateprint("%s%s%s" % ("="*4, "calculate", "="*4))# 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))# a%bprint("a%%b=%s" % (a % b))# a**bprint("a**b=%s" % (a**b))# end of the testprint("%s%s%s" % ("="*4, "end", "="*4))

  运行结果:

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

  比较运算

技术分享

 Example:ComparisonOperator.py

#!/bin/env python# -*- coding:utf-8 -*-__author__ = ‘Stephen‘def num_compare(a, b):    print("%s%s%s" % ("="*4, "start", "="*4))    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等于b")        print("a is equal to b")    else:        # print(u"a不等于b")        print("a is NOT equal to b")    # if a!=b ?, Type 1    if a != b:        # print(u"a不等于b")        print("a is NOT equal to b")    else:        # print(u"a等于b")        print("a is equal to b")    # if a!=b ?, Type 2 --> (deprecated)    # 这种写法即将被废弃    if a <> b:        # print(u"a不等于b")        print("a is NOT equal to b")    else:        # print(u"a等于b")        print("a is equal to b")    # if a<b ?    if a < b:        # print(u"a小于b")        print("a smaller than b")    else:        # print(u"a不小于b")        print("a is NOT smaller than b")    # if a>b ?    if a > b:        # print(u"a大于b")        print("a is larger than b")    else:        # print(u"a不大于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)

  运行结果

====start====a=3b=2==========compare==========a is NOT equal to ba is NOT equal to ba is NOT equal to ba is NOT smaller than ba is larger than b====================end========================start====a=2b=3==========compare==========a is NOT equal to ba is NOT equal to ba is NOT equal to ba smaller than ba is NOT larger than b====================end========================start====a=2b=2==========compare==========a is equal to ba is equal to ba is equal to ba is NOT smaller than ba is NOT larger than b====================end====================

 

赋值运算

技术分享

 Example:AssignmentOperator.py

#!/bin/env python# -*- coding:utf-8 -*-__author__ = ‘Stephen‘def set_initial_value():    # set the initial value of the numbers.    print("%s%s%s" % ("="*4, "initial value start", "="*4))    global a    global b    global c1    global c2    a = 3    b = 2    c1 = 10    c2 = 10    print("a=%s" % a)    print("b=%s" % b)    print("c1=%s" % c1)    print("c2=%s" % c2)    # print("%s%s%s" % ("="*4, "initial value end", "="*4))# Assignment Operator Test# 简单赋值运算set_initial_value()c1 = a + bprint("c1 = a + b ====> %s" % c1)# 加法运算符set_initial_value()c1 += ac2 = c2 +aprint("c1 += a ====> %s" % c1)print("c2 = c2 +a ====> %s" % c2)# 减法运算符set_initial_value()c1 -= ac2 = c2 -aprint("c1 -= a ====> %s" % c1)print("c2 = c2 -a ====> %s" % c2)# 乘法运算符set_initial_value()c1 *= ac2 = c2 * aprint("c1 *= a ====> %s" % c1)print("c2 = c2 * a ====> %s" % c2)# 除法运算符set_initial_value()c1 /= ac2 = c2 / aprint("c1 /= a ====> %s" % c1)print("c2 = c2 / a ====> %s" % c2)# 取模运算符set_initial_value()c1 %= ac2 = c2 % aprint("c1 %%= a ====> %s" % c1)print("c2 = c2 %% a ====> %s" % c2)# 幂赋值运算符set_initial_value()c1 **= ac2 = c2 ** aprint("c1 **= a ====> %s" % c1)print("c2 = c2 ** a ====> %s" % c2)# 取整除运算符set_initial_value()c1 //= ac2 = c2 // aprint("c1 //= a ====> %s" % c1)print("c2 = c2 // a ====> %s" % c2)

  运行结果:

====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 value 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====initial 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

  

逻辑运算

技术分享

 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 ====> %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" % ("not", 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 calculation# 0 ==> False# 非0 ==> Truew = 0x = 2y = -1z = 0#print("%s %s %s ====> %s" % (x, "and", y, x and y))print("%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))print("%s %s %s ====> %s" % (w, "or", z, w or z))print("%s%s%s" % ("-"*10, "number test end", "-"*10))

  运行结果:

----------basic test start----------True and False ====> FalseTrue and True ====> TrueFalse and True ====> FalseFalse and False ====> FalseTrue or False ====> TrueTrue or True ====> TrueFalse or True ====> TrueFalse or False ====> Falsenot  True ====> Falsenot  False ====> True----------basic test end--------------------number test start----------2 and -1 ====> -12 and 0 ====> 02 or -1 ====> 22 or 0 ====> 20 and 0 ====> 00 and 2 ====> 00 or 0 ====> 0----------number test end----------

  

成员运算

技术分享

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))# Testprint("%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)

  运行结果:

----------in test----------4 is a member of test_list [1, 2, 3, 4, 5, 6]7 is 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 is NOT a member of test_list [1, 2, 3, 4, 5, 6]

  

身份运算

技术分享

 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 is 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) ====> %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))

  运行结果:

----------number test----------x = 2y = 2z = 3x is yid(x) ====> 5045844id(y) ====> 5045844x is NOT zid(x) ====> 5045844id(z) ====> 5045832----------string test----------a = hellob = helloc = worlda is bid(a) ====> 36823552id(b) ====> 36823552a is NOT cid(a) ====> 36823552id(c) ====> 36823616

  

位运算

技术分享

Example:BitwiseOperator.py

#!/bin/env python# -*- coding:utf-8 -*-__author__ = ‘Stephen‘# set the initial value of a and bprint("%s%s%s" % ("="*4, "start", "="*4))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 ====> %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))

  运行结果:

====start====a=254,binary:0b11111110b=192,binary:0b11000000--------------------Test Start--------------------c = a & b ====> 192 , binary: 0b11000000c = a | b ====> 254 , binary: 0b11111110c = a ^ b ====> 62 , binary: 0b111110c = ~a ====> -255 , binary: -0b11111111c = a << 2 ====> 1016 , binary: 0b1111111000c = a >> 2 ====> 63 , binary: 0b111111--------------------Test End--------------------

  

运算符优先级

技术分享

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))

  运行结果:

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

  

Python-数据运算