Python Function Note

 #汉诺塔问题Python实现
def my_move(n, a, b, c):
if n == 1:
print(a + ' --> ' + c)
else:
my_move(n-1, a, c, b)#将前n-1个盘子从a放到b
my_move(1, a, b, c)#将最下面的盘子从a放到c
my_move(n-1, b, a, c)#将b上的n-1个盘子放到c上
return
 #杨辉三角Python实现
def my_triangles(max):
i = 1
now = [1]
while i <= max:
yield now#保存当前list
now = [1] + [now[n]+now[n+1] for n in range(len(now)-1)] + [1]#构建下层list
i+=1
print('done')
 #实现将‘123.456’转换成123.456,即函数float的实现
def my_float(s):
def my_front(x, y):
return x*10+y
def my_behind(x, y):
return x*0.1+y front = s.split('.')[0]
behind = s.split('.')[1]
return reduce(my_front, map(int, front)) + 0.1*reduce(my_behind, map(int, behind))
 #利用埃氏筛法筛选出素数
#产生无限的奇数
def my_productNumber():
n = 1
while 1:
n += 2
yield n #返回判断是否是素数的函数
def my_isPrime(n):
return lambda x: x % n > 0 #素数发生器
def my_Primes():
yield 2
it = my_productNumber()
while 1:
n = next(it)
yield n
it = filter(my_isPrime(n), it) for n in my_Primes():
if n < 100:
print(n)
else:
break
 #判断一个数是否回文
def my_isPalindrome(n):
return str(n)[::-1] == str(n)
print(filter(my_isPalindrome, range(1, 1000)))
 #关于装饰器
import functools def log(text=None):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
if text == None:
# print('call %s()' % func.__name__)
pass
else:
# print('%s %s()' % (text, func.__name__))
pass
print('Begin Func↓')
temp = func(*args, **kw)
print('End Func↑')
return temp
return wrapper
return decorator @log('call') #相当于now = log('hahaha')(now)
def now(t):
print("")
return t
now(4)
print(now.__name__)

Python Function Note的更多相关文章

  1. #MySQL for Python(MySQLdb) Note

    #MySQL for Python(MySQLdb) Note #切记不要在python中创建表,只做增删改查即可. #步骤:(0)引用库 -->(1)创建连接 -->(2)创建游标 -- ...

  2. python function parameter

    Python 2.7.10 (default, Oct 14 2015, 16:09:02) [GCC 5.2.1 20151010] on linux2 Type "copyright&q ...

  3. kwargs - Key words arguments in python function

    This is a tutorial of how to use *args and **kwargs For defining the default value of arguments that ...

  4. python Function

    Python 2.7.10 (default, Oct 14 2015, 16:09:02) [GCC 5.2.1 20151010] on linux2 Type "copyright&q ...

  5. elike.python.function()

    将python用于基本的科学计算,能完全替代matlab.就最近写的一个物理模型程序来看,用python建立的物理模型的可控性,代码的层次性都优于matlab,只不过python没有matlab那样的 ...

  6. Python - Learn Note (3)

    Python之模块 包就是文件夹:包可以有多级: 模块就是 xxx.py文件:可以创建自己的模块,并且导入它们,模块的名字就和文件的名字相同: Python使用import语句导入一个模块. impo ...

  7. Python - Learn Note (2)

    Python注释 Python的注释以#开头,后面的文字直到行尾都算注释 Python基本数据类型 整数.浮点数(浮点数也就是小数,之所以称为浮点数,是因为按照科学记数法表示时,一个浮点数的小数点位置 ...

  8. Python - learn note(1)

    1. 下载安装Python 2.7(为了向下兼容以前的版本), Python 3.5(VS2015不支持配置3.6的环境) 教程 需要使用VS2015进行开发,必须勾选上后面两项: 2. VS2015 ...

  9. python function with variadic arguments or keywords(dict) 可变参数与关键字参数

    *args 表示任意个普通参数,调用的时候自动组装为一个tuple **kwags 表示任意个字典类型参数, 调用的时候自动组装成一个dict args和kwags是两个约定俗成的用法. 变长参数可以 ...

随机推荐

  1. js plugin

    http://site518.net/javascript-date-handle/ http://developer.51cto.com/art/201212/374902.htm http://e ...

  2. 转:USB主机控制器(Host Controller)--深入理解

    1. 主机控制器(Host Controller) • UHCI: Universal Host Controller Interface (通用主机控制接口, USB1.0/1.1)      • ...

  3. DB2中的系统表SYSIBM.SYSDUMMY1

    ORACLE中有一张系统数据库表DUAL用来访问系统的相关信息 SELECT SYSDATE FROM DUAL;  --返回当前系统日期 ------------------------------ ...

  4. Hadoop环境搭建-入门伪分布式配置(Mac OS,0.21.0,Eclipse 3.6)

    http://www.linuxidc.com/Linux/2012-10/71900p2.htm http://andy-ghg.iteye.com/blog/1165453 为Mac的MyEcli ...

  5. fedora下体验gentoo安装

    服务器上安装了fedora,但是对gentoo很想体验一番,没有新机器,不想重装系统,所以只能chroot来体验getoo了. 下载portage-20130817.tar.bz2和stage3-am ...

  6. mysql 监控长事务

    mysql> desc information_schema.innodb_trx -> ; +----------------------------+----------------- ...

  7. POJ2286 The Rotation Game(IDA*)

    The Rotation Game Time Limit: 15000MS   Memory Limit: 150000K Total Submissions: 5691   Accepted: 19 ...

  8. 数据结构典型算法的VC实现(袁辉勇)

    1. 迷宫问题求解 #include <stdio.h> #define m 8 //迷宫内有8列 #define n 8 //迷宫内有8行 #define MAXSIZE 100//栈尺 ...

  9. Sublime Text 学习资料

    Sublime Text 全程指南 Sublime Text 3 官方下载: http://www.sublimetext.com/3

  10. JavaScript笔记(一),

    加法函数 javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显 //调用:accAdd(arg1,arg2) //返回值:arg1加上arg2的精确结果 function accA ...