python——基础练习(四)

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

本文转载:https://xiaochuhe.blog.csdn.net/article/details/122489981
2022年1月14日python代码练习如下:

#创建属性
class chuhe():
    def __init__(self,name):   #创建一个对外公开的属性
        self.name = name        #创建一个仅内部使用的属性
        self.compare = "最美!"
 
    def run(self):
        print (f"{self.name},{self.compare}")
xch = chuhe("青鸟")
print (xch.name)
xch.run()
 
 
class chuhe():
    begin = 2000
 
    #def __init__(self):
        #self.name = name
    @classmethod                 #创建类方法
    def true_age(start,birthday):
        return birthday-start.begin
print (chuhe.true_age(2021))
 
from functools import reduce   #reduce函数
print (reduce(lambda x,y:x+y,[1,6,8,5,4]))
 
a,b = [9,8,2],[4,5,1]
c = list(map(lambda x,y:f"{x}-{y}",a,b))
print (c)    #map函数 map(fun,*iterables)
 
'''
输入一个字符串,内容是带小数的实数,例如"123.45",输出是两个整数变量x和y,x是整数部分123,
 y是小数部分45。你可以用split函数完成。
'''
str = input(str("请输入一个带小数的字符串:"))
a = str.split(".")  #字符串拆分split函数可以将.符号消除,并用,号代替分割整数和小数
#print (a)
a = [int(i) for i in a] #将分割的字符串形式转化为数值
print (a)
print (a[0])
print (a[1])

输出结果:
图片.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