python学习

字符串格式化

print输出不换行

for循环 

 range语句

案例 

函数

None类型

global关键字

python数据容器

列表

列表的方法

"""
list列表的相关操作
"""
mylist = ["itcast", "itheima", "python"]
# 1.1查找某元素在列表内的下标索引(如果被查找的元素不存在,会报错)
index = mylist.index("itheima")
print(index)    # 1
# 2. 修改特定下标索引的值
mylist[0] = "传智教育"
print(f"列表被修改元素后,结果是:{mylist}")    # 列表被修改元素后,结果是:['传智教育', 'itheima', 'python']
# 3.在指定下标位置插入新元素
mylist.insert(1, "best")
print(mylist)   #['传智教育', 'best', 'itheima', 'python']
# 4.在列表尾部追加单个新元素
mylist.append("黑马程序员")
print(mylist)   # ['传智教育', 'best', 'itheima', 'python', '黑马程序员']
# 5.在列表尾部追加一批元素
mylist2 = [1, 2, 3]
mylist.extend(mylist2)
print(mylist)   # ['传智教育', 'best', 'itheima', 'python', '黑马程序员', 1, 2, 3]
# 6.删除元素两种方式(del关键字/pop方法)
mylist = ["itcast", "itheima", "python"]
del mylist[2]
print(mylist)   # ['itcast', 'itheima']
mylist = ["itcast", "itheima", "python"]
element = mylist.pop(2)
print(element)  # python
print(mylist)   # ['itcast', 'itheima']
# 7.删除某元素在列表中的第一个匹配项
mylist = ["itcast", "itheima", "itcast", "itheima", "python"]
mylist.remove("itheima")
print(f"通过remove方法移除元素后,列表的结果是:{mylist}")# 通过remove方法移除元素后,列表的结果是:['itcast', 'itcast', 'itheima', 'python']
# 8.清空列表
mylist.clear()
print(mylist)   # []
# 9.统计列表内某元素的数量
mylist = ["itcast", "itheima", "itcast", "itheima", "python"]
count = mylist.count("itheima")
print(count)    # 2
# 10.统计列表中全部元素的鼠=数量
mylist = ["itcast", "itheima", "itcast", "itheima", "python"]
count = len(mylist)
print(count)    # 5

元组(不可修改)

 

字符串(不可修改,修改后是新的字符串)

序列化 

集合(去重,乱序,无下标索引)

由于集合不支持下标索引,所以对于集合的遍历只有for循环,不支持while循环 

字典(去重)

字典可以嵌套,但是字典中的key不可是字典

字典的遍历(不支持while循环)

数据容器的对比

容器的排序 (结果是列表)

函数

位置不定长的结果是元组,关键字不定长的结果是字典

lambda函数

文件

异常

模块

json

python面向对象

self关键字在定义方法时必须存在,在传参调用方法时,也可以忽略,在方法内部访问成员属性,必须调用self关键字

实例

构造方法

魔术方法

class student:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    #__str__魔术方法
    def __str__(self):
        return f"student类对象, name:{self.name}, age:{self.age}"

    # __lt__魔术方法, <  >
    def __lt__(self, other):
        return self.age < other.age
    # __le__魔术方法, <=  >=
    def __le__(self, other):
        return self.age <= other.age
    # __eq__魔术方法, ==
    def __eq__(self, other):
        return self.age == other.age
stu = student("周杰伦", 31)
stu1 = student("林俊杰", 36)
# print(stu)
# print(str(stu))
# print(stu < stu1)
# print(stu > stu1)
# print(stu <= stu1)
# print(stu >= stu1)
print(stu == stu1)
"""
没有__str__ 魔术方法时,打印输出的是地址
<__main__.student object at 0x000002146BE65520>
<__main__.student object at 0x000002146BE65520>
有__str__ 魔术方法时,打印输出的是
student类对象, name:周杰伦, age:31
student类对象, name:周杰伦, age:31
"""

"""
没有__lt__魔术方法时会报错
有__lt__魔术方法时会输出
True
False
"""

"""
 没有__le__魔术方法时会报错
 有__le__魔术方法时会输出
 Ture
 False
"""

"""
没有__eq__魔术方法时会打印false,默认会以对象的地址进行==比较
有__eq__魔术方法时会打印false
"""

封装(私有属性跟私有方法在类外被调用时报错,只能在类内部被使用)

class Phone:
    __curent_voltage = 0.5 # 当前手机运行电压

    def __keep_single_core(self):
        print("让CPU以单核模式运行")

    def call_by_5g(self):
        if self.__curent_voltage >= 1:
            print("5G通话已开启")
        else:
            self.__keep_single_core()
            print("电量不足,无法使用5G通话,并已设置为单核模式运行")

phone = Phone()
phone.call_by_5g()
"""
输出结果是:
让CPU以单核模式运行
电量不足,无法使用5G通话,并已设置为单核模式运行
"""

继承(单继承,多继承)

多继承(如果父类有同名方法或属性, 先继承的优先级高于后继承) 

# 单继承
class Phone:
    IMEI = None # 序列号
    producer = "HM" # 厂商

    def call_by_4g(self):
        print("4G通话")

class Phone2022(Phone):
    face_id = "10001"

    def call_by_5g(self):
        print("2022年新功能:5G通话")

phone = Phone2022()
print(phone.producer)
phone.call_by_4g()
phone.call_by_5g()
"""
HM
4G通话
2022年新功能:5G通话
"""

# 多继承
class NFCReader:
    nfc_type = "第五代"
    producer = "HM"

    def read_card(self):
        print("NFC读卡")

    def write_card(self):
        print("NFC写卡")

class RemoteControl:
    rc_type = "红外遥控"

    def control(self):
        print("红外遥控开启了")

class MyPhone(Phone, NFCReader, RemoteControl):
    pass

phone = MyPhone()
phone.call_by_4g()
phone.read_card()
phone.write_card()
phone.control()
"""
4G通话
NFC读卡
NFC写卡
红外遥控开启了
"""

复写

注解

联合类型注解

多态

python操作数据库

pip install pymysql

也可以在建立连接的时候使用默认提交的方式autocommit=True 

正则表达式

元字符匹配

递归

import os

def test_os():
    """
    演示os模块的3个基础方法
    :return:
    """
    print(os.listdir("D:/test")) # 列出指定路径下的内容
    print(os.path.isdir("D:/test/a")) # 判断路径是否为文件夹
    print(os.path.exists("D:/test")) # 判断指定路径是否存在

def get_files_recursion_from_dir(path):
    """
    从指定的文件夹中使用递归的方式,获取全部文件列表
    :param path: 被判断的文件夹
    :return: list集合
    """
    file_list = []
    if os.path.exists(path):
        for f in os.listdir(path):
            new_path = path + "/" + f
            if os.path.isdir(new_path):
                # 进入到这里,说明是文件夹不是文件
                file_list += get_files_recursion_from_dir(new_path)
            else:
                file_list.append(new_path)
    else:
        print(f"指定的目录{path}, 不存在")
        return []
    return file_list


if __name__ == '__main__':
    print(get_files_recursion_from_dir("D:/test"))
    # ['D:/test/1.txt', 'D:/test/2.txt', 'D:/test/3.txt', 'D:/test/a/a1.txt', 'D:/test/a/a2.txt', 'D:/test/a/a3.txt',
    # 'D:/test/a/d/e/e1.txt', 'D:/test/a/d/e/e2.txt', 'D:/test/a/d/e/e3.txt']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值