1. 程式人生 > >Python3學習筆記1-1

Python3學習筆記1-1

# 迭代
def fact(n):
    if  n == 1:
        return 1
    return n * fact(n-1)
fact(1)
1

#
[x * x for x in range(1,11) if x % 2 == 0]
[4, 16, 36, 64, 100]
[m + n for m in 'abc' for n in 'xyz']
['ax', 'ay', 'az', 'bx', 'by', 'bz', 'cx', 'cy', 'cz']

#首尾
a='    sadadas   '
print(a)
    sadadas   
print
(a.strip()) sadadas #Python內建的map()和reduce()函式 list(map(str, [1,2,4,4,5])) ['1', '2', '4', '4', '5'] from functools import reduce def fn(x, y): return x * 10 + y def char2num(s): digits = {'0':0, '1':1, '2':2, '3':3, '4':4} return digits[s] reduce(fn, map(char2num, '1234')) 1234 from functools import
reduce digits = {'0':0, '1':1, '2':2, '3':3, '4':4} def str2int(s): def fn(x, y): return x * 10 + y def charm2num(s): return digits[s] return reduce(fn, map(charm2num, s)) str2int('1234') 1234 from functools import reduce digits = {'0':0, '1':1, '2':2, '3':3, '4':4} def str2num
(s): return digits[s] def str2int(s): return reduce(lambda x, y: x * 10 + y, map(char2num, s))