python基础语法学习笔记(三)

整理一下自己初学python时打的一些代码,方便日后用的时候查阅。

41 缺省参数、命名参数

def add(a, b=22, c=33):  # 这就是缺省参数,缺省参数必须在后头
    result = a+b+c
    print("%d + %d + %d = %d" % (a, b, c, result))


add(11)
add(11, 33)
add(11, c=45)  # 可以指定给c赋值  叫命名参数

42 不定长参数——若干个数字相加练习

def sum_nums(a, b, *args):  # args只能在最后
    print("*"*50)
    print(a)
    print(b)
    print(args)
    summary = a + b
    n = len(args)
    i = 0
    while i < n:
        summary = summary + args[i]
        i += 1
    print("sum = %d" % summary)
    # for num in args:
    #   summary += num

sum_nums(11, 22, 33, 44, 55, 66, 77, 88)
sum_nums(11, 22, 33)
sum_nums(11, 22)

43 拆包

def sum_nums(a, b, *args, **kwargs):  # kwargs 字典
    print("*"*50)
    print(a)
    print(b)
    print(args)
    print(kwargs)


# sum_nums(11, 22, 33, 77, 88, 99, task=44, done=55)  # 带变量名的会送入字典
A = (44, 55, 66)
B = {"name": "hhhh", "age": 18}
sum_nums(11, 22, 33, *A, **B)

44 引用

a = 100
b = a
print(id(a))
print(id(b))

print("*"*50)
A = [11, 22, 33]
B = A
print(id(A))
print(id(B))
A.append(44)
print(A)
print(B)

# python里就数字、字符串、元组是不可变类型

45 递归 求阶乘

def func(n):
    if n == 1:
        return 1
    else:
        n = n*func(n-1)
        return n


num = int(input("输入你想阶乘的数字:"))
result = func(num)
print("result=%d" % result)

46 匿名函数

unc = lambda a, b: a+b  # 只能 lambda 参数: 表达式(1+3)
c = func(1, 2)
print(c)

# 匿名函数的操作
nums = [11, 3, 66, 777, 223234, 10, 72]
nums.sort()  # 排序
print(nums)

infors = [{"name": "zhusan", "age": 18}, {"name": "hhhh", "age": 20}, {"name": "nihaoaaa", "age": 34}]
infors.sort(key=lambda x: x["name"])  # 按名字排序
print(infors)
infors.sort(key=lambda x: x["age"])  # 按年龄排序
print(infors)

47 匿名函数的应用

def test(a, b, func):
    result = func(a, b)
    print(result)


test(11, 22, lambda x, y: x+y)

48 匿名函数的应用2

def test(a, b, funcc):
    result = funcc(a, b)
    print(result)


func = input("请输入一个匿名函数:")
func = eval(func)

test(11, 22, func)

49 补充1

# a = 100
a = [100]


def test(num):
    num += num  # 让num指向新的值
    print(num)


test(a)
print(a)

50 补充2-交换方式

a = 4
b = 5
'''  # 1
a = a+b
b = a-b
a = a-b
'''
a, b = b, a
print(a)
print(b)

51 面向对象

class Cat:
    def __init__(self, new_name, new_age):  # 初始化对象 创建对象 python自动调用__init__方法
        self.name = new_name
        self.age = new_age

    def __str__(self):  # print(对象)时 的 返回值
        return "%s的年龄是%d" % (self.name, self.age)

    def eat(self):  # 必须写self
        print("The cat is eating")

    def drink(self):
        print("The cat is drinking...")

    def introduce(self):
        print("%s的年龄是 %d" % (self.name, self.age))  # python通用用的是self


Tom = Cat("汤姆", 40)
Tom.eat()
Tom.drink()
# 添加Tom所指的类的属性
Tom.introduce()
print(Tom)

lanmao = Cat("蓝猫", 33)
lanmao.eat()
lanmao.drink()
lanmao.introduce()
print(lanmao)

52 练习-烤地瓜

