Python getting started note (6): About the Value Type

Source: Internet
Author: User

I. Standard Functions

Cmp (): Compare the size

Str (): Convert to string

Type (): type

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 factory Functions

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)

Iii. Functions

Used for numerical calculation: asb (), coerce (), divmod (), pow (), round ()

Asb (): absolute; complete; authoritarian; n: absolute

>>> abs(-1)1

Coerce (): vt. Force, force,
Type conversion, but provides custom two numeric types instead of relying on the python interpreter. A parent is returned, and a forced action exists.
Coerce (...)
Coerce (x, y)-> (x1, y1)

Return a tuple consisting of the two numeric arguments converted
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 division remainder, returns the ancestor containing the quotient and remainder

>>> divmod(10,3)(3, 1)>>> divmod(3,10)(0, 3)>>> divmod(10,2.5)(4.0, 0.0)

Pow (): power of a number: exponential meaning

Both pow () and ** can implement exponential operations. pow () is born first.

>>> pow(2,5)32>>> 2**532

Round (): Rounding

Round (...)
Round (number [, ndigits])-> floating point number

Round a number to a given precision in decimal digits (default 0 digits ).
This always returns a floating point number. Precision may be negative.

>>> round(1.234,2)1.23>>> round(3.14)3.0>>> for each in range(10):     print round(math.pi,each)3.03.13.143.1423.14163.141593.1415933.14159273.141592653.141592654

4. Only integer Functions

Oct (): octonary number system octal string format

>>> oct(255)'0377'

Hex (): hexadecimal number system hexadecimal string format

>>> hex(255)'0xff'

ASCII Code Conversion Function

Ord (): ordinal number, which converts a character to an integer.

>>> ord('A')65

Chr (): char: a single character. The number pair should be ASCII characters.

>>> chr(65)'A'

5. Operators

>>> X> = 80 and x <= 100 True> 80 <= x <= 100True ----------------------------- always writes an error: >>> 80 = <x <= 100 SyntaxError: invalid syntax

Vi. Usage

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 outif __name__=="__main__":    sorce = input('Enter your sorce:')    print 'level:%s' %result(sorce)    

 

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.