Python numeric types and techniques

Source: Internet
Author: User
Python numeric types and techniques

Numeric type in Python

Int
Float
Fractions. Fraction
Decimal. Decimal

Data integration

Int (f): removes the decimal part and only keeps the integer part. Therefore, the result of int (-3.8) is-3.
Math. trunc (f): Same as int (f)
Round (f, digits): returns the number of digits that are retained.
Math. floor (f)
Math. ceil (f)

Determine

Math. isinf ()
Math. isfinite ()
Math. isnan ()
Float. is_integer ()

How to calculate power

The following three Chinese laws indicate square construction
Math. sqrt (144)
144*0.5
Pow (144, 0.5)

Hexadecimal conversion

Int (s, base): The first parameter is a string that represents a number, and the second parameter is in hexadecimal format. Int ('20140901', 2) indicates converting the binary string '20160901' to an integer.
Oct, hex, bin: converts a number into a base string representation, so the result is str rather than a number.

0xfe, 0b111110, 0o376, and 254 are the same in Python, indicating the number 254. there is no difference between these representation methods for Python. '0xfe 'is just a string. if you want to convert it to an integer, you need to use the int function, int ('0xfe', 16 ).

Common Modules

Math

Used for some mathematical operations

Random

Used to generate random numbers.
This module provides many functions, which are particularly useful.
Random. random (): generates a random number between 0 and 1.
Random. randint (min, max): generate a random integer between [min, max)
Random. choice (iterable): returns an element randomly selected from an iteratable object.
Random. sample (iterable, k): randomly selects non-repeated k elements from the iterable and returns them in an array.
Random. randrange (start, stop, step): step by step in [start, stop) to randomly generate an element.
Random. shuffle (l): random order of the sequence, and None is returned. It must be noted that this works in the same place.

Decimal

If you need accurate results, you can use this module.
Decimal. Decimal (str): used to create a Decimal object.
Decimal. getcontext (). PRec = n: specifies the number of decimal places.

Fractions

If you need accurate results, you can use this module.
X = fractions. Fraction (1, 3)
Y = fractions. Fraction (0.25)
Z = fractions. Fraction (* (3.25.as _ integer_ratio ()))

The above is the content of the Python numeric type and its skills. For more articles, please follow the PHP Chinese network (www.php1.cn )!

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.