PYTHON practical operation - routine use example 1

1. Conversion between bases

def fun():
    num=int(input('请输入一个十进制的整数:')) #将str类型转换成int类型
    print(num,'的二进制数为:',bin(num))  #第一种写法,使用了个数可变的位置参数
    print(str(num)+'的二进制数为:'+bin(num))  #第二种写法,使用了“+”作为连接符(+的左右均为str类型)
    print('%s的二进制数为:%s' % (num,bin(num))) #第三种写法,格式化字符串
    print('{0}的二进制数为:{1}'.format(num,bin(num)))#第三种写法,格式化字符串
    print(f'{num}的二进制数为:{bin(num)}')#第三种写法,格式化字符串
    print('---------------------------------------------')
    print(f'{num}的八进制数为:{oct(num)}')
    print(f'{num}的十六进制数为:{hex(num)}')

2. Add text color to the output under the shell

print('\033[40;37m\t\tHello world\033[0m')
print('\033[41;37m\t\tHello world\033[0m')
print('\033[42;37m\t\tHello world\033[0m')
print('\033[43;37m\t\tHello world\033[0m')
print('\033[44;37m\t\tHello world\033[0m')
print('\033[45;37m\t\tHello world\033[0m')
print('\033[46;37m\t\tHello world\033[0m')
print('\033[47;37m\t\tHello world\033[0m')

Three, the use of several variables

name1='aa'
name2='bb'
name3='cc'
name4='dd'
name5='ee'

####path1#############
print ('❶\t',name1)
print ('❷\t',name2)
print ('❸\t',name3)
print ('❹\t',name4)
print ('❺\t',name5)

####path2,列表#############
lst_nam=['aa','bb','cc','dd','ee']
lst_sig=['❶','❷','❸','❹','❺']
for i in range(5):
     print(lst_sig[i],'\t',lst_nam[i])

####path3,字典#############
d={'❶':'aa','❷':'bb','❸':'cc','❹':'dd','❺':'ee'}
for key in d:
    print(key,'\t',d[key])

####path4,zip#############
for s,name in zip(lst_sig,lst_nam):
    print(s,'\t',name)

4. General operations

height=170
weight=50.5
bmi=weight/(height+weight)
print('您的身高是:'+str(height))
print('您的体重是:'+str(weight))
print('您的BMI指数是:'+str(bmi))
print('您的BMI指数是:'+'{:0.2f}'.format(bmi))
print('用户手机账户原有话费金额为:8元')
money=int(input('请输入用户充值金额:'))
money+=8
print('当前的余额为:',money,'')
father_height=float(input('请输入父亲的身高:'))
mother_height=float(input('请输入母亲的身高:'))
son_height=(father_height+mother_height)*0.54
print('预测子女的身高为:{}cm'.format(son_height))

Guess you like

Origin blog.csdn.net/vincent0920/article/details/129274271