潦草地记录了个人于Python基础学习中的笔记

前置知识

python解释器作用:将代码翻译为二进制语言以提供给计算机运行

.py文件是py的代码文件

字面量

字面量:代码中直接表示特定值的语法表达式,换句话说:字面量是一种用于表示数据值的直接方式

# 整数字面量
number = 42
# 浮点数字面量
pi = 3.14
# 字符串字面量
message = "Hello, World!"
# 布尔值字面量
is_true = True
is_false = False
# 数据容器字面量
my_list = [1, 2, 3, 4]
my_tuple = (1, 2, 3, 4)
my_dict = {'a': 1, 'b': 2, 'c': 3}
my_set = {1, 2, 3, 4}

这些字面量直接表示相应类型的值。例如,整数字面量直接表示整数值,字符串字面量直接表示字符串值,依此类推。

注释

# 这是一个单行注释
age = 25  # 这是对变量的注释

'''
这是一个
多行注释的例子
'''

"""
这也是一个
多行注释的例子
"""

变量

变量:存储计算结果或表示值的抽象概念(变量名=值)

x = 10 # 整数类型
name = "John" # 字符串类型

数据类型

数据类型是指变量所表示的数据的种类,如int、float、str等

type()用于查看数据类型,会返回值:

注意:变量无类型,数据本身才有类型

转换数据类型

  • int() 将值转换为整数。
  • float() 将值转换为浮点数。
  • str() 将值转换为字符串。
  • list() 将可迭代对象转换为列表。
  • tuple() 将可迭代对象转换为元组。
  • set() 将可迭代对象转换为集合。
  • bool() 将值转换为布尔值。
# 从字符串解析出数字
str_num = "42"
num = int(str_num)

# 将列表转换为字符串
my_list = [1, 2, 3]
str_list = ",".join(map(str, my_list))

标识符

标识符:变量,方法,类的名字等,是用户给他们的命名

只可以使用中文,英文,数字和下划线命名;大小写敏感;不可占用关键字

# 合法的标识符
my_variable = 42
user_name = "John"
calculate_sum = lambda x, y: x + y

# 不合法的标识符
3variable = 42  # 以数字开头
my-var = 10     # 包含横杠
if = 5          # 使用关键字作为标识符

运算符

算术运算符

  • +:加法
  • -:减法
  • *:乘法
  • /:除法
  • //:整除
  • %:取余
  • **:幂运算
a = 10
b = 3

result_add = a + b       # 13
result_sub = a - b       # 7
result_mul = a * b       # 30
result_div = a / b       # 3.333...
result_floor = a // b    # 3
result_mod = a % b       # 1
result_pow = a ** b      # 1000

比较运算符

  • ==:等于
  • !=:不等于
  • <:小于
  • >:大于
  • <=:小于等于
  • >=:大于等于
x = 5
y = 10

result_equal = x == y    # False
result_not_equal = x != y # True
result_less = x < y       # True
result_greater = x > y    # False
result_less_equal = x <= y # True
result_greater_equal = x >= y # False

逻辑运算符

  • and:与
  • or:或
  • not:非
p = True
q = False

result_and = p and q     # False
result_or = p or q        # True
result_not = not p        # False

赋值运算符

  • =:赋值
  • +=:加法赋值
  • -=:减法赋值
  • *=:乘法赋值
  • /=:除法赋值
  • //=:整除赋值
  • %=:取余赋值
  • **=:幂运算赋值
x = 5
x += 3     # x 现在为 8
x -= 2     # x 现在为 6
x *= 4     # x 现在为 24
x /= 2     # x 现在为 12.0

成员运算符

  • in:在序列中
  • not in:不在序列中
my_list = [1, 2, 3, 4]

result_in = 2 in my_list       # True
result_not_in = 5 not in my_list # True

字符串与其操作

定义方式

字符串的定义支持使用单引号,双引号和三引号

my_string_single = 'Hello, Python!'
my_string_double = "Hello, Python!"

在pycham中,ctrl+shift+上或下可以移动代码位置

字符串拼接

# 使用+运算符
str1 = "Hello"
str2 = "World"

result = str1 + " " + str2  # 'Hello World'

# 使用+=运算符
str1 = "Hello"
str2 = "World"

str1 += " " + str2  # 等效于 str1 = str1 + " " + str2
print(str1)         # 'Hello World'

# 使用 % 运算符进行格式化(在Python 2中常见,不推荐在Python 3中使用)
str1 = "Hello"
str2 = "World"

result = "%s %s" % (str1, str2)  # 'Hello World'

# 使用 format() 方法
str1 = "Hello"
str2 = "World"

result = "{} {}".format(str1, str2)  # 'Hello World'

# 使用 f-strings(Python 3.6及以上版本)
str1 = "Hello"
str2 = "World"

result = f"{str1} {str2}"  # 'Hello World'

# 使用 join() 方法
words = ["Hello", "World", "!"]

result = " ".join(words)  # 'Hello World !'

字符串格式化

字符串格式化:通过占位实现拼接,它会将不同类型转化为占位标识对应的类型并拼接进去

message = "%s + %s = 10" % (4,6)

