ls = [{"name":"张三", "age":19}, {"name":"李四", "age":12}, {"name":"王五", "age":18}] def sort_by_age(item): return item["age"] # ls = sorted(ls, key=sort_by_age, reverse=True) ls = sorted(ls, key=lambda x:x['age'], reverse=True) print(ls) ## counts = {"when":400, "you": 20, "alone":100, "me":30} counts=sorted(counts.items(),key=lambda x:x[1]) print(counts)
# 字符串 name = " Python c++ Java " print(name) print(type(name)) # <class 'str'> # 是str类型就会有string类所具有的方法 # 1.strip 去两端空格 print(len(name)) name = name.strip() print(len(name)) print(name) # 2.split 分割 默认按照空格分割 li1 = name.split() li2 = name.split('+') print(li1) print(li2) # 3.replace 替换 price = '$9.9$' price = price.replace("$", "") print(float(price)) # 4. join 列表变成字符串 print("".join(['Python', 'c++', 'Java'])) print(" ".join(['Python', 'c++', 'Java'])) print("/".join(['Python', 'c++', 'Java'])) print("_".join(['Python', 'c++', 'Java'])) print("_".join([str(i) for i in range(10)]))
只有方法的类
# 类是对象的模板, 对象是类的具体实例 class Frog(): ## 行为, 就是方法 就是我们 之前学的函数, 只不过要加self def jump(self): print("愉快的跳跃中") def eat(self): print("愉快的吃着美食") # 使用类 # 根据类来创建对象被称为实例化。 # 对象名 = 类名() f1 = Frog() # f1对象调用 eat()方法, 通过.的形式 f1.eat() f1.jump()
# 类是对象的模板, 对象是类的具体实例 class Frog(): def __init__(self): self.name = None self.color = None self.age = 0 ## 行为, 就是方法 就是我们 之前学的函数, 只不过要加self def jump(self): print(f"{self.name}在愉快的跳跃中") def eat(self): print("愉快的吃着美食") # 使用类 # 根据类来创建对象被称为实例化。 # 对象名 = 类名() f1 = Frog() f1.name = "小顽皮" # f1对象调用 eat()方法, 通过.的形式 f1.eat() f1.jump()
class Frog(): def __init__(self): self.name = None self.color = None self.age = 0 def jump(self): print(f"{self.name}在愉快的跳跃中, self的地址{id(self)}") def eat(self): print("愉快的吃着美食") f1 = Frog() print(id(f1)) f1.name = "小顽皮" f1.jump() f2 = Frog() print(id(f2)) f2.name = "大顽皮" f2.jump()
class Frog(): def __init__(self, name, color, age=0): # 构造方法, 当类被实例化时会自动调用 # 作用:初始化成员属性 print("__init__") self.name = name self.color = color self.age = age def jump(self): print(f"{self.name}在愉快的跳跃中,颜色是{self.color} 年龄{self.age}") def eat(self): print("愉快的吃着美食") f1 = Frog("小顽皮", "绿色") f2 = Frog("大顽皮", "绿色", 3) f1.jump() f2.jump()
封装/继承/多态
子类继承父类所有属性和方法
class Person(): def __init__(self,name, age=0): self.name=name ## 私有属性 self.__age = age def getAge(self): return self.__age def setAge(self, age): self.__age = age def eat(self): print(f'{self.name}在吃饭') def __show(self): print("show()") class Student(Person): def __init__(self,name,mark=0): super().__init__(name) # Person.__init__(self,name) # fu self.mark=mark def eat(self): """重写""" print(f'重新实现eat()') def study(self): print(f'{self.name}在学习,拿了{self.mark}分') class Teacher(Person): def __init__(self,name,sal=0): super().__init__(name) self.salary=sal def teach(self): print(f'{self.name}在教课,工资{self.salary}') s1 = Student("张珊", 100) # print(s1.__age) # s1.__show() s1.study() s1.eat()
class Person(): def __init__(self,name, age=0): self.name=name ## 私有属性 self.__age = age def getAge(self): return self.__age def setAge(self, age): self.__age = age def eat(self): print(f'{self.name}在吃饭') def __show(self): print("show()") class Student(Person): def __init__(self,name,mark=0): super().__init__(name) # Person.__init__(self,name) # fu self.mark=mark def eat(self): """重写""" print(f'重新实现eat()') def study(self): print(f'{self.name}在学习,拿了{self.mark}分') class Teacher(Person): def __init__(self,name,sal=0): super().__init__(name) self.salary=sal def teach(self): print(f'{self.name}在教课,工资{self.salary}') s1 = Student("张珊", 100) # print(s1.__age) # s1.__show() s1.study() s1.eat()