Python Function Note

Python Function Note

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

Python Function Note的相关文章

#MySQL for Python(MySQLdb) Note

#MySQL for Python(MySQLdb) Note #切记不要在python中创建表,只做增删改查即可. #步骤:(0)引用库 -->(1)创建连接 -->(2)创建游标 -->(3)选择数据库 -->(4)执行语句 -->(5)关闭连接 #(0)引用库 import MySQLdb #(1)创建连接 con = MySQLdb.connect(user = "root", passwd = "123456",host =

Python - Learn Note (2)

Python基本数据类型 整数.浮点数(浮点数也就是小数,之所以称为浮点数,是因为按照科学记数法表示时,一个浮点数的小数点位置是可变的).字符串(字符串是以''或""括起来的任意文本).布尔值.空值(空值是Python里一个特殊的值,用None表示) print语句 print语句也可以跟上多个字符串,用逗号","隔开,就可以连成一串输出,遇到逗号","会输出一个空格:使用"+"拼接不会产生空格 Python注释 Python

python basic note

偶尔用Python做点事情,用的时候隔了许久,又不太记得了,有时连基本的语法都忘掉,这里记录在一张纸上,方便查看.也涵盖比较实用的内容,方便信手捻来(我没写错吧) 其中代码片段主要摘自前段时间写的一些Python代码. Python Help >>> python("subprocess") 帮助是很重要,linux程序员,你懂的 Python tutorial https://docs.python.org/2/tutorial/ 初学Python的人用这个好,计算

python Function

Python 2.7.10 (default, Oct 14 2015, 16:09:02) [GCC 5.2.1 20151010] on linux2 Type "copyright", "credits" or "license()" for more information. >>> help(list) Help on class list in module __builtin__: class list(objec

python regex note

To remember python regex easily, we organise python regex notes from concrete to abstract, and from simple to sophisticated. I, Important character: 1, Quantitive character: ? :[0,1],  *:[0,infi), +:(0,infi), {n}:[n rep], {m,}:[m, infi), {,n}:[0,n],

Python - learn note(1)

1. 下载安装Python 2.7(为了向下兼容以前的版本), Python 3.5(VS2015不支持配置3.6的环境) 教程 需要使用VS2015进行开发,必须勾选上后面两项: 2. VS2015开发Python Visual Studio集成了Python Tools for Visual Studio插件.我们只需要在自定义安装的时候点选安装即可.--配置开发环境 修改文件保存编码方式: 3.交互模式 :?根据主提示符(">>>")来执行命令 从属提示符(&q

跟着文档学python(三):zip (Python function, in 2. Built-in Functions)

一,函数的文档: zip(): zip(*iterables) Make an iterator that aggregates elements from each of the iterables. Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops w

Python manual note

This variable should be treated as read-only by the user. Don’t explicitly assign a value to it — you would create an independent local variable with the same name masking the built-in variable with its magic behavior.

Python菜鸟之路一:Python基础-内置函数补充

常用内置函数及用法: 1. callable() def callable(i_e_, some_kind_of_function): # real signature unknown; restored from __doc__ """检查对象object是否可调用.如果返回True,object仍然可能调用失败:但如果返回False,调用对象ojbect绝对不会成功 Return whether the object is callable (i.e., some kin