Python-資料運算

That's_it發表於2016-09-01

算數運算

Example:ArithmeticOperator.py

#!/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Stephen'
# set the initial value of a and b
print("%s%s%s" % ("="*4, "start", "="*4))
a = 3
b = 2
print("a=%s" % a)
print("b=%s" % b)

# start calculate
print("%s%s%s" % ("="*4, "calculate", "="*4))

# a+b
print("a+b=%s" % (a+b))

# a-b
print("a-b=%s" % (a-b))

# a*b
print("a*b=%s" % (a*b))

# a/b
print("a/b=%s" % (a/b))

# a%b
print("a%%b=%s" % (a % b))

# a**b
print("a**b=%s" % (a**b))

# end of the test
print("%s%s%s" % ("="*4, "end", "="*4))

  執行結果:

====start====
a=3
b=2
====calculate====
a+b=5
a-b=1
a*b=6
a/b=1
a%b=1
a**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 1
num_compare(3, 2)
# Round 2
num_compare(2, 3)
# Round 3
num_compare(2, 2)

  執行結果

====start====
a=3
b=2
==========compare==========
a is NOT equal to b
a is NOT equal to b
a is NOT equal to b
a is NOT smaller than b
a is larger than b
====================end====================
====start====
a=2
b=3
==========compare==========
a is NOT equal to b
a is NOT equal to b
a is NOT equal to b
a smaller than b
a is NOT larger than b
====================end====================
====start====
a=2
b=2
==========compare==========
a is equal to b
a is equal to b
a is equal to b
a is NOT smaller than b
a 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 + b
print("c1 = a + b ====> %s" % c1)

# 加法運算子
set_initial_value()
c1 += a
c2 = c2 +a
print("c1 += a ====> %s" % c1)
print("c2 = c2 +a ====> %s" % c2)

# 減法運算子
set_initial_value()
c1 -= a
c2 = c2 -a
print("c1 -= a ====> %s" % c1)
print("c2 = c2 -a ====> %s" % c2)

# 乘法運算子
set_initial_value()
c1 *= a
c2 = c2 * a
print("c1 *= a ====> %s" % c1)
print("c2 = c2 * a ====> %s" % c2)


# 除法運算子
set_initial_value()
c1 /= a
c2 = c2 / a
print("c1 /= a ====> %s" % c1)
print("c2 = c2 / a ====> %s" % c2)

# 取模運算子
set_initial_value()
c1 %= a
c2 = c2 % a
print("c1 %%= a ====> %s" % c1)
print("c2 = c2 %% a ====> %s" % c2)

# 冪賦值運算子
set_initial_value()
c1 **= a
c2 = c2 ** a
print("c1 **= a ====> %s" % c1)
print("c2 = c2 ** a ====> %s" % c2)

# 取整除運算子
set_initial_value()
c1 //= a
c2 = c2 // a
print("c1 //= a ====> %s" % c1)
print("c2 = c2 // a ====> %s" % c2)

  執行結果:

====initial value start====
a=3
b=2
c1=10
c2=10
c1 = a + b ====> 5
====initial value start====
a=3
b=2
c1=10
c2=10
c1 += a ====> 13
c2 = c2 +a ====> 13
====initial value start====
a=3
b=2
c1=10
c2=10
c1 -= a ====> 7
c2 = c2 -a ====> 7
====initial value start====
a=3
b=2
c1=10
c2=10
c1 *= a ====> 30
c2 = c2 * a ====> 30
====initial value start====
a=3
b=2
c1=10
c2=10
c1 /= a ====> 3
c2 = c2 / a ====> 3
====initial value start====
a=3
b=2
c1=10
c2=10
c1 %= a ====> 1
c2 = c2 % a ====> 1
====initial value start====
a=3
b=2
c1=10
c2=10
c1 **= a ====> 1000
c2 = c2 ** a ====> 1000
====initial value start====
a=3
b=2
c1=10
c2=10
c1 //= a ====> 3
c2 = c2 // a ====> 3

  

邏輯運算

 Example:LogicalOperator.py

#!/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Stephen'
# Definition
a = True
b = False
c = True
d = False

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


# Test
print("%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 = 2
y = 2
z = 3
print("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 = 2
y = 2
z = 3
x is y
id(x) ====> 5045844
id(y) ====> 5045844
x is NOT z
id(x) ====> 5045844
id(z) ====> 5045832
----------string test----------
a = hello
b = hello
c = world
a is b
id(a) ====> 36823552
id(b) ====> 36823552
a is NOT c
id(a) ====> 36823552
id(c) ====> 36823616

  

位運算

Example:BitwiseOperator.py

#!/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Stephen'
# set the initial value of a and b
print("%s%s%s" % ("="*4, "start", "="*4))
a = 254            # 254 = 1111 1110
b = 192            # 192 = 1100 0000
c = 0
print("a=%s,binary:%s" % (a, bin(a)))
print("b=%s,binary:%s" % (b, bin(b)))

# Test
print("%s%s%s" % ("-"*20, "Test Start", "-"*20))

c = a & b
print("c = a & b ====> %s , binary: %s" % (c, bin(c)))

c = a | b
print("c = a | b ====> %s , binary: %s" % (c, bin(c)))

c = a ^ b
print("c = a ^ b ====> %s , binary: %s" % (c, bin(c)))

c = ~a
print("c = ~a ====> %s , binary: %s" % (c, bin(c)))

c = a << 2
print("c = a << 2 ====> %s , binary: %s" % (c, bin(c)))

c = a >> 2
print("c = a >> 2 ====> %s , binary: %s" % (c, bin(c)))

print("%s%s%s" % ("-"*20, "Test End", "-"*20))

  執行結果:

====start====
a=254,binary:0b11111110
b=192,binary:0b11000000
--------------------Test Start--------------------
c = a & b ====> 192 , binary: 0b11000000
c = a | b ====> 254 , binary: 0b11111110
c = a ^ b ====> 62 , binary: 0b111110
c = ~a ====> -255 , binary: -0b11111111
c = a << 2 ====> 1016 , binary: 0b1111111000
c = a >> 2 ====> 63 , binary: 0b111111
--------------------Test End--------------------

  

運算子優先順序

Example: ArithmeticOperatorPriority.py

#!/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Stephen'


a = 12
b = 3
c = 4
d = 2
res = 0

res = (a + b) * c / d
print("(%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 / d)
print("(%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))

  執行結果:

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

  

相關文章