第二次作业
1.九九乘法表
a = 1
while a <= 9:
i = 1
while i <= a:
print(f'{i}*{a}={a+i}', end='\t')
i += 1
print()
a += 1
1*1=2
1*2=3 2*2=4
1*3=4 2*3=5 3*3=6
1*4=5 2*4=6 3*4=7 4*4=8
1*5=6 2*5=7 3*5=8 4*5=9 5*5=10
1*6=7 2*6=8 3*6=9 4*6=10 5*6=11 6*6=12
1*7=8 2*7=9 3*7=10 4*7=11 5*7=12 6*7=13 7*7=14
1*8=9 2*8=10 3*8=11 4*8=12 5*8=13 6*8=14 7*8=15 8*8=16
1*9=10 2*9=11 3*9=12 4*9=13 5*9=14 6*9=15 7*9=16 8*9=17 9*9=18
2.for语句
str1 ='123456789'
for i in str1:
print(i)
1
2
3
4
5
6
7
8
9
3.break和continue
break
name ='123456789'
for a in name:
if a=='3':
print('遇到3就不输出')
break
print(a)
1
2
遇到3就不输出
contine
name ='123456789'
for a in name:
if a=='3':
print('遇到3就不输出')
continue
print(a)
1
2
遇到3就不输出
4
5
6
7
8
9
4.切片
name = "abcdef"
print(name[0])
print(name[1])
print(name[2])
a
b
c
name = "abcdef"
print(name[2:5:2])
print(name[:5])
print(name[::1])
print(name[::-1])
print(name[:-5])
print(name[-2:-5:-2])
ce
abcde
abcdef
fedcba
a
ec
name = "abcdefa"
print(name.find('b'))
print(name.find('h'))
print(name.find('b', 2, 4))
print(name.find('b', 1, 2))
print(name.count('a'))
1
-1
-1
1
2
name = "abcdef"
print(name.replace('b', 'c'))
print(name)
accdef
abcdef
name = 'hello'
print(name.ljust(10,'.'))
hello.....
5.列表
name = ['tom','lily','rose']
name[0] = 'sun'
print(name)
['sun', 'lily', 'rose']
name = ['tom', 'lily', 'rose']
print(name.index('lily', 0, 2))
print(name.index('lily', 0, 2))
print(len(name))
1
1
3
name = ['tom', 'lily', 'rose']
a = input('请输⼊您要搜索的名字:')
print('lily' not in name)
print('jack' not in name)
if a in name:
print('名字已经存在')
else:
print('名字不存在')
请输⼊您要搜索的名字:tom
False
True
名字已经存在
name = ['tom', 'lily', 'rose']
name.append('jack')
name.extend('jack')
name.insert(2 , 'eddie')
print(name)
['tom', 'lily', 'eddie', 'rose', 'jack', 'j', 'a', 'c', 'k']
name = ['tom', 'lily', 'rose', 'jack']
del name[1]
name1= name.pop(0)
name.remove('jack')
print(name)
print(name1)
['rose']
tom
name = ['tom', 'lily', 'rose', 'jack']
name.clear()
print(name)
[]
name = ['tom', 'lily', 'rose', 'jack']
name[0] = 'eddie'
name.reverse()
print(name)
['jack', 'rose', 'lily', 'eddie']
name = ['tom', 'lily', 'rose', 'jack']
name.sort()
print(name)
['jack', 'lily', 'rose', 'tom']
6.列表的循环
name = ['tom', 'lily', 'rose', 'jack']
i = 0
while i < len(name):
print(name[i])
i += 1
tom
lily
rose
jack
name = ['tom', 'lily', 'rose', 'jack']
for i in name:
print(i)
tom
lily
rose
jack