
Python Note

东拼西凑的
python初学之路。
字符串
- 制表符:
\t - 换行符:
\n - 去除右边空白:
.rstrip() - 去除左边空白:
.lstrip() - 去除左右两边空白(包括换行符):
.strip() - 字符串内使用引号:
\" - 首字母大写:
.title() - 全部大写:
.upper() - 全部小写:
.lower() - 构造字符串(花括号内为变量):`f"Hello,{variant}."
- 去除前缀:
.removeprefix("string") - 去除后缀:
.removesuffix(".txt")
运算
- 乘法:
* - 除法(返回浮点数):
/ - 大数易读:
1_000_000_000 - 同时赋值:
x, y, z = 0, 2, 7 - 同时输出:
print(x, y, z) - 幂次:
** - Python 之禅:
import this
列表
定义:方括号扩死,里面元素用逗号隔开。
下标和数组一样,从零开始。
负数下标表示倒数第几个。
- 末尾插入元素:
.append() - 对应位置插入元素:
.insert(1, 'fgh') - 删除对应位置的元素:
del b[1] - 弹出末尾元素:
.pop() - 访问弹出元素:
a = b.pop() - 弹出对应位置的元素:
.pop(1) pop和del的区别在于pop可访问删除的元素,del不行。- 根据值删除一个元素:
.remove("spw") - 排序(列表中元素类型要一样才能排序,按照字典序排序):
.sort() - 倒序:
.sort(reverse = True) - 临时排序,不改变列表,可加入参数:
sorted(b, reverse = True) - 访问列表中元素个数:
len(b)
- Title: Python Note
- Author: Falling_Sakura
- Created at : 2023-12-05 10:49:34
- Updated at : 2024-11-21 10:44:39
- Link: https://vercel.fallingsakura.top/772e1c59.html
- License: This work is licensed under CC BY-NC-SA 4.0.
Comments