python——基础练习(二)

简介: python——基础练习(二)

本文转载:https://xiaochuhe.blog.csdn.net/article/details/122417475
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)

输出结果:图片.png

第二部分:

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所对应的字符

输出结果:
图片.png

相关文章
|
5天前
|
Python
Python 练习实例26
Python 练习实例26
|
5天前
|
Python
Python 练习实例25
Python 练习实例25
|
1天前
|
Python
Python 练习实例35
Python 练习实例35
|
1天前
|
Python
Python 练习实例34
Python 练习实例34
|
1天前
|
Python
Python 练习实例36
Python 练习实例36
|
2天前
|
Python
Python 练习实例33
Python 练习实例33
|
3天前
|
Python
Python 练习实例30
Python 练习实例30
|
3天前
|
Python
Python 练习实例29
Python 练习实例29
|
6天前
|
Python
Python 练习实例19
Python 练习实例19
|
5天前
|
Python
Python 练习实例27
Python 练习实例27