| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
每行不用; , 使用縮進2格或4格
Comments: # Docstrings: """ or '''
keywords: https://www.programiz.com/python-programming/keyword-list
內建資料類型:
type()
int({str})
LEGB
condition_is_true if condition else condition_is_falseif condition:
passif condition:
pass
elif condition:if condition:
pass
else:
passTrue True, 'abc', 1, -1, [1,2]
False False, None, 0, 0.0, '', [], (), {}, set()
python 沒有 替代方式 dictionary
def numbers_to_strings(argument):
switcher = {
0: "zero",
1: "one",
2: "two",
}
return switcher.get(argument, "nothing")str[start : end : step]
len(str)
string to array str.split(str="", num=string.count(str)) str.rsplit(str="", num=string.count(str))
str = "Line1-abcdef \nLine2-abc \nLine4-abcd";
print str.split( );
print str.split(' ', 1 );','.join()
tools: https://regex101.com/ tutorial: http://zwindr.blogspot.com/2016/01/python-regular-expression.html
import re
phone = "123-456-7890"
search = re.search(r'(\d{3})-(\d{3})-(\d{4})', phone)
if search is not None:
pass
search.group(1)..
# re
re.compile(pattern, flags=0)
re.search(pattern, string, flags=0)
re.match(pattern, string, flags=0)
re.split(pattern, string, maxsplit=0, flags=0)
re.sub(pattern, repl, string, count=0, flags=0)
# match
match.group([group1, ...])
pattern = re.compile(r'(\w*) (\w*)(?P<tt>.*)')
match = pattern.match('hello world!!!')
print(match.group(0))
# >> !!!
print(match.group(3))
#同上 >> !!!
print(match.group('tt'))f = filter(fn, array)m = map(fn, array)d = reduce(fn, array)f = lambda arg1, arg2, ....: expressionclass Some:
def __init__(self, x):
self.x = x
@classmethod
def service(clz, y):
print('do service...', clz, y)
Some.service(30)import datetime
import time
# datetime
now = datetime.datetime.now()
now = datetime.datetime.utcnow()
# timestamp
time.time()
# time tuple
time.localtime()
# date
now.date()import pytz
now = datetime.datetime.utcnow()
# 從國家取得時區
pytz.country_timezones(‘cn’)
# 取得時區時間
tz = pytz.timezone('Asia/Tokyo')
datetime.datetime.now(tz)datetime.timedelta
datetime.datetime.now() - datetime.timedelta(days=3)
# 時間差
(datetime.datetime(2015,1,13,12,0,0) - datetime.datetime.now()).total_seconds()time.strftime("%Y-%m-%d %H:%M:%S")
datetime.datetime.strptime("2014-12-31 18:20:10", "%Y-%m-%d %H:%M:%S")
urlparse
from urllib.parse import urlparse
o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
o.scheme
o.geturl()import random
random.randint(0,99) # 隨機整數
random.randrange(0, 101, 2) # 隨機選取0到100間的偶數
random.choice('abcdefg&#%^*f') # 隨機字元
random.sample(range(20), 20)
random.sample('abcdefghij',3) # 多個字元中選取特定數量的字元
random.shuffle([1, 2, 3, 4, 5, 6]) # 洗牌https://docs.python.org/3/library/csv.html https://blog.gtwang.org/programming/python-csv-file-reading-and-writing-tutorial/
| Back | FazBrowse Home | New Git URL |