python3学习笔记十(循环语句)

参考http://www.runoob.com/python3/python3-loop.html

  • 循环语句

while循环

# !/usr/bin/env python3

n = 100

sum = 0
counter = 1
while counter <= n: #while循环
sum = sum + counter
counter += 1
print("1 到 %d 之和为: %d" % (n,sum))


var = 1
while var ==1: #表达式永远为true
num = int(input("输入一个数字: "))
print("你输入的数字是: ",num)
print("Good Bye!") #使用 CTRL+C 来退出当前的无限循环

while 循环使用 else 语句

count = 0
while count <5: #while循环使用else语句,条件语句为false时执行else语句
print(count,"小于5")
count = count +1
else:
print(count,"大于或等于5")

for语句

languages = ['c','c++','perl','python']

for x in languages:

  print(x)

range函数

for i in range(5):    

  print(i)

#range(3,9)指定区间的值,range(1,10,2)range以指定数字开始并指定不同的增量(甚至可以是负数,有时这也叫做'步长')






猜你喜欢

转载自www.cnblogs.com/hc1hr2/p/10180888.html
今日推荐