zoukankan      html  css  js  c++  java
  • Python for loop and while loop

    #!pyton2
    #-*- coding:utf-8 -*-
    
    for letter in "Python":
        print "Current letter is:",letter
        
    fruits=["apple","mango","pear"]
    
    for fruit in fruits:
        print fruit
        
    for index in range(len(fruits)):
            print fruits[index]
    
    > python2 test.py
    Current letter is: P
    Current letter is: y
    Current letter is: t
    Current letter is: h
    Current letter is: o
    Current letter is: n
    apple
    mango
    pear
    apple
    mango
    pear

    numbers=[1,2,3,4,5] odd=[] even=[] while len(numbers)>0: number=numbers.pop() if number%2==0: even.append(number) else: odd.append(number) print "numbers: ",numbers print "odd :" ,odd print "even:",even numbers: [] odd : [5, 3, 1] even: [4, 2]
    #break and continue

    i=1 while i<10: i+=1 if i%2 >0: continue print i i=1 while 1: print i i+=1 if i>4: break 2 4 6 8 10 1 2 3 4
    #while,else
    
    while count<3:
        print count," is less than 3"
        count+=1
    else:
        print count, "is not less than 3"
    
    #死循环    
    flag=1
    while flag==1:
        print "True!"
    
    
    0  is less than 3
    1  is less than 3
    2  is less than 3
    3 is not less than 3
    True!
    True!
    True!
    True!
    True!
    True!
    True!
    #!python2
    #-*- coding:utf-8 -*-
    
    def test_fun(num,step):
        i=0
        numbers=[]
        while i<num:
            print "At the top i is %d " %i
            numbers.append(i)
        
            i=i+step
            print "Numbers now: ",numbers
            print "At the bottom i is %d " % i
        
        print "The numbers:"
        for n in numbers:
            print n
            
    def test_fun2(num,step):
        numbers=[]
        for i in range(0,num,step):
            print "At the top i is %d" %i
            numbers.append(i)
            print "Numbers now:", numbers
            
        print "The numbers:"
        for index in range(len(numbers)):
            print numbers[index]
            
    
    def find_prime(start_num,end_num):
        for num in range(start_num,end_num):
            for i in range(2,num):
                if num%i==0:
                    print "%d = %d * %d" %(num,i,num/i)
                    break;        
            else:
                print "%d is a prime" % num
            
    
    > python2
    Enthought Canopy Python 2.7.11 | 64-bit | (default, Jun 11 2016, 11:33:47) [MSC v.1500 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from ex33_2 import *
    >>> find_prime(10,20)
    10 = 2 * 5
    11 is a prime
    12 = 2 * 6
    13 is a prime
    14 = 2 * 7
    15 = 3 * 5
    16 = 2 * 8
    17 is a prime
    18 = 2 * 9
    19 is a prime
    >>> test_fun(8,4)
    At the top i is 0
    Numbers now:  [0]
    At the bottom i is 4
    At the top i is 4
    Numbers now:  [0, 4]
    At the bottom i is 8
    The numbers:
    0
    4
    >>> test_fun2(8,4)
    At the top i is 0
    Numbers now: [0]
    At the top i is 4
    Numbers now: [0, 4]
    The numbers:
    0
    4
  • 相关阅读:
    【poj3294】 Life Forms
    【poj3415】 Common Substrings
    【poj3693】 Maximum repetition substring
    【bzoj2034】 2009国家集训队—最大收益
    【bzoj4198】 Noi2015—荷马史诗
    【poj2406】 Power Strings
    【ural1297】 Palindrome
    【spoj SUBST1】 New Distinct Substrings
    【poj1743】 Musical Theme
    django rest framework serializers小结
  • 原文地址:https://www.cnblogs.com/dadadechengzi/p/6225884.html
Copyright © 2011-2022 走看看