登录网站,浏览更多精彩内容
您需要 登录 才可以下载或查看,没有账号?加入我们
×
007.关键词错误
python关键词不能作为变量来使用
010.序列切片
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
print(letters[3:6])
3和6都是下标,包含3的元素,不包含6的元素
011.序列前位置切片
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
print(letters[:3])
:3 是 0:3的简写
013.负数切片
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
print(letters[-3:])
-3:表示 -3:-1的简写
014.序列索引步长
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
print(letters[::2])
序列切片的完整语法,是[start:end:step]
如果不传step参数,默认是1
[:]意思是整个列表,从开始到结尾
[::2]意思是,从开始(包含)到结尾,但是步长2
018.移除重复元素
019.列表的例序排序
lista = [20, 40, 30, 50, 10]
lista.sort(reverse=True)
print("lista is", lista)
027 格式化输出
pprint(复杂数组)
030.打印字母a到z
035 同时遍历多个序列
a = [1, 2, 3]
b = (4, 5, 6)
for i, j in zip(a, b):
print(i + j)
047 字符串格式化
name = input("Enter name: ")
age = input("Enter age: ")
print("Your name is %s and your age is %s" % (name, age))
print(f"Your name is {name} and your age is {age}")
050 多分隔符单词计数
import re
def count_words(filepath):
with open(filepath, 'r') as file:
string = file.read()
string_list = re.split(r",| ", string)
print(string_list)
return len(string_list)
print(count_words("p050.txt"))
|