for while . break:退出循环 continue:退出本次循环 例子 for i range(0,101,2): print(i) -------------------------------------------------------------------------------- while n<=100: if (n>10): break prin t(i) n = n + 1 print ('end') 小结 循环是让计算机做重复任务的有效的方法. break语句…
1.使用while循环输入1234568910 n = 1 while n < 11: if n == 7: pass else: print(n) n = n + 1 2.求1 - 100的所有数的和 n = 1 s = 0 while n < 101: s = s + n n = n + 1 print(s) 3.输出1 - 100内的所有奇数 n = 1 while n < 101: temp = n % 2 if temp == 0: pass else: print(n) n…
for循环格式: for index in range(0,3):#等同于range(3),取0\1\2 print(index) index = 0 starnames = ['xr1','xr2','xr3'] for index in range(len(starnames)): print(starnames[index]) 结果: xr1xr2xr3 range函数: range(1,5) 取1-4 range(1,5,2) 取1-4,1是起始下标,5是终止下标,步长为2 range(…
注:运行环境 Python3 1.循环语句 (1)for循环 注:for i in range(a, b): #从a循环至b-1 for i in range(n): #从0循环至n-1 import numpy as np # 导入NumPy库 if __name__ == "__main__": , ): #从1循环至2 print("i=",i) #打印i值 输出: i= 1i= 2 (2)while循环 import numpy as np #…
1.使用while循环输入 1 2 3 4 5 6 8 9 10 n = 1 while n < 11: if n == 7: pass else: print(n) n = n + 1 2.求1-100的所有数的和 n = 1 s = 0 while n < 101: s = s + n n = n + 1 print(s) 3.输出 1-100 内的所有奇数 n = 1 while n < 101: temp = n % 2 if temp == 1: print(n) el…