- The Control Structure of Programs
Branch & Loop Structure
Compact type:
<
c
o
m
m
a
n
d
1
>
<command1>
<command1> if
<
c
o
n
d
i
t
i
o
n
>
<condition>
<condition> else
<
c
o
m
m
a
n
d
2
>
<command2>
<command2>, means if condition is judged true, do the command1, and if condition is judged false, do the command2.
Exception Handling: try/except combination, ATTENTION: try/except is different from if/else in Python, it’s NameError.
After the exception type is annotated, it only responds to THIS exception, and the name of the exception is same as the variable.
The high-level of using Exception Handling:
try:
<
c
o
m
m
a
n
d
1
>
<command1>
<command1>
except:
<
c
o
m
m
a
n
d
2
>
<command2>
<command2>
else:
<
c
o
m
m
a
n
d
3
>
<command3>
<command3>
finally:
<
c
o
m
m
a
n
d
4
>
<command4>
<command4>
The commands in Finally Struct must be executed, the commands in Else Struct will be executed when exception do not happen.
- The overview of Random Library
Basic Random Function:
seed(a= None):Initialize the given-random number seed, default value is the time of system. e.g: random.seed(10) produce the sequence of the seed 10.
random(): generate a random decimal from 0.0 to 1.0.
WARNING: Using seed can get a accurate number of time, so we can playback the experiment we have done before, if don’t use the seed, the default value–system number–is a value accurate to the millisecond and it’s very difficult to recurrent.
randint(a, b): generate a random integer from a to b.
randrange(m, n, [k]): generate a random integer from m to n according to the step k, parameter k is alternate.
getrandbits(k): generate a random integer which is k bits long.
uniform(a, b): generate a random decimal from a to b, can reach the 16byte precision.
choice(seq): choose a element randomly from the sequence seq.
shuffle(seq): sort the elements in the sequence seq randomly, return the chaos-shuffled sequence.