python toy codes

函數:

# -*- coding:utf-8 -*-
def my_abs(x):
    if x >= 0:
        return x
    else:
        return -x

print u'請輸入數字:'
x=raw_input()
xx = my_abs(int(x))

print u'%s的絕對值是%d' %(x,xx)

列表生成式:

L = ['Hello', 'World', 18, 'Apple', None]
print [s.lower() if isinstance(s,str) else s for s in L ]

generator--生成器:

# -*- coding:utf-8 -*-
def fib(max):
    n, a, b = 0, 0, 1
    while n < max:
        print b
        a, b = b, a + b
        n = n + 1

print fib(7)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章