favorite_places={'张三':['上海','广州','深圳'],'李四':['九寨沟','张家界','张++']}
name=input('请输入名字')
for k in favorite_places:
if(name==k):
print(favorite_places[name])
//当我们要遍历字典中值的时候,加上values()函数,代码如下:
favorite_places={'张三':['上海','广州','深圳'],'李四':['九寨沟','张家界','张++']}
name=input('请输入名字')
for k in favorite_places.values():
print(k)
输出:
[‘上海’, ‘广州’, ‘深圳’]
[‘九寨沟’, ‘张家界’, ‘张++’]
**
favorite_places={'张三':['上海','广州','深圳'],'李四':['九寨沟','张家界','张++']}
for k,v in favorite_places.items():
print(k,v)
输出:
张三 [‘上海’, ‘广州’, ‘深圳’]
李四 [‘九寨沟’, ‘张家界’, ‘张++’]
k存放的是zhangsan,v存放的是李四.
用for循环打印
**
**for i in range(1,10):
for j in range(1,i+1):
print(j,"*",i,"=",i*j,end=' ') #end是默认不换行。
print()#换行。**
**
标注:python太强大了。输出结果:
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
用while循环打印:
i=1
j=1
while i<10:
while j<=i:
print(j, "*", i, "=", i * j, end=' ') # end是默认不换行。
j+=1
j=1
i+=1
print()
输出:
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
j=0;
for i in range(1,101):
j+=i;
print(j)
输出:5050
代码:
list1=[]
str1=input('请输入一个字符串')
for i in str1:
if i.isdecimal()==True: //用isdecimal()函数判断是不是数字
list1.append(int(i))
else:
list1.append(i.upper()) 将字符转换成大写
print(list1)
输出结果:
[‘A’, ‘F’, ‘D’, ‘A’, ‘F’, ‘D’, 5, 4, ‘F’, ‘D’]
代码:
list1=[]
str1=input("请输入一个八位以下的整数:")
if len(str1)<=8:
print('这是一个%d位数'%len(str1))
for v in str1:
list1.append(v)
b=reversed(list1)
print(list(b))
输出结果:
请输入一个八位以下的整数:456789
这是一个6位数
[‘9’, ‘8’, ‘7’, ‘6’, ‘5’, ‘4’]
sum=0
i=1
initia=10
while i<11:
sum+=initia
initia=initia/2
sum+=initia
i+=1
print(sum)
输出结果:
29.970703125
代码:
num=0
space1=0
alpha=0
str1=input("请输入一行字符串")
for v in str1:
if v.isalpha():
alpha+=1
elif v.isdigit():
num+=1
elif v.isspace():
space1+=1
else:
alpha+=1
num=[m[i][j] for i in range(3) for j in range(3)]
print(num)
names = [['Tom','Billy','Jefferson','Andrew','Wesley','Steven','Joe'],
'Alice','Jill','Ana','Wendy','Jennifer','Sherry','Eva']
for i in range(0,1):
for n in names[0]:
print(n)
list2=[n for i in range(0,1) for n in names[0]]
print(list2)
输出结果:
Tom
Billy
Jefferson
Andrew
Wesley
Steven
Joe
[‘Tom’, ‘Billy’, ‘Jefferson’, ‘Andrew’, ‘Wesley’, ‘Steven’, ‘Joe’]
for i in range(100,1000):
x=i//100
y=i%100//10
z=i%100%10
if(i==x**3+y**3+z**3):
print(i)
输出结果:
153
370
371
407
i='*'
j='***'
for i in range(1,8,2):
print(('*'*i).center(7))
if i==7:
for i in range(5, 0, -2):
print(('*'*i).center(7))
输出结果:
*
代码:
num=input("判断是不是回文")
if num[0]==num[4] and num[1]==num[3]:
print(num)
else:
print('bs')
方法二:
num=input("判断是不是回文")
if num==num[::-1]:
print(num)
else:
print('bs')
for x in range(1,5):
for y in range(1,5):
for z in range(1,5):
if x!=y and x!=z and y!=z:
print("这些三位数%d%d%d"%(x,y,z))
输出:
这些三位数123
这些三位数124
这些三位数132
这些三位数134
这些三位数142
这些三位数143
这些三位数213
这些三位数214
这些三位数231
这些三位数234
这些三位数241
这些三位数243
这些三位数312
这些三位数314
这些三位数321
这些三位数324
这些三位数341
这些三位数342
这些三位数412
这些三位数413
这些三位数421
这些三位数423
这些三位数431
这些三位数432
users={'张三':'123',';李四':'456'}
for i in range(3):
name =input('请输入姓名')
pwd=input('请输入密码:')
if name in users:
if pwd == users[name]:
print('登录成功,')
break;
else:
print("密码有误")
冒泡排序:
list01 = [5,65,4,1,3,8,9]
for i in range(len(list01)):
for j in range(1,len(list01)):
if list01[j]>list01[j-1]:
list01[j],list01[j-1]=list01[j-1],list01[j]
print(list01)