This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://www.runoob.com/python/python-100-examples.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re | |
class Tail: | |
def __init__(self, file=None, regex=None): | |
self.__file = file | |
self.__mode = 'r' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def happy_num(num): | |
st = set() | |
while True: | |
if num == 1: | |
return True | |
num = sum([int(i)**2 for i in str(num)]) | |
if num in st: | |
return False | |
st.add(num) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find(origin=None, items=None): | |
dc = {} | |
lst = [] | |
idx=0 | |
for i in items: | |
for j in origin: | |
if i == j: | |
lst.append(origin.index(j, idx)) | |
idx=origin.index(j)+1 | |
else: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def inject_user(default_user=None): | |
def user(fn): | |
def wraps(*args, **kwargs): | |
if kwargs.get('user'): | |
return kwargs['user'] | |
else: | |
return default_user | |
return wraps | |
return user | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add(*args): | |
from functools import reduce | |
sum = reduce(lambda x,y:x+y, args) | |
return sum | |
def sum(fn, num): | |
while True: | |
if num < 9: | |
return num | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ld = [2, 3, 4, 5, 6, 7] | |
def isPrime(n): | |
if n <= 1: | |
return False | |
i = 2 | |
while i * i <= n: | |
if n % i == 0: | |
return False | |
i+=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def list_deduplication(): | |
"""List Deduplication. | |
""" | |
ld = [1, 3, 2, 4, 3, 4, 2, 5, 5, 7] | |
nld = [] | |
[nld.append(str(i)) for i in ld if str(i) not in nld] | |
return ','.join(nld) | |
list_deduplication() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fun(): | |
"""Guess the number. | |
Three chances Yo. | |
""" | |
constant=4 | |
for i in range(3): | |
number = int(input("Please input a number:")) | |
if num == constant: | |
msg = "congratulations! you win!" | |
return msg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#实现一个简单的python tornado Websocket 实例. | |
#${ROOT}/ws.py | |
#!-*- coding:utf-8 -*- | |
import os.path | |
import tornado.httpserver | |
import tornado.web | |
import tornado.ioloop |
NewerOlder