赞
踩
2022年1月10日python代码练习如下:
第一部分:
- from colorama import Fore,init #从colorama模块中导入Fore,init对象
- import random #导入random模块
- print (random.randint(1,10)) #随机打印1跟10之间的一个整数
- random.seed(2) #定格2次打印
- print (random.randint(1,5))
- print (random.randint(1,5))
- print (random.randint(1,5))
- result = 1000
- print (f'money={result}') #格式化字符串
- #嵌套函数
- def f(x):
- print (f'输出的x值={x}')
-
- def h():
- result = x ** 2
- print (f'最终返回的结果是{result}')
- return h
-
- fh = f(3)
- fh()
- #参数函数
- def f(x,y):
- print ('开始执行函数y')
- consult = y(x)
- print ('返回的结果是{}'.format(consult))
- if __name__ == '__main__':
- y = lambda x: x ** 2
- f(4,y)

输出结果:
第二部分:
- from colorama import Fore,init #从colorama模块中导入Fore,init对象
- #全局变量
- a = 1
- def local():
- a = 2
- print ('函数local的a为%d'%a)
- local()
- #局部变量
- a = 1
- def chuhe():
- global a
- print (f"chuhe函数里面的a为{a}")
- chuhe()
- print (init(autoreset=True))#输出完有色字体后自动恢复默认颜色
- print (Fore.BLUE + '\n\n\t 杀\t世\t子,夺\t青\t鸟!')
- print (Fore.YELLOW + '\n\n\t 杀\t世\t子,夺\t青\t鸟!'.strip())#字符串去除空格
- a,b = 4,3
- print (a//b)#向下取整
- a,b = 9,2
- print (a%b)#计算余数
- print (abs(-9))#求绝对值
- print (hex(11))#十进制转十六进制
- print (int('1100',base=2))#二进制转十进制
- print (ord('a'))#字符a对应的ascii码
- print (chr(97))#ascii码97所对应的字符

输出结果:
今日份运动截图:
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。