[2.13] Python math functions study notes and random function

When I die, I see lesson today read head, forgot to send the job, was criticized Noriaki
and chemical operations or messing around directly on the hair
I feel like I've died early, and now is not death, the school will

function

Very easy, there is a lot of overlap between the language of section

Note
the use of certain mathematical functions Previously, to use this statement

import math

Before using a random number, get another call

import random

\ (import \) role is to call a library
such as before

import keyword

And \ (C ++ \) in the

#include<bits/stdc++.h>

similar

And when using the function library, you need to add "name of the library."

For example, the square root function needs to be written

math.sqrt()

Without \ (math. \) Will complain

Library:
package features some of
the same library function similar to the function

Absolute value function

\ (abs \) function, when engaged in competition is not very popular (in fact, I was too dishes

print( abs( -19260817 ) ) #顺带一提,19260817是一个质数

This function is not \ (math \) library, which is a built-in function

bigger than smaller than

Return true 1, false 0 is returned
without calling \ (math \) library

\ (max, min \) function

\ (max \) literal meaning, the given parameter returns a larger value
, however, and \ (C ++ \) is not the same, can compare multiple

a1 = 1
a2 = 2
a3 = 3
a4 = 4444  #我又来迫害米4达了
print( max( a1, a2, a3, a4 ) )

The return value is 4444

\ (min \) Similarly

Instead of calling \ (math \)

power

To calculate \ (2 ^ 5 \)

print( pow( 2, 5 ) )

This interesting, pow takes two

The first can be used directly, integer arithmetic

print( pow( 2, 5 ) )

The second can not be directly used to invoke \ (math \) library
will retain decimal places

print( math.pow( 2, 5 ) )

Rounding function

Can the provisions of digits, just add a parameter on the line, but without the default, then, is reserved bit integer

print( round( 3.14159 ) ) # 输出3
print( round( 3.14159, 2 ) ) # 输出3.14

Note that this function is not \ (math \) library

Tuple function

What is it, that forms part of the integer part plus float tuple

print( math.modf( 22.7 ) )

Returns the integer portion of the floating-point and floating-point numbers are part
although there will be Kichiku accuracy error ......

Prescribing function

I wrote the above

print( math.sqrt( 16 ) )

Return is still float

Random number function

There are a lot of random number function
Be sure to call before using \ (random \) library

\ (choice \) function

From the elements of a sequence in a randomly selected
can also pick a string, you can also mix and match

print( random.choice( [ 1, 3, 5, 7, 9, "屁桃" ] ) )
print( random.choice( range( 5 ) ) )  #range(5) == [ 0, 1, 2, 3, 4 ]

However, note that if write it like this, then the string will not be seen as an element, but each letter as an element

print( random.choice( "aCutePig" ) )

\ (randrange \) function

From the specified range, the radix number is incremented selected from

print( random.randrange( 1, 100, 5 ) )

From \ (1 \) to \ (99 \) , select \ (1 + 5n \) number, for example, \ (6,11,16,21 \)

print( random.randrange( 1, 100, 2 ) )

Select \ (1,3,5,7 \) , etc.

Start of range and base can not write, began to default is 0, the default is 1 base

\ (random \) function

Less than a randomly generated \ (1 \) greater than or equal \ (0 \) floating point

print( random.random() )

\ (shuffle \) function

Upset (easy to understand

list = [ 1, 2, 3, 4, 5 ] #生成一个列表
random.shuffle( list ) #打乱列表元素顺序

\ (uniform \) function

Generating a section closed arbitrary real number (integer or floating point within
, of course, and compared endless float, integer number seems too poor
I \ (1 \) to \ (100 \) between the generated \ ( 100 \) number not an integer (character issues do not rule out

print( random.uniform( 1, 100 ) )

Guess you like

Origin www.cnblogs.com/with6676/p/12306049.html