1.for常用循环
"""
for 循环的三种模式
"""
#for-1,第一种方法:简单,只能取值,不能修改
list1=['id', 'name', 'pin_yin', 'pin_yin_ad', 'group_id', 'group_code']
for item in list1:
print(item)
print("------------split------------")
# for-2,第二种方法:for...in..range
for index in range(len(list1)):
print("索引号:{},值:{}".format(index,list1[index]))
print("------第三个------")
# for-3,第三种方法:for --- in enumerate
for index,item in enumerate(list1):
print("索引号:{}".format(index),end="\t")
print("值:{}".format(item))
2.dict字典常用方式
"""
dict 字典的常用取值方法
"""
dict01={
"num":1,"name":"tom","address":"chinese"}
print(dict01.keys()) #把所有key存放在一个list集合中
print(dict01.values()) #把所有value存放在一个list集合中
print(dict01.items()) #遍历key-value整体
#1.遍历出 字典的key 以及对应的value值
for key in dict01:
print("key is :%s,"%key,end="\t")
print("value is: %s,"%dict01[key])
#2.遍历出 字典的key 以及对应的value值
print("-----------")
for key,value in dict01.items():
print("key is :%s"%key,end="\t")
print("value is :%s"%value)
3.while常用方法
import random
def get_numbers(start:int,end:int,number:int):
"""
函数作用:输入整数起始、结束的数值,以及一个生成多少个数字的number数字
作用:在start与end的整数之间生成一个number个数字的列表
:param start:
:param end:
:param number:
:return:
"""
# 定义一个结合存储结果
numbers=[]
while True:
# 获取随机值[值的范围是上面的函数输入的参数范围]
temp=random.randint(start,end)
# 判断当前数字是否在列表中,如果不再就是添加到列表中
if temp not in numbers:
numbers.append(temp)
# 判断数量是否满足要求
if len(numbers)==number:
break
return numbers
if __name__ == '__main__':
listrange1=get_numbers(100,200,3)
print(listrange1)