Python笔记(1)----条件、循环语句

1,if条件语句


    if 判断条件:
         执行语句
    else:
         执行语句


num = 3
if num < 5:
    print "yes"
else:
    print "no




条件较多时可以用or或者and,and(同时成立)、or(一个成立即可)。


if (num > 1 and num < 5) or (num > 6 and num < 9):
    print "yes"
else:
    print "no"


2,while循环语句
 
 while 判断条件:
      执行语句


count = 0
while (count < 9):
    print"the count is:",count
    count = count + 1
print "good bye"


while语句的continue、break命令


i=1
while i <10:
    i +=1
    if i%2>0:
        continue
    print i


i=1
while 1:
    print i
    i+=1
    if i>10:
        break


while 无线循环


v = 1
while v == 1:
    num = raw_input("Enter a number : ")
    print "You entered: ", num


while.....else循环


c = 0
while c < 5:
    print c, "is less than 5"
    c = c + 1
else:
    print c, " is not less than 5"


3,for循环
  
for interating_var in sequence:
    statements(s)


fruits = ['banana','apple','mango']
for fruit in fruits:
    print "current fruit:",fruit




通过索引执行循环


fruits = ['banana','apple','mango']
for index in range(len(fruits)):
    print "current fruit:",fruits[index]


注:range()返回一个序列数,len()返回列表的长度。


for....else


for num in range(10,20):
    for i in range(2,num):
        if num % i == 0:
            j = num/i
            print "%d equal %d * %d" % (num ,i,j)
            break
    else:
        print num, "is a prime number"


4,循环嵌套


i = 2
while(i < 100):
    j = 2
    while(j <= (i/j)):
        if not(i%j):
            break
        j = j + 1
    if (j > i/j):
          print i, "is a prime number"
    i = i + 1
print "Good bye!"


pass语句


for letter in 'Python':
   if letter == 'h':
      pass
      print 'this is pass block'
   print 'current letter :', letter


print "Good bye!"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值