前往顾页
以后地位: 主页 > 收集编程 > .Net实例教程 >

c#流程节制

时候:2014-04-19 22:21来源:知行网www.zhixing123.cn 编辑:麦田守望者

*源法度编码

# -*- coding: utf-8 -*-

*循环可以有一个else语句;它在循环迭代完整个列表(对for)或履行前提为false(对while)时履行,但循环被break中断的环境下是不会履行。以下搜刮素数的示例法度演示了这个句子:

for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print n, 'equals', x,'*', n/x
            break
    else:
   # loop fell through without finding a factor
       print n,'is a prime number'

成果以下:

2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

*Pass语句:甚么也不做

whlie true:
    pass


*函数
1.定义

def fib2(n): # return Fibonacci series up to n
    """Return a list containing the Fibonacci series up to n."""
    result = []
    a,b = 0,1
    while b < n:
        result.append(b) # see below
        a, b = b, a+b
    return result

f100 = fib2(100) # call it
print  f100 # write the result
fib3=fib2    #可重定名
print fib3
print fib3(100)

成果以下:

[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
<function fib2 at 0x00B41AB0>
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

注:如无前往值的函数,即前往None
2.参数默许值

def parrot(voltage, state='a stiff', action='voom',type='Norwegian Blue'):
    print "-- This parrot wouldn't", action,
    print "if you put", voltage, "volts through it."
    print "-- Lovely plumage, the", type
    print "-- It's", state, "!"
    
parrot(1000)
parrot(action = 'VOOOOOM',voltage =1000000)
parrot('a thousand',state = 'pushing up the daisies')
parrot('a million','bereft of life','jump')

3.可变参数

def fprintf(file,*args):
    print file+":"
    for x in args:
        print x
arg=["a","b","c"];
fprintf("txt",*arg);

注:*name领受一个元组,**领受一个字典,且*name必须在**name之前呈现
*lambda表达式

def make_incrementor(n):
    return lambda x: x + n
f=make_incrementor(42);
print f(0);
print f(1);

成果:

42
43

*文档字符串

def my_function():
    """Do nothing, but document it.

    No, really, it doesn't do anything.
    """
    pass
print my_function.__doc__

成果:

Do nothing, but document it.

    No, really, it doesn't do anything.

注:第一行冗长介绍,第二行留空(与文档的缩进格局有关),第三行详细介绍
 

顶一下
(1)
100%
踩一下
(0)
0%
------分开线----------------------------
标签(Tag):C# C#实例教程 c#根本教程 C#源代码 c#技能
------分开线----------------------------
颁发评论
请自发遵循互联网相关的政策法规,严禁公布色情、暴力、革命的谈吐。
评价:
神色:
考证码:点击我更换图片
猜你感兴趣