Python Learning--math-related module functions and the use of random number modules

Source: Internet
Author: User
Tags natural logarithm pow random shuffle shuffle

Math-related function usage

Import Math Module--import Math

1.floor () rounding down print (Math.flooor (6.1))

2.ceil () Up-Rounding print (Math.ceil (6.1))

3.round () rounding (odd if even, for system built-in functions) print (Round (5.8)) print (round (6.8))

4.pow () calculates a number of n-th-square print (Math.pow (5,3))

5.SQRT () Open square operation print (MATH.SQRT (9))

6.fabs () calculates the absolute value of a number and returns a floating-point type PRITN (Math.fabs (-6))

7,abs () calculates the absolute value, which is the system built-in function, which is the same as the original data type print (ABS (6))

8.MODF () splits a number into integers and decimals, returning a value of tuple print (MATH.MODF (5.5))

9.copysign () assigns the symbol of the second number to the first number print (Math.copysign (3,-3.5))

10.fsum () calculates the and (return value of floating-point) print (Math.fsum ([1,2,3,4,5,6]) of numbers in a sequence

11.sum () calculates the sum of numbers in a sequence (for the system built-in function, the return value is determined by the original value) print (SUM ([1,2,3,4,5,6]))

The values commonly used in 12.math modules

12.1 (PI) print (Math.PI)

12.2 E (natural logarithm) print (MATH.E)


 Import Random Number Module

Import Random

1.random () randomly gets the decimal between 0~1 (contains 0 does not contain 1)

Print (Random.random ())

2.choice () randomly returns a value from a sequence

Print (Random.choice ([1,2,3,4,5,6,7,8]))

3.shuffle () Random Shuffle list

Print (Random.shuffie ([1,2,3,4, ' A ', ' B '))

4.randrange () Gets the integer in the specified range

Print (Random.randrange (2,14,2))

5.uniform () Gets the random number of the specified range

Print (Random.uniform (2,14))

Python Learning--math-related module functions and the use of random number modules

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