Transfer from http://www.cnblogs.com/BeginMan/archive/2013/06/08/3125876.html
One, standard type function
CMP (): Compare size
STR (): Convert to String
Type (): Types
cmp (...) cmp (x, y) - > integer return negative (negative) if x<y, zero ( 0 if x = = y, positive (positive) if x>y. |
As follows:
>>> cmp ( 5 , 3.2 ) 1 >>> cmp ( 3.5 , 8 ) - 1 |
Ii. Conversion of factory functions
There is a loss of precision
>>>
int
(
1.847
)
1
>>>
long
(
42
)
42L
>>>
float
(
42
)
42.0
>>>
complex
(
42
)
(
42
+
0j
)
>>>
complex
(
2.4
,
-
8
)
(
2.3999999999999999
-
8j
)
|
Third, function function
For numeric operations: ASB (), coerce (), Divmod (), POW (), round ()
ASB (): Absolute: Absolute, complete, authoritarian, N: Absolute
coerce ():Vt. Force, Force,
Type conversions, but provides a custom two numeric type conversion instead of relying on a Python interpreter. Returns a meta-ancestor that has a mandatory behavior.
Coerce (...)
Coerce (x, y) (x1, y1)
Return a tuple consisting of the numeric arguments converted to
A common type, using the same rules as used by arithmetic operations.
If coercion is not possible, raise TypeError.
>>>
coerce
(
1
,
2
)
(
1
,
2
)
>>>
coerce
(
1.2
,
2l
)
(
1.2
,
2.0
)
>>>
coerce
(
1.2
,
2
)
(
1.2
,
2.0
)
>>>
coerce
(
1
,
2.3
)
(
1.0
,
2.2999999999999998
)
>>>
coerce
(
1j
,
123
)
(
1j
, (
123
+
0j
))
|
Divmod ():. Divmod divides the remainder, returns the Ganso of the containing quotient and the remainder
>>>
divmod
(
10
,
3
)
(
3
,
1
)
>>>
divmod
(
3
,
10
)
(
0
,
3
)
>>>
divmod
(
10
,
2.5
)
(
4.0
,
0.0
)
|
Pow ():power of a number: the meaning of the exponent
Both POW () and * * can achieve exponential operations, and POW () is born first.
Round (): Rounding
Round (...)
Round (number[, ndigits]), floating point number
Round a number to a given precision in decimal digits (default 0 digits).
This is always returns a floating point number. Precision may negative.
>>>
round
(
1.234
,
2
)
1.23
>>>
round
(
3.14
)
3.0
>>>
for each
in range
(
10
):
print round
(math.pi,each)
3.0
3.1
3.14
3.142
3.1416
3.14159
3.141593
3.1415927
3.14159265
3.141592654
|
Iv. functions for integers only
Oct ():octonary number system octal string form
Hex (): Hexadecimal number system hex string form
ASCII Code conversion function
Ord (): Ordinal: Ordinal, converting character to corresponding integer value
Chr (): Char: Single character, numeric pair should be an ASCII character
Five, operator
>>> x>
=
80 and x<
=
100
True
>>>
80
<
=
x<
=
100
True
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
总是写错:
>>>
80
=
|
Vi. Pragmatism
1. Score Level
def
result(x):
dic
=
{
9
:
‘A‘
,
8
:
‘B‘
,
7
:
‘C‘
,
6
:
‘D‘
}
myre
=
x
/
10
for
obj
in
sorted
(dic.keys(),reverse
=
True
):
if
myre>
=
obj:
out
=
dic[obj]
break
else
:
out
=
‘F‘
return
out
if
__name__
=
=
"__main__"
:
sorce
=
input
(
‘Enter your sorce:‘
)
print
‘level:%s‘
%
result(sorce)
"Turn" Python Learning (5)--Data type