Python基础(下)
第二周知识点总结¶
In [30]:
from random import choice
from random import randrange
from random import seed
from random import random
In [4]:
# choice
doors = ['A', 'B', 'C']
# Randomly returns one of 'A', 'B' or 'C'
choice(doors)
Out[4]:
(2)randange¶
In [12]:
#rand
a=randrange(4)
print(list(range(4)))
print(a)
In [28]:
# 固定每次随机的数 seed()
seed(100) #如果给了seed值,后面的randrange每次运行选择的结果是一样的
a=randrange(20)
print(a)
In [42]:
# 用法示例
doors = ['A', 'B', 'C']
# 3 possible outcomes
first_chosen_door = doors.pop(randrange(3))
first_chosen_door, doors
Out[42]:
(3)random¶
In [33]:
# random() 为0-1之间随机的数
a=random()
print(a)
(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.')
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.')
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()
Out[43]:
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]:
In [41]:
# 把所有大写字母转为小写。 .lower()
'Yes, BY all means, yes!'.lower()
Out[41]:
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}.'
)
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}')
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)
(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()
In [ ]:

浙公网安备 33010602011771号