Python基础(下)

 

第二周知识点总结

In [30]:
from random import choice
from random import randrange
from random import seed
from random import random
 

1、随机选择

(1)choice

In [4]:
# choice

doors = ['A', 'B', 'C']
# Randomly returns one of 'A', 'B' or 'C'
choice(doors)
Out[4]:
'A'
 

(2)randange

In [12]:
#rand

a=randrange(4)
print(list(range(4)))
print(a)
 
[0, 1, 2, 3]
3
In [28]:
# 固定每次随机的数   seed()

seed(100)   #如果给了seed值,后面的randrange每次运行选择的结果是一样的
a=randrange(20)
print(a)
 
4
In [42]:
# 用法示例

doors = ['A', 'B', 'C']
# 3 possible outcomes
first_chosen_door = doors.pop(randrange(3))
first_chosen_door, doors
Out[42]:
('B', ['A', 'C'])
 

(3)random

In [33]:
# random() 为0-1之间随机的数
a=random()
print(a)
 
0.17474750209982115
 

(4)randint

左闭右闭区间

In [ ]:
 
 

2、输入语句判断

In [34]:
while True:
    try:
        nb_of_games = int(input('How many games should I simulate? '))
        if nb_of_games <= 0:
            raise ValueError
        print('Ok, will do!')
        break
    except ValueError:
        print('Your input is incorrect, try again.')
 
How many games should I simulate? 12
Ok, will do!
In [35]:
while True:
    try:
        nb_of_games = int(input('How many games should I simulate? '))
        print('Ok, will do!')
        break
    except ValueError:
        print('Your input is incorrect, try again.')
 
How many games should I simulate? 12
Ok, will do!
In [43]:
# 示例

def set_simulation():
    while True:
        try:
            nb_of_games =\
                    int(input('How many games should I simulate? '))
            if nb_of_games <= 0:
                raise ValueError
            break
        except ValueError:
            print('Your input is incorrect, try again.')
    while True:
        contestant_switches = input('Should the contestant switch? ')
        if contestant_switches.istitle():
            contestant_switches = contestant_switches.lower()
        if contestant_switches in {'yes', 'y'}:
            contestant_switches = True
            print('I keep in mind you want to switch.')
            break
        if contestant_switches.lower() in {'no', 'n'}:
            contestant_switches = False
            print("I keep in mind you don't want to switch.")
            break
        print('Your input is incorrect, try again.')
    return nb_of_games, contestant_switches

set_simulation()
 
How many games should I simulate? 23
Should the contestant switch? 23
Your input is incorrect, try again.
Should the contestant switch? 23
Your input is incorrect, try again.
Should the contestant switch? y
I keep in mind you want to switch.
Out[43]:
(23, True)
 

3、一些对str的操作

In [37]:
# 是否以大写字母开头。   .istitle()

'Y'.istitle()
'Yes'.istitle()
'Yes_'.istitle()
'Yes_12_'.istitle()
'yes'.istitle()
'yEs'.istitle()
'Yes, by all means, yes!'.istitle()
'Yes, By All Means, Yes!'.istitle()
Out[37]:
True
In [41]:
# 把所有大写字母转为小写。    .lower()

'Yes, BY all means, yes!'.lower()
Out[41]:
'yes, by all means, yes!'
 

4、格式化输出

 

(1)f

In [49]:
x = 10
u = 4.5
v = 10
print(f'x is equal to {x}.'
      'That is not all: '
      f'{u} divided by {v} equals {u / v}.'
     )
 
x is equal to {x}. That is not all: 4.5 divided by 10 equals 0.45.
In [51]:
x = 123 / 321 
print(f'{x}')
print(f'{x:.0f}')
print(f'{x:.1f}')
print(f'{x:.2f}')
print(f'{x:.3f}')
print(f'{x:.4f}')
print(f'{x:.30f}')
 
0.38317757009345793
0
0.4
0.38
0.383
0.3832
0.383177570093457930955338497370
 

5、函数

(1)函数赋值

In [45]:
# x and y are parameters without default values,
# u and v are parameters without default values
def f(x, y, u=10, v=20):
    print(x, y, u, v)

# 2 positional arguments
f(1, 2)     #若未给u和v赋值,则默认为u=10, v=20
# 3 positional arguments
f(1, 2, 4)
# 4 positional arguments
f(1, 2, 3, 4)
# 1 positional argument, 2 keyword arguments
f(1, v=4, y=2)
# 3 keyword arguments
f(u=3, y=2, x=1)
 
1 2 10 20
1 2 4 20
1 2 3 4
1 2 10 4
1 2 3 20
 

(2)函数应用

In [46]:
# 函数使用举例:

#超过给定数量隐藏

def simulate(nb_of_games_to_display=3):
    for i in range(nb_of_games):
        if i < nb_of_games_to_display:
            print('Display all details of game number', i)
        elif i == nb_of_games_to_display:
            print('...')
            
nb_of_games = 2
simulate()
print()

nb_of_games = 3
simulate()
print()

nb_of_games = 4
simulate()
 
Display all details of game number 0
Display all details of game number 1

Display all details of game number 0
Display all details of game number 1
Display all details of game number 2

Display all details of game number 0
Display all details of game number 1
Display all details of game number 2
...
In [ ]:
 
posted @ 2021-09-02 23:38  企小鹅同学、  阅读(66)  评论(0)    收藏  举报