一、for语句的使用
1、for循环运用在列表区间
players=["Currey","Jordan","James","Durant","Obama","Obama"]
print("打印前3位球员的名称")
for player in players[0:3]:
print(player)
2、将for循环应用在数据类别的判断
names=["杨桃","杨婷","杨思","刘思","王明"]
namey=[]
for name in names:
if name.startswith("杨"):
namey.append(name)
print(namey)
结果:['杨桃', '杨婷', '杨思']
3、删除列表内所有元素
names=["杨桃","杨婷","杨思","刘思","王明"]
for name in names[:]:
names.remove(name)
print(names)
二、range函数
1、只有一个参数的range()函数
#输入数字打印出对应*的数量
n=int(input("输入星号数量:"))
for number in range(n):
print("*",end="")
结果:***
2、有2个参数的range()函数
#输入正整数n,计算从0加到n的数
n=int