时隔一年,再次学习python。
range函数是 “取头不取尾”
#用于计算组合数
def f1(m): #累乘
temp = 1
for i in range(2,m+1):
temp *= i
return temp
def f2(n, m): #计算组合数
return f1(n) // (f1(m) * f1(n - m))
print(f"C(5,2)的结果是{f2(5, 2)}")
可变参数函数
def func(*args): #这里args为元组
print(f"func1的参数个数是:{len(args)}")
print(args) #python里print可以直接打印
func1(1, 2, 3)
func1(1, 2, 3, 4, 5, 6, 7, 8, 9)
func1()
func1("hello", "world")
def fuc2(**kargs): #这里kargs是字典
__ name __ == “__ main __” 的作用:
#hello.py
print(__name__);
if __name__ = "__main__":
print("Hello world")
#test.py
import hello
print("import from hello.py")
运行test.py的运行结果是: hello
而直接运行hello.py的结果是:hello
Hello world
因为在本文件内__name__是__main__,而import的文件,__name__就是文件名
生成随机验证码,包含random和string的用法
import random
import string
def generate_random_code(length=6):
characters = string.ascii_letters + string.digits #
code = ''.join(random.choice(characters) for _ in range(length)) #这里for _ in range() 用于控制次数
return code
code_length = 4
code = generate_random_code(code_length)
print(f"The verification code is {code}")
高阶函数
def cal(*args, **kargs): #可进行任意数的求和
result = 0
items = list(args) + list(kargs)
for item in items:
result += item
return result
函数返回值
#如果没有返回值则默认是None
def say_hi():
print('Hi')
a = say_hi
print(f"函数的返回值是{a}")
print(f"函数返回值的类型是{type(a)}")
#输出结果如下
'''
函数的返回值是None
函数返回值的类型是<class 'NoneType'>
'''
def check_age(age):
if age < 0:
return "Age cannot be negative."
elif age < 18:
return None
else:
return "Success"
result = check_age(20)
if not result:
print("Age is under 18.")
print(result)
列表(List)的方法
| 编号 | 使用方式 | 作用 |
|---|---|---|
| 1 | 列表.append(元素) |
向列表中追加一个元素 |
| 2 | 列表.extend(容器) |
将数据容器的内容依次取出,追加到列表尾部 |
| 3 | 列表.insert(下标, 元素) |
在指定下标处,插入指定的元素 |
| 4 | del 列表[下标] |
删除列表指定下标元素 |
| 5 | 列表.pop(下标) |
删除列表指定下标元素 |
| 6 | 列表.remove(元素) |
从前向后,删除此元素第一个匹配项 |
| 7 | 列表.clear() |
清空列表 |
| 8 | 列表.count(元素) |
统计此元素在列表中出现的次数 |
| 9 | 列表.index(元素) |
查找指定元素在列表的下标,找不到报错ValueError |
| 10 | len(列表) |
统计容器内有多少元素 |
字符串是不可修改的数据库容器
字符串的方法
| 编号 | 操作 | 说明 |
|---|---|---|
| 1 | 字符串[下标] |
根据下标引索出特定位置字符 |
| 2 | 字符串.index(字符串) |
查找给定字符串的第一个匹配项的下标 |
| 3 | 字符串.replace(字符串1, 字符串2) |
将字符串内的全部字符串1,替换为字符串2 不会修改原字符串,而是得到一个新的字符串 |
| 4 | 字符串.split(字符串) |
按照给定字符串,对字符串进行分隔 不会修改原字符串,而是得到一个新的列表 |
| 5 | 字符串.strip()字符串.strip(字符串) |
移除首尾的空格和换行符或指定字符串 |
| 6 | 字符串.count(字符串) |
统计字符串内某字符串的出现次数 |
| 7 | len(字符串) |
统计字符串的字符个数 |
具体应用:
#字符串不能通过方法修改
char = "hello world wto ciallo ciallo"
print(f"world的第一个字符是{char.index("world")}")
char.replace("ciallo","hello")
print(char)
list_from_char = char.split(" ")
print(list_from_char)
print(f"去掉hello后的字符串是{char.strip('hello')}")
print("字符串长度是:", len(char))
print("字符串的w个数是:", char.count("w"))
序列的切片
s = "Hello,Python"
print(s[0:5]) # Hello
print(s[6:12]) # Python
print(s[::-1]) # nohtyP,olleH
p = [0,1,2,3,4,5,6,7,8,9]
print(s[::2]) # 打印偶数列
print(s[1::2]) # 打印偶数列
s[::-1]用于反转序列 因为步长为负反向取·
字典操作
| 编号 | 操作 | 说明 |
|---|---|---|
| 1 | 字典[Key] |
获取指定Key对应的Value值 |
| 2 | 字典[Key] = Value |
添加或更新键值对 |
| 3 | 字典.pop(Key) |
取出Key对应的Value并在字典内删除此Key的键值对 |
| 4 | 字典.clear() |
清空字典 |
| 5 | 字典.keys() |
获取字典的全部Key,可用于for循环遍历字典 |
| 6 | len(字典) |
计算字典内的元素数量 |