111111
1111吧
全部回复
仅看楼主
level 3
>>> print('{} {}'.format('hello','world')) # 不带字段
hello world
>>> print('{0} {1}'.format('hello','world')) # 带数字编号
hello world
>>> print('{0} {1} {0}'.format('hello','world')) # 打乱顺序
hello world hello
>>> print('{1} {1} {0}'.format('hello','world'))
world world hello
>>> print('{a} {tom} {a}'.format(tom='hello',a='world')) # 带关键字
world hello world
2025年09月11日 08点09分 1
level 3
tup = ('张明', '赵虎', '马汉', '李云龙', '王义')
for member in tup:
print(member)
print(tup[1:3])
new_tup = tup + ('阿Q', '张无忌')
print(new_tup)
lst = list(new_tup)
lst.pop()
tup = tuple(lst)
print(tup)
2025年09月22日 06点09分 33
level 3
lst = ['Rose', 'Jack', 'John', 'Xu', 'You', 'Black']
for item in lst:
print(item)
lst.insert(2, 'Blue')
print(lst)
lst[2] = 'hello'
print(lst)
index = lst.index('John')
print(index)
slice_2_4 = lst[2:5]
print(slice_2_4)
last_two = lst[-2:]
print(last_two)
john_index = lst.index('John')
lst = lst[:john_index + 1] + ['one', 'two'] + lst[john_index + 1:]
print(lst)
2025年09月22日 06点09分 34
level 3
t = ("骆昊", 38, True, "四川成都")
print(t)
print(t[0])
print(t[3])
for member in t:
print(member)
t = ('王大锤', 20, True, '云南昆明')
print(t)
person = list(t)
print(person)
person[0] = '李小龙'
person[1] = 25
print(person)
fruits_list = ['apple', 'banana', 'orange']
fruits_tuple = tuple(fruits_list)
print(fruits_tuple)
2025年09月22日 06点09分 35
level 3
# (1)定义列表并遍历显示
lst = ['Rose', 'Jack', 'John', 'Xu', 'You', 'Black']
for item in lst:
print(item)
# (2)在位置2插入“Blue”
lst.insert(2, 'Blue')
print(lst)
# (3)修改位置2的内容为“hello”
lst[2] = 'hello'
print(lst)
# (4)返回列表中'John'的索引值
index = lst.index('John')
print(index)
# (5)取出列表中索引2-4的元素
print(lst[2:5])
# (6)取出列表后2个元素
print(lst[-2:])
# (7)在John后面插入["one","two"]
john_index = lst.index('John')
lst = lst[:john_index + 1] + ['one', 'two'] + lst[john_index + 1:]
print(lst)
2025年09月22日 06点09分 36
level 3
# (1)定义字典
my_dict = {'a': 1, 'b': 2, 'b': '3', 'd': '4'}
# (2)输出元素个数并遍历所有元素
print("元素个数:", len(my_dict))
for key, value in my_dict.items():
print(f"{key}: {value}")
# (3)打印字典中key为b的内容
print("key为b的内容:", my_dict['b'])
# (4)删除字典中key为c的元素
if 'c' in my_dict:
del my_dict['c']
# (5)添加key为"e",值为"5"的元素
my_dict['e'] = '5'
print(my_dict)
2025年09月22日 08点09分 37
level 3
# 创建字典的字面量语法
scores = {'骆昊': 95, '白元芳': 78, '狄仁杰': 82}
print(scores)
# 创建字典的构造器语法
items1 = dict(one=1, two=2, three=3, four=4)
# 通过zip函数将两个序列压成字典
items2 = dict(zip(['a', 'b', 'c'], '123'))
# 创建字典的推导式语法
items3 = {num: num ** 2 for num in range(1, 10)}
print(items1, items2, items3)
# 通过键可以获取字典中对应的值
print(scores['骆昊'])
print(scores['狄仁杰'])
# 对字典中所有键值对进行遍历
for key in scores:
print(f'{key}: {scores[key]}')
# 更新字典中的元素
scores['白元芳'] = 65
scores['诸葛王朗'] = 71
scores.update(冷面=67, 方启鹤=85)
print(scores)
if '武则天' in scores:
print(scores['武则天'])
print(scores.get('武则天'))
# get方法也是通过键获取对应的值但是可以设置默认值
print(scores.get('武则天', 60))
# 删除字典中的元素
print(scores.popitem())
print(scores.popitem())
print(scores.pop('骆昊', 100))
# 清空字典
scores.clear()
print(scores)
2025年09月22日 08点09分 38
level 3
# 定义集合
a = {11, 22, 33, 44}
d = {55, 22, 66, 44}
# (1)在a集合中添加55元素
a.add(55)
# (2)删除a集合中22元素
a.remove(22)
# (3)对a和d集合分别进行交、并操作,并打印结果
intersection = a & d
union = a | d
print("交集:", intersection)
print("并集:", union)
2025年09月25日 06点09分 39
level 3
# 创建集合的字面量语法
set1 = {1, 2, 3, 3, 3, 2}
print(set1)
print("Length =", len(set1))
# 创建集合的构造器语法(面向对象部分会进行详细讲解)
set2 = set(range(1, 10))
set3 = set({1, 2, 3, 3, 2, 1})
print(set2, set3)
# 创建集合的推导式语法(推导式也可以用于推导集合)
set4 = {num for num in range(1, 100) if num % 3 == 0 or num % 5 == 0}
print(set4)
# 向集合添加元素和从集合删除元素
set1.add(4)
set1.add(5)
set2.update([11, 12])
set2.discard(5)
if 4 in set2:
set2.remove(4)
print(set1, set2)
print(set3.pop())
print(set3.pop())
print(set3)
# 集合的交集、并集、差集、对称差运算
print(set1 & set2)
# print(set1.intersection(set2))
print(set1 | set2)
# print(set1.union(set2))
print(set1 - set2)
# print(set1.difference(set2))
print(set1 ^ set2)
# print(set1.symmetric_difference(set2))
# 判断子集和超集
print(set2 <= set1)
# print(set2.issubset(set1))
print(set3 <= set1)
# print(set3.issubset(set1))
print(set1 >= set2)
# print(set1.issuperset(set2))
print(set1 >= set3)
# print(set1.issuperset(set3))
2025年09月25日 06点09分 40
level 3
s = "http://www.cztgi.edu.cn/"
# 1)统计字母c出现的次数
print("字母c出现的次数:", s.count('c'))
# 2)查找"edu"子串的位置
print("'edu'子串的位置:", s.find('edu'))
# 3)将所有"."替换为","
print("替换后的字符串:", s.replace('.', ','))
# 4)字母全变为大写
print("大写字符串:", s.upper())
# 5)输出总字符个数
print("总字符个数:", len(s))
# 6)拼接子串"index"
s += "index"
print("拼接后的字符串:", s)
2025年09月25日 08点09分 41
level 3
s = input("请输入字符串:")
count = 0
for char in s:
if char.isdigit():
count += 1
print("数字的数量为:", count)
2025年09月25日 08点09分 42
level 3
str1 = 'hello, world!'
# 通过内置函数len()计算字符串的长度
print(len(str1)) # 13
# 获得字符串首字母大写的拷贝
print(str1.capitalize()) # Hello, world!
# 获得字符串每个单词首字母大写的拷贝
print(str1.title()) # Hello, World!
# 获得字符串变大写后的拷贝
print(str1.upper()) # HELLO, WORLD!
# 从字符串中查找子串所在位置
print(str1.find('ll')) # 2
print(str1.find('or')) # 8
print(str1.find('shit')) # -1
# 与find类似但找不到子串时会引发异常
# print(str1.index('shit'))
# 检查字符串是否以指定的字符串开头
print(str1.startswith('He')) # False
print(str1.startswith('hel')) # True
# 检查字符串是否以指定的字符串结尾
print(str1.endswith('!')) # True
# 将字符串以指定的宽度居中并在两侧填充指定的字符
print(str1.center(50, "*"))
# 将字符串以指定的宽度靠右放置左侧填充指定的字符
str2 = 'abc123456'
# 检查字符串是否由数字构成
print(str2.isdigit()) # False
# 检查字符串是否以字母构成
print(str2.isalpha()) # False
# 检查字符串是否以数字和字母构成
print(str2.isalnum()) # True
str3 = ' jackfrued@网页链接 '
# 获得字符串修剪左右两侧空格之后的拷贝
print(str3.strip())
2025年09月25日 08点09分 43
level 3
s1 = 'hello ' * 3
print(s1) # hello hello hello
s2 = 'world'
s1 += s2
print(s1) # hello hello hello world
print('ll' in s1) # True
print('good' in s1) # False
str2 = 'abc123456'
# 从字符串中取指定位置的字符(下标运算)
print(str2[2]) # c
# 字符串切片(从指定的开始索引到指定的结束索引)
print(str2[2:5]) # c12
print(str2[2:]) # c123456
print(str2[2::2]) # c246
print(str2[2:2]) #
print(str2[2:-2]) # ac246
print(str2[::-1]) # 654321cba
print(str2[-3:-1]) # 45
2025年09月25日 08点09分 44
level 3
s1 = 'hello, world!'
s2 = "hello, world!"
# 以三个双引号或单引号开头的字符串可以换行
s3 = """
hello,
world!
"""
print(s1, s2, s3, end="")
s1 = "\"hello, world!\""
s2 = "\n\t\\hello, world!\\\\\n"
print(s1, s2, end="")
s1 = '\141\142\143\x61\x62\x63'
s2 = '\u9a86\u660a'
print(s1, s2)
2025年09月25日 08点09分 45
level 3
# 第一个函数:calculate
def calculate(num):
if num % 2 == 0:
print(num / 2)
else:
print(3 * num + 1)
calculate(9)
calculate(8)
# 第二个函数:sumStartToEnd
def sumStartToEnd(start, end):
he = 0
for i in range(start, end + 1):
he += i
return he
print("start到end之间的和为:", sumStartToEnd(1, 100))
2025年09月26日 07点09分 46
1 2 尾页