"Turn" Python Learning (5)--Data type

Source: Internet
Author: User
Tags ord pow

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

>>> abs(-1)1

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.

>>> 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 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.03.13.143.1423.14163.141593.1415933.14159273.141592653.141592654

Iv. functions for integers only

Oct ():octonary number system octal string form

>>> oct(255)‘0377‘

Hex (): Hexadecimal number system hex string form

>>> hex(255)‘0xff‘

ASCII Code conversion function

Ord (): Ordinal: Ordinal, converting character to corresponding integer value

>>> ord(‘A‘)65

Chr (): Char: Single character, numeric pair should be an ASCII character

>>> chr(65)‘A‘

Five, operator

>>> x>=80 and x<=100True>>> 80<=x<=100True-----------------------------总是写错:>>> 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

Related Article

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.