当前位置:   article > 正文

python——基础练习(二)_python用函数输出f(x)=y

python用函数输出f(x)=y

2022年1月10日python代码练习如下:

第一部分:

  1. from colorama import Fore,init #从colorama模块中导入Fore,init对象
  2. import random #导入random模块
  3. print (random.randint(1,10)) #随机打印1跟10之间的一个整数
  4. random.seed(2) #定格2次打印
  5. print (random.randint(1,5))
  6. print (random.randint(1,5))
  7. print (random.randint(1,5))
  8. result = 1000
  9. print (f'money={result}') #格式化字符串
  10. #嵌套函数
  11. def f(x):
  12. print (f'输出的x值={x}')
  13. def h():
  14. result = x ** 2
  15. print (f'最终返回的结果是{result}')
  16. return h
  17. fh = f(3)
  18. fh()
  19. #参数函数
  20. def f(x,y):
  21. print ('开始执行函数y')
  22. consult = y(x)
  23. print ('返回的结果是{}'.format(consult))
  24. if __name__ == '__main__':
  25. y = lambda x: x ** 2
  26. f(4,y)

输出结果:


 第二部分:

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

输出结果:

今日份运动截图:

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/901193
推荐阅读
相关标签
  

闽ICP备14008679号