1.分析下面代码的运行结果?
x = 10
y = x # y = 10; x=10
y = 20 # y = 20; x=10
print(x)
print(y)
2. 用print打印出下面内容:
文能提笔安天下,
武能上马定乾坤。
心存谋略何人胜,
古今英雄唯是君。
s = """文能提笔安天下,
武能上马定乾坤。
心存谋略何人胜,
古今英雄唯是君。
"""
print(s)
print('文能提笔安天下,\n武能上马定乾坤。\n心存谋略何人胜,\n古今英雄唯是君。')
“”"
3. 利用 input函数,连续输入两个数字求和?
“”"
n1 = input('>>: ').strip()
n2 = input('>>: ').strip()
print(int(n1) + int(n2))
n1 = int(input('>>: ').strip())
n2 = int(input('>>: ').strip())
print(n1 + n2)
while True:
n1 = input('>>: ').strip()
n2 = input('>>: ').strip()
if n1.isdecimal() and n2.isdecimal():
n1 = int(n1)
n2 = int(n2)
print(n1 + n2)
break
else:
print('必须输入整数!!!')
“”"
4. 分别使用%占位符以及format方法两种方式制作趣味模板程序需求:
等待用户输名字、地址、爱好,
根据用户的名字和爱好进任意格式化输出
如:敬爱可亲的xxx,最喜欢在xxx地方法xxx
“”"
n1 = input('>>: ').strip()
n2 = input('>>: ').strip()
n3 = input('>>: ').strip()
print("敬爱可亲的%s,最喜欢在%s地方做%s" % (n1, n2, n3))
print("敬爱可亲的{},最喜欢在{}地方做{}".format(n1, n2, n3)) # 推荐使用format
“”"
5. 有 names = " 张三 李四 王五 赵六 " 将names字符串中所有的名字放在一个列表中
“”"
names = " 张三 李四 王五 赵六 "
print(names.split()) # 以空为分割
print(names.strip().split(" ")) # 以空格分割
“”"
6. 查找字符串" 张三 李四 王五 赵六 "王五的索引位置
“”"
names = " 张三 李四 王五 赵六 "
print(names.find('王五'))
print(names.index('王五'))
#find 找到值返回对应的下标,找不到返回 -1
#index 找到值返回对应的下标,找不到报错 ValueError
“”"
7. 将十进制1025分别转换为二进制,八进制以及十六进制
“”"
i = 1025
print('10 --> 2: ', bin(i))
print('10 --> 8: ', oct(i))
print('10 --> 16: ', hex(i))
“”"
打印结果:
10 --> 2: 0b10000000001
10 --> 8: 0o2001
10 --> 16: 0x401
“”"
“”"
8. 将"goods"与"food"以及"meat"拼接为完整路径,即"/goods/food/meat/"
“”"
s1 = "goods"
s2 = "food"
s3 = "meat"
result = '/' + s1 + '/' + s2 + '/' + s3 + '/'
print(result)
print('/' + '/'.join([s1, s2, s3]) + '/')
“”"
9. s = "hello world"切片操作
(1) s[1:4]
(2) s[-1:-4]
(3) 打印"world"如何切片
切片:
– 顾头不顾尾
– 默认从左往右切
“”"
s = "hello world"
# s[1:4] # ell
print(s[1:4])
# s[-1:-4] # 切出来是个空
print(s[-1:-4])
print(s[-1:-4:1]) # 默认步长为1
print(s[-1:-4:-1]) # -1表示从右往左挨着取值
# 打印"world"如何切片
print(s[6:]) # print(s[6::1])
print(s[-5:]) # print(s[-5::1])
“”"
10. “1” == 1的结果是什么?结果是什么数据类型
结果:
False,bool类型
“”"