当前位置:网站首页>随机数,函数
随机数,函数
2022-07-31 05:18:00 【m0_59138290】
随机数
- 随机一个1-10之间的小数
- 随机一个99-999之间的整数
- 从列表出随机一个元素
- 从列表中随机两个元素
import random
print(random.uniform(0, 10))
print(random.randint(99, 999))
print(random.choice([1, 2, 3, 4]))
print(random.sample([1, 2, 3, 4], k=2))
函数
定义一个函数,且有一个参数
函数功能:取得传入字符串的第一个字符和最后一个字符
并返回def fun_exer(data_str): print(data_str[0], data_str[-1]) fun_exer("function_exercise")
定义一个函数:可以接收一个参数或多个位置参数,参数的个数不定
传入多个参数:包含整型,浮点型,
功能:计算所有参数的和, 然后返回def fun_exer1(*args): list_data = list(args) sum = 0 for i in list_data: sum = i + sum print(sum) fun_exer1(2, 3.5, 18)
定义一个函数:可以接收一个或多个关键字参数,参数的个数不定
传入多个参数
功能:输出所有关键字参数的key和value
def fun_exer2(**kwargs):
print(kwargs.keys())
print(kwargs.values())
fun_exer2(a=1, b=2, c=3)
- 定义一个函数,2个位置参数,2个关键字参数
功能:打印所有参数
a.全部以关键字参数的形式传参
def function(arg1, arg2, kwarg1, kwarg2):
print(arg1, arg2, kwarg1, kwarg2)
function(arg1=1, arg2=2, kwarg1=3, kwarg2=4)
b.全部以位置参数的形式传参
def function(arg1, arg2, kwarg1, kwarg2):
print(arg1, arg2, kwarg1, kwarg2)
function(1, 2, 3, 4)
c.以位置参数和关键字混合的形式传参
def function(arg1, arg2, kwarg1, kwarg2):
print(arg1, arg2, kwarg1, kwarg2)
function(1, 2, kwarg1=3, kwarg2=4)
d.带默认值的参数不传,只传位置参数
def function(arg1, arg2, kwarg1=3, kwarg2=4):
print(arg1, arg2, kwarg1, kwarg2)
function(1, 2)
定义一个函数:总共有四个参数:
前两个参数,只能已位置参数传入
后两个参数,只能已关键字参数传入def fun_exer3(pos1, pos2, /, *, kwd1, kwd2): print(pos1, pos2, kwd1, kwd2) fun_exer3(1, 2, kwd1=3, kwd2=4)
边栏推荐
猜你喜欢
Chemical Reagent Phospholipid-Polyethylene Glycol-Amino, DSPE-PEG-amine, CAS: 474922-26-4
国际站卖家大促攻略,只需要做好这几件事
Remote file xxx is mapped to the local path xxx and can't be found. You can continue debugging....
MW: 3400 4-Arm PEG-DSPE four-arm-polyethylene glycol-phospholipid a saturated 18-carbon phospholipid
超详细!!!让你了解冒泡排序的底层逻辑和思想
Pytorch learning notes 09 - multiple classification problem
UE5 最新动态虚幻引擎全新版本引爆互联网
Incredibuild 宣布支持 Yocto
Wangeditor rich text editor to upload pictures and solve cross-domain problems
IDEA overview and installation and debugging
随机推荐
Learning and understanding of ROS service programming
Remote file xxx is mapped to the local path xxx and can't be found. You can continue debugging....
Remote file xxx is mapped to the local path xxx and can‘t be found. You can continue debugging....
哈希表基础
顶级程序员都是怎么做的?
[已解决]ssh连接报:Bad owner or permissions on C:\\Users/XXX/.ssh/config
国际站卖家大促攻略,只需要做好这几件事
螺旋矩阵Ⅱ
【Rhapsody学习笔记】2:Count Down
钉钉企业内部-H5微应用开发
cenos7配置IP,配置IP不生效
浏览器中的画中画(Picture-in-Picture)API
DOM操作-通过关系来获取元素
DOM操作-案例:切换背景图片
mPEG-DMPE Methoxy-polyethylene glycol-bismyristyl phosphatidylethanolamine for stealth liposome formation
实现离线文件推流成rtsp 2
Unity Text一个简单的输入特效
nacos1.4.1创建配置报错
ES6-对象
力扣刷题.快乐数