Data Type Classic
We can classify these type to follow as =>
(1) Integer and floating-point objects : 1234, 1.23, 14e-10
sign : e or E is exponent. Python 3.x only has integer (normal and long have
been merged) but Python 2.x , it still are two integer types and normal.
(2) Complex number objects : 3+4j, j
(3) Decimal Obejct : Decimal(‘2.1456’)
(4) Fraction : Fraction(-8,5)
(5) Sets : set(“apple”)
(6) Boolean Object :Ture or False, Bool , 1
(7) Numeric module(build-in libraries) : random, math
(8) Third-party extension : vector, plotting
| Operator | Example |
|---|---|
| express operators | +, -, *, /, >>, **, &, >>> 1+1 2 |
| Built-in mathematical functions | pow, abs, round …. |
| module | random, math, |
Expression operators
| Operators | Description |
|---|---|
| or | Logical OR => x or y |
| and | Logical AND => x and y |
| x<y, x<=y, x>y, x>=y | |
| x==y, x!=y | Logical equal , not equal |
| x | y | Bitwise OR |
| x ^ y | Bitwise XOR |
| x <<y , x >>y | Shift x left or right by y bits |
| +, -, *, /, % | |
| // | floor |
| ~x | Bitwise Not |
| x[i], x[i:j] | retrive sub slicing |
| [] | list |
| {} | dictionary |
| () | tuple |
| x in y, y not in x | membership |
| foor |
Integer and float
In Python 2.0, Integer is represented by “L” , but it is none in python 3.0.
*Python 2.0
>>>3 ** 200
265613988875874769338781322035779626829233452653394495974574961739092490901302182994384699044001L
*Python 3.0
>>>3 ** 200
265613988875874769338781322035779626829233452653394495974574961739092490901302182994384699044001
Complex Numbers
Complex Numbers divide into real number and imaginary, J or j suffix to the imaginary part.
| >>> (1+1j) * 1j (-1+1j) >>> 1+1j*3 (1+3j) >>> j * j -1 |
Fraction Type
(1) 1/2 is represented to Fraction( 1 , 2 )
(2) accurate , Ex: 0.1 + 0.1 + 0.1 -0.3 and Fraction(1, 10) + Fraction(1, 10) + Fraction(1, 10)
- Fraction(3, 10)
| >>> from fractions import Fraction >>> Fraction(1,10) + Fraction(1,10) Fraction(1, 5) >>> Fraction(2,5) - Fraction(1,5) >>> Fraction(2,5) * Fraction(1,5) >>> Fraction("0.25") >>> Fraction("0.3") + Fraction("0.1") >>> 0.1 + 0.1 + 0.1 -0.3 # The result should be 0 |
Sets
(1) It is not unordered and non-duplicated a of collection items.
(2) It is used to “ {}” to represent element of the sets
| >>> food = {'apples', 'banana'} >>> food set(['banana', 'apples']) >>> type(food) <type 'set'> |
set operator
x = set("apple") , y = set("banana")
| Operators | Description | Example |
|---|---|---|
| - | different | >>> x – y set(['p', 'e', 'l']) |
| | or Union | Union | >>> x | y set(['a', 'p', 'b', 'e', 'l', 'n']) >>> x.union(y) set(['a', 'p', 'b', 'e', 'l', 'n']) |
| & | Intersection | >>> x & y |
| ^ | Symmetric different(XOR) | >>> x ^ y |
| < , > | superset, subset | >>> x > set(“a”) |
| in | include | >>> “a” in x True |
| add(Object) | insert into one to the set | >>> x.add("apple") |
| update(Obejct) | update set the set | >>> x.update("a","b") |
| remove(Objcet) | remove | >>> x.remove('a') >>> x set(['p', 'e', 'l']) |
Note : {} is still a dictionary in all Python.
| >>> type({}) # don’t {} and space element <class 'dict'> >>> s = set() >>> type(s) <class, ‘set’> |
Advanced
| >>> { x*4 for x in 'apple'} set(['aaaa', 'llll', 'pppp', 'eeee']) |
Boolean
(1) Boolean is the same as 1 , Ex: True = 1
| >>> type(Ture) <class, ‘bool’> |
留言
張貼留言