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. Promise 让异步更优

    每个异步方法都返回一个Promise 更优雅. then方法 每一个Promise  都有一个叫then 的方法, 接受一对callback    被解决时调用,resolve, 被拒绝   reje ...

  2. MySql的大小写问题

    原来Linux下的MySQL默认是区分表名大小写的,通过如下设置,可以让MySQL不区分表名大小写:1.用root登录,修改 /etc/my.cnf:2.在[mysqld]节点下,加入一行: lowe ...

  3. hdu4666 最远曼哈顿距离

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4666 #include <cstdio> #include <cstring> ...

  4. selenium webdriver python 操作IE浏览器

    Step1:下载IEDriverServer 下载路径:http://selenium-release.storage.googleapis.com/index.html 我下载的是2.48版本的IE ...

  5. 上海Uber优步司机奖励政策(1月18日~1月24日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. React Native专题

    转载注明出处:地址:http://www.lcode.org本文出自:[江清清的技术专栏]本React Native讲解专题:主要讲解了React Native开发,由基础环境搭建配置入门,基础,进阶 ...

  7. java解析页面包jsoup

    http://www.open-open.com/jsoup/parsing-a-document.htm jsoup: Java HTML Parser jsoup is a Java librar ...

  8. pandas对象保存到mysql出错提示“BLOB/TEXT column used in key specification without a key length”解决办法

    问题 将DataFrame数据保存到mysql中时,出现错误提示: BLOB/TEXT column used in key specification without a key length 原因 ...

  9. [置顶] 步步辨析JS中的对象成员

    前提 首先我们应该明白创建一个JS对象的具体实例是实例化的过程,而实例化是通过new关键字实现的,这个对象是含有constructor的,一般的核心对象都会具有constructor以便创建其实例.因 ...

  10. JS 操作 HTML 自定义属性

    <input type="text" id="txtBox" displayName="123456" /> 获取自定义属性值: ...