# %s 转字符串占位
# %d 转整数占位
# %f 转浮点型占位(m.n可用于控制数据的宽度和精度,写在%与d/f的中间,如%5.2f,%.2f)

快速格式化:`f"{变量}";缺点是无法进行精度的控制

name = "Alice"
age = 30

formatted_string = "My name is {} and I am {} years old.".format(name, age)
# 或者使用 f-string
formatted_string_f = f"My name is {name} and I am {age} years old."

表达式

具有明确执行结果的代码语句
表达式格式化

print("1+1=%d" % (1+1))
print(f"1+2={1+2}")
print("字符串的数据类型是%s" % type("牛魔"))

print语句输出不换行的方法

print("hello", end='原神')
print("world", end='启动')

数据输入

获取键盘输入:input语句

input语句会将你输入的内容当成字符串对待(使用type语句可验证)

所以你需要使用数据类型转换的语句,如int()

IF

if语句:

  1. 注意空格缩进
  2. 不能忘记判断条件之后的:
  3. 判断结果需要是布尔类型
age = 18
if age>=18:
    print("已成年")
    print("老东西")
else:
    print("未成年")

if else语句:

  1. else不需要判断条件,当if的条件不满足时,else执行
  2. else的代码块同样需要缩进

if elif else语句:

age = int(input("输入你的数字:"))

if age < 120:
    print("1")
elif age >=120 and age < 180:
    print("2")
elif age >= 180 and age <300:
    print("3")
elif (age >300 or age == 300) and age < 500:
    print("4")
else: # else可以不写
    print("5")

WHILE

while循环

num = 0
i = 1
while i <= 100:
    num += i
    i += 1
print (num)
#通过while循环实现9x9乘法表
i = 1
while i <= 9:
    j=1
    while j <= i:
        print(f"{i}*{j}={i*j}\t",end="")
        j+=1
    i+=1
    print()

FOR

for循环

name = "牛魔酬宾"
# for 临时变量 in 数据集(序列类型):
for x in name:
    print(x) # 循环满足条件时的执行的代码

range语句的语法:

  1. range(num) 从0开始,到num结束,如num=5,得到[0,1,2,3,4]
  2. range(num1,num2)从num1开始,到num2结束,如num1=5,num2=10,得到[5,6,7,8,9]
  3. range(num1,num2,step) 从1开始,到2结束,隔step取一个数字,如num1=5,num2=10,step=2,得到[5,7,9]
for i in range(5):
    print(i)
# Output:
# 0
# 1
# 2
# 3
# 4

for i in range(2, 8):
    print(i)
# Output:
# 2
# 3
# 4
# 5
# 6
# 7

for i in range(1, 10, 2):
    print(i)
# Output:
# 1
# 3
# 5
# 7
# 9

continue关键字:临时结束本次循环

for i in range(5):
    if i == 2:
        continue
    print(i)
# 当 i 的值等于 2 时,continue 被触发,跳过了 print(i),直接进入下一次循环。因此,输出将不包含 2。

break关键字:结束循环(注:只能结束它所在的循环)

for i in range(5):
    if i == 3:
        break
    print(i)
# 当 i 的值等于 3 时,break 被触发,导致循环立即终止。因此,输出将是 0、1、2。

函数

  1. 已组织好的
  2. 可重复使用的
  3. 针对特定功能的

将功能封装入函数后可以重复利用,减少重复代码,提高效率

def say_hi():
    print("hello world")

say_hi()  # 执行顺序是先执行say_hi(),再返回到函数内的语句中

必须先定义,后使用

函数的参数

函数传入参数的功能允许其接受外部调用时接收来自外部的数据

def add (x,y):
    result = x + y
    print(f"{x}+{y}的结果是{result}")

add(10,20)

函数的返回值

函数完成运算后返回个调用者的结果

别把代码写到return的后面

def add (x,y):
    result = x + y
    return result

r = add(10,20)
print(r)

None类型

其表示空的,无意义的意思

一个函数如果没有 return 语句或者 return 后面没有值,函数会隐式返回 None

同时也可以主动用return返回None

def say_hi():
    print("hello world")

say_hi()
result = say_hi()
print(result)   # 输出:None

print(type(result))  # 输出:<class 'NoneType'>

# ---

def my_function():
    print("Hello")
    return 42

result = my_function()
print(result)  # 输出: 42

在IF判断中,None = False

def check_age(x):
    if x >= 18:
        return True
    else:
        return None

result = check_age(16)
if not result:
    print("未成年")
else:
    print("已成年")

也可以用于声明无初始内容的变量(如name = None),然后在程序的执行过程中根据需要将其赋予其他值。

函数的嵌套调用

函数的嵌套调用是指在一个函数的定义或执行过程中调用另一个函数。这种嵌套结构可以是函数调用自身(递归调用),也可以是调用其他函数。

def outer_function():
    print("Outer Function")

    def inner_function():
        print("Inner Function")

    inner_function()

outer_function()

## 输出:
# Outer Function
# Inner Function

函数嵌套调用的参数传递

def outer_function(x):
    print("Outer Function:", x)

    def inner_function(y):
        print("Inner Function:", y)

    inner_function(x + 1) # 输出为6

outer_function(5) # 输出为5

函数的递归调用

def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n - 1)

result = factorial(5)
print(result)  # 输出: 120

局部变量:定义在函数体内的变量,只在函数体内生效;它们在函数开始执行时创建,在函数结束时销毁。

def example_function():
    x = 10  # 这是局部变量
    print(x)

example_function()
# 在这里尝试访问 x 会导致 NameError,因为 x 是局部变量

全局变量:在函数内外都能生效的变量

global_variable = 20  # 这是全局变量

def example_function():
    print(global_variable)  # 可以在函数内部访问全局变量

example_function()

如果在函数内部使用与全局变量同名的变量,Python 将创建一个新的局部变量,而不是修改全局变量。如果需要修改全局变量,应使用 global 关键字。

x = 10  # 全局变量

def modify_global_variable():
    x = 5  # 这是一个新的局部变量,而不是修改全局变量
    print(x)

modify_global_variable()
print(x)  # 输出: 10,全局变量未被修改
x = 10  # 全局变量

def modify_global_variable():
    global x  # 使用 global 关键字声明要修改的是全局变量
    x = 5
    print(x)

modify_global_variable()
print(x)  # 输出: 5,全局变量被修改

数据容器

可以存储多个元素的数据类型

包括listtuplestrsetdict{1}

列表

使用方括号 [] 来创建列表,并在方括号内放置元素,用逗号分隔。

name = ['原神','启动']
print(name)
print(type(name))

name1 = ['原神',1,True,[1,2,3],[[123,321],[151,'原神']],type(1)]
print(name1)
print(type(name1))

使用索引来访问列表中的元素。索引从0开始,可以使用负索引从列表末尾开始计数。

name = ['原神', '启动', [1, 5, 9, 74], 6, 21, 5, 1, 3, 48, 6, 65, 5]
print(name[0])
print(name[1])
print(name[2][0])
print(name[2])
print(name[4])
print(name[5])
print(name[-5])
print(name[-4])
print(name[-3])
print(name[-2])
print(name[-1])

取出列表中的元素

列表的方法

  1. 查找某元素的下标
name = ['原神', '启动', [1, 5, 9, 74], 6, 21, 5, 1, 3, 48, 6, 65, 5]

# 列表.index(元素)
index = name.index('原神')
print(index) # 输出: 0
  1. 修改特定位置的元素
name = ['原神', '启动', [1, 5, 9, 74], 6, 21, 5, 1, 3, 48, 6, 65, 5]
print(name[0])

# 列表[下标]=值
name[0] = '牛魔'
print(name[0])
  1. 在指定下标位置插入新元素
name = ['原神', '启动', [1, 5, 9, 74], 6, 21, 5, 1, 3, 48, 6, 65, 5]

# 列表.insert(下标,元素)
name.insert(1,'太美丽了')
print(name)
  1. 追加元素(写入尾部)
name = ['原神', '启动', [1, 5, 9, 74], 6, 21, 5, 1, 3, 48, 6, 65, 5]

# 列表.append(元素)
name.append("酬宾")
print(name)
  1. 将另一个列表的元素添加到当前列表
op = ['原神','启动']
name = ['原神', '启动', [1, 5, 9, 74], 6, 21, 5, 1, 3, 48, 6, 65, 5]

# 列表.extend(列表2)
name.extend(op)
print(name)
  1. 删除元素
name = ['原神', '启动', [1, 5, 9, 74], 6, 21, 5, 1, 3, 48, 6, 65, 5]

# del 列表[下标]
del name[0]
print(name)
name = ['原神', '启动', [1, 5, 9, 74], 6, 21, 5, 1, 3, 48, 6, 65, 5]

# 列表.pop(下标) ##pop的这个值可以取出来
op = name.pop(0)
print(name)
print(op)
  1. 删除某元素在列表中的第一个匹配项
name = ['原神', '启动', [1, 5, 9, 74], 6, 21, 5, 1, 3, 48, 6, 65, 5]

# 列表.remove(元素)
name.remove('原神')
print(name)
  1. 清空列表
name = ['原神', '启动', [1, 5, 9, 74], 6, 21, 5, 1, 3, 48, 6, 65, 5]

# 列表.clear()
name.clear()
print(name)
  1. 统计某个元素在列表中的数量
name = ['原神', '启动', [1, 5, 9, 74], '原神', '原神', 5, 1, 3, '原神', 6, 65, 5]

# 列表.count(元素)
op = name.count('原神')
print(op)
  1. 统计列表内的元素个数
name = ['原神', '启动', [1, 5, 9, 74], '原神', '原神', 5, 1, 3, '原神', 6, 65, 5]

# len(列表)
op = len(name)
print(op)

列表的迭代

-while循环

List = [21, 25, 21, 23, 22, 20, 31, 29, 33, 30]
index = 0
while index < len(List):
    num = List[index]
    print(num)
    index += 1

-for循环

List = [21, 25, 21, 23, 22, 20, 31, 29, 33, 30]
for index in List:
    print(index)

列表的切片

可以使用切片操作获取列表的子集

my_list = [1, 2, 3, 'apple', 'orange']

subset = my_list[1:4]   # [2, 3, 'apple']

元组

使用圆括号 () 来创建元组,并在括号内放置元素,用逗号分隔。

my_tuple = (1, 2, 'apple', 'orange')

t1 = (1,2,3)
t2 = tuple()
t3 = ()
t4 = (4, ) #单独一个元素需要后面加逗号
t5 = ((1,2,3),(4,5,6))

元组一旦定义完成就不可以修改,其内的元素和列表一样可以是不同的数据类型

注意,元组的元素不可修改,但是不代表内容一定不能改

t1 = (1,2,[5,6])

t1[2][0] = 3
t1[2][1] = 4
print(t1)

元组的方法

  1. 查找数据的下标
my_tuple = (1, 2, 'apple', 'orange')

# tuple.index()
print(my_tuple.index(1)) # 0
  1. 统计指定元素个数
my_tuple = (1, 2, 'apple', 'orange')

# tuple.count()
print(my_tuple.count(1)) # 1
  1. 统计元组内元素个数
my_tuple = (1, 2, 'apple', 'orange')

# len()
print(len(my_tuple)) # 4

元组的切片

my_tuple = (1, 2, 'apple', 'orange')
subset = my_tuple[1:3]   # (2, 'apple')

字符串

也是属于数据容器的一类,元组的性质都有

字符串的方法

  1. 字符串的替换
original_string = "Hello, World!"

# 字符串.replace(被替换字符串,替换的字符串)
new_string = original_string.replace("World", "Python")
print(new_string)  # 输出: 'Hello, Python!'
  1. 字符串的分割
sentence = "This is a sample sentence."

# 字符串.split(分割符字符串)
words = sentence.split()
print(words)
# 输出: ['This', 'is', 'a', 'sample', 'sentence.']
# 分割出来的结果会得到一个列表
  1. 字符串的规整操作
text = "    Python is fun!    "

# 字符串.strip()
cleaned_text = text.strip()
print(cleaned_text)  # 输出: 'Python is fun!'
text = "Hello, Python!"

# 大小写转换
lowercase_text = text.lower()
uppercase_text = text.upper()
capitalized_text = text.capitalize()
print(lowercase_text)    # 输出: 'hello, python!'
print(uppercase_text)    # 输出: 'HELLO, PYTHON!'
print(capitalized_text)  # 输出: 'Hello, python!'

字符串切片

text = "Hello, World!"
substring1 = text[0:5]    # 'Hello'
substring2 = text[7:]     # 'World!'
substring3 = text[:5]     # 'Hello'

序列

内容连续,有序,可使用下标索引的一类数据容器

列表,元组,字符串都可以视为序列

序列切片:从序列中取出一个子序列

对序列切片时不会影响序列本身,切片的方法如上文演示

集合

与列表、元组、字符串不同的是它是一种无序、可变、不重复的数据结构,且它的内部是无序的(不支持索引访问)

使用花括号 {}set() 函数来创建集合。

{}
s1 = {1,2,3,"牛魔","酬宾"}
s2 = set()

集合的方法

  1. 添加元素
my_set = {1, 2, 3, 4, 5}
my_set.add(6)
  1. 移除元素
my_set = {1, 2, 3, 4, 5}

my_set.remove(3)
# 或者
my_set.discard(3)

## 如果元素不存在,remove() 方法会引发 KeyError,而 discard() 方法不会。
  1. 随机取出元素
s1 = {1,2,3,"牛魔","酬宾"}
num = s1.pop()
print(num)
  1. 清空集合
s1 = {1,2,3,"牛魔","酬宾"}
s1.clear()
print(s1)
  1. 取两个集合的差集
s1 = {1,2,3,"牛魔","酬宾"}
s2 = {1,2,3}
s3 = s1.difference(s2)
print(s3)
  1. 消除两个集合的差集 | 在s1内删除和s2相同的元素
s1 = {1,2,3,"牛魔","酬宾"}
s2 = {1,2,3}
s1.difference_update(s2)
print(s1)
  1. 两个集合合并成一个
s1 = {1,2,3,"牛魔","酬宾"}
s2 = {1,2,3,4}
s3 = s1.union(s2)
print(s3)
  1. 统计集合元素数量
s1 = {1,2,3,"牛魔","酬宾",1,2,3}
num = len(s1)
print(num) # 5

集合的迭代

s1 = {1,2,3,"牛魔","酬宾"}
for i in s1:
    print(i)

字典

它不允许元素重复,也不允许key重复

不使用下标索引

key不允许是字典,value允许是任何数据类型

dict1 = {"牛魔":99,"酬宾":88,"大神":77}
dict2 = {}
dict3 = dict()
print(dict1, type(dict1))
print(dict2, type(dict2))
print(dict3, type(dict3))

字典使用键来访问字典中的值

my_dict = {'name': 'John', 'age': 30, 'occupation': 'Engineer'}
name = my_dict['name']

字典的方法

  1. 新增(修改)元素
dict1 = {"牛魔":99,"酬宾":88,"大神":77}

# 字典[key] = Value 
## key存在则更新值,不存在则新增一个键值对
dict1["啊"] = 11
print(dict1)
dict1["牛魔"] = 98
print(dict1)
  1. 删除元素
dict1 = {"牛魔":99,"酬宾":88,"大神":77}

# 字典.pop(key)
num = dict1.pop("牛魔")
print(dict1)
print(num)
  1. 清空字典
dict1 = {"牛魔":99,"酬宾":88,"大神":77}

# 字典.clear()
dict1.clear()
print(dict1)
  1. 获得所有的key,value,所有的键值对
my_dict = {'name': 'John', 'age': 30, 'occupation': 'Engineer'}

keys = my_dict.keys()        # 返回所有键的视图
values = my_dict.values()    # 返回所有值的视图
items = my_dict.items()      # 返回所有键值对的视图
  1. 遍历字典(字典的迭代)
dict1 = {"牛魔":99,"酬宾":88,"大神":77}
keys = dict1.keys()
print(keys)
print(type(keys))

# 遍历键
for i in keys:
    print(i)
# 遍历值
for key in dict1:
    print(key)
    print(dict1[key])
# 遍历键值
for k,v in dict1.items():
    print(key, value)
  1. 统计字典内元素数量
num = len(dict1)
print(num)

容器的排序

列表的排序

sort() 方法是列表对象的方法,用于原地排序列表。

numbers = [4, 2, 7, 1, 9]

numbers.sort()
print(numbers)  # 输出: [1, 2, 4, 7, 9]

sort() 方法还接受一个 reverse 参数,用于指定排序顺序是升序还是降序。

numbers = [4, 2, 7, 1, 9]
numbers.sort(reverse=True)
print(numbers)  # 输出: [9, 7, 4, 2, 1]

sorted() 函数用于返回一个新的已排序的列表,而不改变原始列表。

numbers = [4, 2, 7, 1, 9]
sorted_numbers = sorted(numbers)
print(sorted_numbers)  # 输出: [1, 2, 4, 7, 9]
print(numbers)         # 输出: [4, 2, 7, 1, 9](原始列表未改变)

元组的排序

元组是不可变的,因此没有类似于 sort() 方法的原地排序方法。但可以使用 sorted() 函数来获取一个新的已排序元组。

tuple_numbers = (4, 2, 7, 1, 9)
sorted_tuple = tuple(sorted(tuple_numbers))
print(sorted_tuple)  # 输出: (1, 2, 4, 7, 9)

集合的排序

集合是无序的,因此不能使用排序方法。但同元组一样可以将集合转换为有序的列表,然后进行排序。

set_numbers = {4, 2, 7, 1, 9}
sorted_set = set(sorted(set_numbers))
print(sorted_set)  # 输出: {1, 2, 4, 7, 9}

字典的排序

可以对字典的键或值进行排序。

my_dict = {'apple': 3, 'banana': 1, 'orange': 2}
sorted_dict_keys = sorted(my_dict.keys())
print(sorted_dict_keys)  # 输出: ['apple', 'banana', 'orange']

如果想按值排序,可以使用 sorted() 函数和匿名函数

sorted_dict_items = sorted(my_dict.items(), key=lambda x: x[1])
print(sorted_dict_items)  # 输出: [('banana', 1), ('orange', 2), ('apple', 3)]

字符串大小比较:字符串的比较是基于数字的码值(ASCII)大小进行比较的

函数进阶

函数的多个返回值

def test_return():
    return 1,"hello",True

x,y,z = test_return()
print(x)
print(y)
print(z)

函数传参

  1. 位置参数:根据位置来传递参数,这是最常见的参数传递方式
def example_function(x, y):
    print(x, y)

example_function(1, 2)
  1. 关键字参数:通过指定参数名进行传递,可以不按照函数定义中的顺序传递参数。可以与位置参数混用,但混用时位置参数必须在关键字参数前面
def user_info(name,age,gender):
    print(f"{name},{age},{gender}")

user_info("牛魔",20,gender="男")
user_info(gender = "牛魔",age = 20,name = "男")
user_info(name = "牛魔",age = 20,gender = "男")
  1. 缺省参数:又叫默认参数,为函数的参数提供默认值,如果调用函数时没有传递该参数,将使用默认值。

    注:如果设置了的话,这个默认值一定是在后面的

def example_function(x, y=0):
    print(x, y)

example_function(1)  # 输出: 1 0
example_function(1, 2)  # 输出: 1 2
  1. 不定长参数

一个*是位置不定长参数*args,其会以元组的形式存在;

def example_function(*args):
    print(args)

# 使用可变位置参数进行函数调用
example_function(1, 2, 3)  # 输出: (1, 2, 3)

两个*是关键字不定长**kwargs,其会以字典的形式存在;

def example_function(**kwargs):
    print(kwargs)

# 使用可变关键字参数进行函数调用
example_function(a=1, b=2, c=3)  # 输出: {'a': 1, 'b': 2, 'c': 3}
  1. 命名关键字参数:又叫强制关键字参数,可以使用 * 来指示之后的参数是强制关键字参数。与位置参数和默认参数不同,命名关键字参数在函数调用时必须显式地使用参数名进行传递,而不是依赖于参数的位置。
def print_info(name, age, *, city, country):
    print(f"Name: {name}, Age: {age}, City: {city}, Country: {country}")

# 使用命名关键字参数进行函数调用
print_info("John", 25, city="New York", country="USA")

# 在上面的例子中,city和country被定义为命名关键字参数,它们前面有一个星号*。
# 在函数调用时,city和country必须使用参数名来传递,而不能根据它们在函数定义中的位置来传递。

匿名函数

def关键字,定义的是带有名称的函数,可以重复使用

lambda关键字,定义的是无名称的函数,只能使用一次,且只能写一行

def test_func(compute):
    result = compute(1,2)
    print(result)
test_func(lambda x,y:x+y)

文件

文件编码:即将内容翻译成二进制,再将二进制翻译回原先内容的技术(如utf-8,GBK等)

文件的操作

1.打开

使用 open() 函数打开文件,指定文件路径和打开模式。

name:文件名的字符串

mode:即打开文件的方式,如只读r,写入w和追加a

encoding:编码格式

# open(name,mode,encoding)
F = open("E:/测试.txt","r",encoding="UTF-8")
print(type(F))

2.读写

read()方法:读取字符,返回指定字符数量

print(F.read())

readlines()方法:读取所有行,返回列表

F = open("E:/测试.txt","r",encoding="UTF-8")
# print(type(F),F.read())
Line = F.readlines()
print(type(Line),Line)

readline()方法:读取一行

F = open("E:/测试.txt","r",encoding="UTF-8")
line1 = F.readline()
line2 = F.readline()
print(line1,line2)

for循环读取(文件迭代)

with open('example.txt', 'r') as file:
    for line in file:
        print(line.strip())

文件的关闭

3.关闭

如果不关,这个程序会一直被占用

file = open('example.txt', 'r')
content = file.read()
print(content)

# f.close()
file.close()

with语句:执行完毕后自动关闭文件,不需要显式调用 close() 方法。

with open('example.txt', 'r') as file:
    content = file.read()
    print(content)
# 文件在 with 块结束时自动关闭

文件的写入

write()之后flush()

write之后只是将内容写入到了内存之中,还需要flush将其真正写入文件

F = open("E:/测试01.txt","w",encoding="UTF-8")
F.write("太美丽了李唐")
#F.flush()
F.close() #close自带flush功能

异常

异常就是BUG,导致解释器无法正确运行的东西;或是程序运行时出现的错误

异常的捕获

定义:提前对可能出现的BUG进行处理

提前假设某处可能出现异常,做好提前准备,当真的出现异常时,可以有后续手段

使用 try...except 块来捕获可能引发异常的代码块。在 try 块中编写可能引发异常的代码,然后在 except 块中处理异常。

可以使用多个 except 块来捕获不同类型的异常。

try:
    f = open("E:/test01.txt","r",encoding = "UTF-8")
except:
    print("异常出现")
    f = open("E:/test01.txt","w",encoding = "UTF-8")
else:
    print("无异常")
try:
    f = open("E:/test01.txt","r",encoding = "UTF-8")
except Exception as e:
    print("异常出现")
    print(e)
    f = open("E:/test01.txt","w",encoding = "UTF-8")
else:
    print("无异常")

捕获指定异常

try:
    print(name)
except NameError as e:
    print("出现指定的异常")
    print(e)
try:
    # print(name)
    1/0
except (NameError,ZeroDivisionError) as e:
    print("出现指定的异常")
    print(e)

else

else 块用于在没有发生异常时执行的代码,

try:
    result = 10 / 2
except ZeroDivisionError as e:
    print(f"Error: {e}")
else:
    print(f"Result: {result}")

finally

finally 块用于无论是否发生异常都会执行的代码。

try:
    result = 10 / 2
except ZeroDivisionError as e:
    print(f"Error: {e}")
else:
    print(f"Result: {result}")
finally:
    print("This will always be executed.")

异常的传递性

def func1():
    print("func1开始执行")
    num = 1/0 #异常存在处
    print("func1结束执行")

def func2():
    print("func2开始执行")
    func1()
    print("func2结束执行")

def main():
    func2()

main()

E:\dev\code\venv\Scripts\python.exe E:\dev\code\main.py
func2开始执行
func1开始执行
Traceback (most recent call last):
File "E:\dev\code\main.py", line 443, in
main()
File "E:\dev\code\main.py", line 441, in main
func2()
File "E:\dev\code\main.py", line 437, in func2
func1()
File "E:\dev\code\main.py", line 432, in func1
num = 1/0

   ~^~

ZeroDivisionError: division by zero

进程已结束,退出代码为 1

def func1():
    print("func1开始执行")
    num = 1/0
    print("func1结束执行")

def func2():
    print("func2开始执行")
    func1()
    print("func2结束执行")

def main():
    try:
        func2()
    except Exception as e:
        print(f"出现异常,异常信息是:{e}")

main()

python模块

概述

什么是模块:以.py结尾的python文件,可以直接拿过来用

[from 模块名] import [模块 | 类 | 变量 | 函数 | * ] [别名]
import time

print("开始")
time.sleep(5)
print("完成")

from 模块名 import 功能名

from time import sleep

print("开始")
sleep(5)
print("完成")

from 模块名 import *

from time import *

print("开始")
sleep(5)
print("完成")

import 模块 as 别名

import time as a

print("开始")
a.sleep(5)
print("完成")

自定义模块

__init\_\_.py

__init\_\_.py文件的作用是将文件夹变为一个Python模块,Python中的每一个模块的包中,都有一个\_\_init\_\_.py

\__main\_\_属性

一个模块被另一个程序第一次引入时,其主程序将运行。如果我们想在模块被引入时,模块中的某一程序块不执行,我们可以用__name__属性来使该程序块仅在该模块自身运行时执行。

if __name__ == '__main__':
   print('程序自身在运行')
else:
   print('我来自另一模块')

在新模块内使用

import test_01

\_\_all__属性

\_\_all\_\_是针对模块公开接口的一种约定,以提供了”白名单“的形式暴露接口。如果定义了\_\_all\_\_,其他文件中使用from xxx import *导入该文件时,只会导入 __all\_\_ 列出的成员,其他成员都被排除在外。

# test_01
# __all__ = ['func']

def func():
   pass
# test_02

import test_01

__all__ = ['func2', 'test_01']
def func2():
    pass

def func3():
    pass
#test_03

from test_02 import *

func2()
test_01.func()
func3() #无法运行

python包

利用python绘制图形

前置:json格式

json的字符串必须是单引号或三引号

import json
data = [{"name":"牛魔","age":11},{"name":"酬宾","age":10},{"name":"原神","age":19}]

json_str = json.dumps(data, ensure_ascii = False)
print(type(json_str))
print(json_str)
import json
# data = [{"name":"牛魔","age":11},{"name":"酬宾","age":10},{"name":"原神","age":19}]
#
# json_str = json.dumps(data,ensure_ascii = False)
# print(type(json_str))
# print(json_str)

s = '[{"name":"牛魔","age":11},{"name":"酬宾","age":10},{"name":"原神","age":19}]'
a = json.loads(s)
print(type(a))
print(s)

pyecharts-折线图

from pyecharts.charts import Line
from pyecharts.options import TitleOpts,LegendOpts,ToolboxOpts,VisualMapOpts

line = Line()
line.add_xaxis(["中国","美国","英国"])
line.add_yaxis('GDP',[30,20,10])

line.set_global_opts(
    title_opts=TitleOpts(title="GDP展示", pos_left="center", pos_bottom="1%"),
    legend_opts=LegendOpts(is_show=True),
    toolbox_opts=ToolboxOpts(is_show=True),
    visualmap_opts=VisualMapOpts(is_show=True)
)


line.render()

面向对象

类与成员变量

class Student:
    name = None
    gender = None
    nationality = None
    native_place = None
    age = None

stu1 = Student()
stu1.name = "牛魔"
stu1.gender = "男"
stu1.nationality = "中国"
stu1.native_place = "广东省"
stu1.age = 31

print(stu1.name)
print(stu1.gender)
print(stu1.nationality)
print(stu1.native_place)
print(stu1.age)

成员方法

成员方法的定义语法

def 方法名(self,形参1....)

在方法内部,如果想要访问类的成员变量,必须使用self

调用成员方法时不用理self,它不占用形参的位置

class Student:
    name = None
    gender = None
    nationality = None
    native_place = None
    age = None

    def say_hi(self):
        print(f"HELLO WORLD,我是{self.name},今天来点大家想看的东西")

stu1 = Student()
stu1.name = "牛魔"
stu1.gender = "男"
stu1.nationality = "中国"
stu1.native_place = "广东省"
stu1.age = 31


stu1.say_hi()

魔术方法

__init\_\_方法

既有赋值的功能,也有定义的功能,但同样不能忘记两个下划线和self

class Student:
 # name = None
 # gender = None
 # nationality = None
 # native_place = None
 # age = None

 def __init__(self,name,gender,nationality,native_place,age):
     self.name = name
     self.gender = gender
     self.nationality = nationality
     self.native_place = native_place
     self.age = age

 def say_hi(self):
     print(f"HELLO WORLD,我是{self.name},今天来点大家想看的东西")

 stu2 = Student("牛魔",'男',"中国","广东省",18)
 print(stu2.name)
 print(stu2.gender)
 print(stu2.nationality)
 print(stu2.native_place)
 print(stu2.age)
 stu2.say_hi()

__str\_\_方法

class Student:
    # name = None
    # gender = None
    # nationality = None
    # native_place = None
    # age = None

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def __str__(self):
        return f"{self.name},{self.age}"

    def say_hi(self):
        print(f"HELLO WORLD,我是{self.name},今天来点大家想看的东西")

stu = Student("牛魔",18)
print(stu)
print(str(stu))

__It\_\_方法

class Student:
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def __lt__(self, other):
        return self.age < other.age

stu1 = Student("牛魔",18)
stu2 = Student("CB",19)
print(stu1 > stu2)
print(stu1 < stu2)

__Ie\_\_方法

class Student:
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def __lt__(self, other):
        return self.age < other.age

    def __le__(self, other):
        return self.age <= other.age

stu1 = Student("牛魔",18)
stu2 = Student("CB",19)
print(stu1 >= stu2)
print(stu1 <= stu2)
print(stu1 < stu2)
print(stu1 > stu2)

__eq\_\_方法

class Student:
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def __lt__(self, other):
        return self.age < other.age

    def __le__(self, other):
        return self.age <= other.age

    def __eq__(self, other):
        return self.age == other.age

stu1 = Student("牛魔",18)
stu2 = Student("CB",18)
print(stu1 >= stu2)
print(stu1 <= stu2)
print(stu1 < stu2)
print(stu1 > stu2)
print(stu1 == stu2)

封装

将现实世界中的事务描述为类的属性与方法的过程

私有成员

定义私有成员的方法:在变量或者方法名以两个下划线开头

类的其他成员可以访问私有成员的

为了提供仅供内部使用的属性和方法,不对外开放

class Phone:

    __current_voltage = 0

    def call_by_5g(self):
        if self.__current_voltage >= 1:
            print("打开5G")
        else:
            self.__keep_single_core()
            print("电量不足")

    def __keep_single_core(self):
        print("单核运行")

phone = Phone()
phone.call_by_5g()
# phone.__keep_single_core()
# print(phone.__current_voltage)

继承

分为单继承与多继承

# 单继承
class Phone:
    IMSI = None
    producer = 'ys'

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

class Phone2022(Phone):
    face_id = '1001'

    def call_by_5g(self):
        print("5G")

phone = Phone2022()
phone.call_by_4g()
phone.call_by_5g()
# 多继承
class Phone:
    IMSI = None
    producer = 'ys'

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

class NFCReader:
    nfc_type = '第五代'
    producer = "牛魔"

    def read_card(self):
        print("读卡")
    def write_card(self):
        print("写卡")

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()


print(phone.producer) #输出同名属性时,左边的父类优先(先到先得)

复写

在子类中重写父类属性内容或方法

复写后调用将使用新的属性值或方法

class Phone:
    IMSI = None
    producer = 'ys'

    def call_by_4g(self):
        print("4G")
        
class NewPhone(Phone):
    producer = "牛魔"

    def call_by_4g(self):
        print("5G")

phone = NewPhone()
print(phone.producer)
phone.call_by_4g()

超类

重写后还想调用原来的可以参见以下

class Phone:
    IMSI = None
    producer = 'ys'

    def call_by_4g(self):
        print("开4G")
        
class NewPhone(Phone):
    producer = "牛魔"

    def call_by_4g(self):
        # 方法1
        # print(f"{Phone.producer}")
        # Phone.call_by_4g(self) #self不能忘
        
        # 方法2
        print(f"{super().producer}")
        super().call_by_4g()
        print("开5G")

phone = NewPhone()
print(phone.producer)
phone.call_by_4g()

多态

指的是多种状态,完成某个行为时,使用不同的对象会得到不同的状态

抽象类:包含没有具体实现的抽象方法,提供顶层设计的类

class Animals():
    def speak(self):
        pass
        # 用父类来确定有哪些方法
        # 具体方法由子类实现
        # 这种就是抽象类或者称之为接口
        
class Dog(Animals):
    def speak(self):
        print("汪汪汪")

class Cat(Animals):
    def speak(self):
        print("喵喵喵")

def make_noise(animals:Animals):
    animals.speak()

dog = Dog()
cat = Cat()

make_noise(dog)
make_noise(cat)

类型注解

标记数据类型,方便IDE和自己指定值的类型

基础注解

# 基础数据类型注解
var_1:int = 10
var_2:str = "str"
var_3:bool = True

# 类对象注解
class Student:
    pass
stu:Student = Student()

# 基础容器类型注解
my_list:list = [1,2,3]
my_tuple:tuple = ("牛魔",2,True)
my_set:set = {"牛魔",2,True}
my_dict:dict = {"牛魔":15}

# 容器类型详细注解
you_list:list[int] = [1,2,3]
you_tuple:tuple[str,int,bool] = ("牛魔",2,True)
you_set:set[str,int,bool] = {"牛魔",2,True}
you_dict:dict[str,int] = {"牛魔":15}

# 在注释内进行类型注解
import random
import json
var_4 = random.randint(1,10)   # type:int
var_5 = json.loads('{"name": "zhangsan"}')   # type:dict[str,str]
def func():
    pass
var_6 = func()  # type:None

# 函数形参进行注解
def add(x: int,y: int):
    return x+y

add(1,2)

# 函数返回值进行注解
def func(data: list) -> list:
    return data

print(func(1))

注解仅仅是提示性的,不是决定性的

var_1:int = 'str'
var_2:str = 2
print(var_1)
print(var_2)

Union类型

这是一种联合类型

# Union演示
from typing import Union

my_list:list[Union[int,str]] = [1,2,"原神","牛魔"]

def func(data:Union[int,str]) -> Union[int,str]:
    pass
func()

发表评论