zoukankan      html  css  js  c++  java
  • Python流程控制与函数

    if

    >>> x = int(raw_input("Please enter an integer:")) 
    Please enter an integer:42
    >>> if x < 0:
    ...     x = 0
    ...     print "变为0"
    ... elif x == 0:
    ...     print "0"
    ... elif x == 1:
    ...     print "Single"
    ... else:
    ...     print "More"
    ... 
    More
    
    

    for

    # Measure some strings:
    a = ['cat', 'window', 'defenestrate']
    for x in a:
        print x, len(x)
    

    range() 函数

    >>> range(10)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> range(5,10)
    [5, 6, 7, 8, 9]
    >>> range(0,10,3)
    [0, 3, 6, 9]
    
    
    a = ['PHP', 'Java', 'Python']
    for i in range(len(a)):
        print i, a[i]
    

    break continue

    break用于跳出最近的一个for

    定义函数

    >>> def fib(n):
    ...     a,b = 0,1
    ...     while a<n:
    ...             print a,
    ...             a,b = b,a+b
    ... 
    >>> fib(100)
    0 1 1 2 3 5 8 13 21 34 55 89
    >>> f = fib
    >>> f(100)
    0 1 1 2 3 5 8 13 21 34 55 89
    

    带参数的函数

    def ask_ok(prompt, retries=4, complaint='Yes or no,please!'):
        while True:
            ok = raw_input(prompt)
            if ok in ('y', 'ye', 'yes'):
                return True
            if ok in ('n', 'no'):
                return False
            retries = retries - 1
            if retries < 0:
                raise IOError('refuse user')
            print complaint
    
    
    ask_ok('yes', 5, 'Yes or no!')
    
    
  • 相关阅读:
    Graphics竖排打印字体
    VC GetDlgItem
    C# FontStyle
    VC弹出"选择文件"和"选择文件夹"对话框(转)
    VC获取应用程序路径
    SqlServer延时函数
    volatile修饰变量
    外部命令和内部命令
    curl命令简单使用
    close_socket断开连接的方式
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/9903741.html
Copyright © 2011-2022 走看看