class SweetPotato:

    def __init__(self):
        self.cookedString = "生的"
        self.cookedLevel = 0
        self.condiments = []

    def __str__(self):
        return "地瓜%s(%d),添加的作料有:%s" % (self.cookedString, self.cookedLevel, str(self.condiments))

    def cook(self, cooked_time):
        self.cookedLevel += cooked_time
        if 0 <= self.cookedLevel < 3:
            self.cookedString = "生的"
        elif 3 <= self.cookedLevel < 5:
            self.cookedString = "半生不熟"
        elif 5 <= self.cookedLevel < 8:
            self.cookedString = "熟了"
        elif self.cookedLevel > 8:
            self.cookedString = "烤糊了"

    def addcondiments(self, add_condiments):
        self.condiments.append(add_condiments)


potato1 = SweetPotato()
potato1.addcondiments("番茄酱")
print(potato1)

potato1.cook(1)
print(potato1)


potato1.cook(1)
potato1.addcondiments("孜然")
print(potato1)

potato1.cook(5)
print(potato1)

53 练习-房子

class Home:
    def __init__(self, new_size, new_info, new_addr):
        self.size = new_size
        self.info = new_info
        self.addr = new_addr
        self.left_area = new_size
        self.furniture = []

    def __str__(self):
        return "房子的总面积:%d平方米,可用面积:%d,户型:%s,地址:%s, 所拥有的家具:%s"\
               % (self.size, self.left_area, self.info, self.addr, str(self.furniture))

    def add_furniture(self, furniture):
        self.furniture.append(furniture.getname())
        self.left_area -= furniture.getarea()


class Bed:
    def __init__(self, new_name, new_area):
        self.name = new_name
        self.area = new_area

    def __str__(self):
        return "%s占用的面积是:%d" % (self.name, self.area)

    def getarea(self):
        return self.area

    def getname(self):
        return self.name


fangzi = Home(129, "三室一厅", "北京")
print(fangzi)

bed1 = Bed("席梦思", 4)
fangzi.add_furniture(bed1)
print(fangzi)

bed2 = Bed("三人床", 6)
fangzi.add_furniture(bed2)
print(fangzi)

54 隐藏属性

class Dog:
    def set_age(self, new_age):
        if 0 < new_age <= 100:
            self.age = new_age
        else:
            self.age = 0

    def get_age(self):
        return self.age


dog = Dog()
dog.set_age(7)
age = dog.get_age()
print(age)

55 私有方法 __test 即为私有

class Dog:
    def __test1(self):
        print("----1----")

    def test2(self):
        print("----2----")


dao = Dog()
dao.__test1()
dao.test2()

56

class Dog:
    def __del__(self):
        print("---over---")


dog1 = Dog()
dog2 = dog1

del dog1
print("======")  # 在这之后才delete dog2

57 继承

class Animal:
    def eat(self):
        print("----chi----")

    def drink(self):
        print("----drink----")


class Dog(Animal):  # 继承
    def bark(self):
        print("汪汪叫")


class Cat(Animal):
    def catch(self):
        print("hhhhh")


dog1 = Dog()
dog1.eat()
cat1 = Cat()
cat1.drink()
cat1.catch()

58

class Animal:
    def eat(self):
        print("----chi----")

    def drink(self):
        print("----drink----")


class Dog(Animal):  # 继承
    def bark(self):
        print("汪汪叫")


class Xiaotq(Dog):
    def fly(self):
        print("i can fly")


xiao = Xiaotq()
xiao.eat()

59 重写

class Animal:
    def eat(self):
        print("----chi----")

    def drink(self):
        print("----drink----")


class Dog(Animal):  # 继承
    def bark(self):
        print("汪汪叫")


class Xiaotq(Dog):
    def fly(self):
        print("i can fly")

    def bark(self):
        print("------狂叫--------")
        # 第一种
        # Dog.bark(self)  # 同时调用父类方法
        # 第二种
        super().bark()


xiao = Xiaotq()
xiao.eat()
xiao.bark()

60 私有方法、私有属性在继承中的体现

class A:
    def __init__(self):
        self.num1 = 100
        self.__num2 = 200  # 私有属性

    def test1(self):
        print("---test1---")

    def __test2(self):  # 私有方法
        print("---test2---")

    def test3(self):
        self.__test2()
        print(self.__num2)


class B(A):
    def test4(self):
        self.__test2()
        print(self.__num2)


b = B()
b.test1()
# b.__test2()
# 私有方法不会被继承,私有属性也不会被继承
print(b.num1)
# print(b.__num2)

b.test3()  # test3内有私有方法和私有属性,这里可以调用

# b.test4()  # 子类中的共有方法是不能继承父类的私有方法和私有属性的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值