您的位置:首页 > 编程语言 > Python开发

Python基础零碎知识小笔记《2017-10-07》(个人向)

2017-10-07 22:29 686 查看
一、time的一些格式

help(time.strftime)
Help on built-in function strftime in module time:

strftime(...)
strftime(format[, tuple]) -> string

Convert a time tuple to a string according to a format specification.
See the library reference manual for formatting codes. When the time tuple
is not present, current time as returned by localtime() is used.

Commonly used format codes:

%Y  Year with century as a decimal number.
%m  Month as a decimal number [01,12].
%d  Day of the month as a decimal number [01,31].
%H  Hour (24-hour clock) as a decimal number [00,23].
%M  Minute as a decimal number [00,59].
%S  Second as a decimal number [00,61].
%z  Time zone offset from UTC.
%a  Locale's abbreviated weekday name.
%A  Locale's full weekday name.
%b  Locale's abbreviated month name.
%B  Locale's full month name.
%c  Locale's appropriate date and time representation.
%I  Hour (12-hour clock) as a decimal number [01,12].
%p  Locale's equivalent of either AM or PM.

Other codes may be available on your platform.  See documentation for
the C library strftime function.


time.strftime(“%Y-%m-%d %H:%M:%S”,data)

输出:’2017-10-07 21:11:04’

二、shelve模块

d = shelve.open(xxx)


三、正则表达式

一个不错的博主总结,博主id为:WangSai.coo

http://blog.csdn.net/vip_wangsai/article/details/52090301

这位博主的总结也不错,

http://www.cnblogs.com/zj-luxj/p/6901048.html

re.search('(?P<id>[0-9]+)','abcd1234')
//实质上{‘id’:‘1234’}


四、with…open

with open('xxx','w/r...') as f:
pass

with open('xxx','w/r...') as f1,open('xxx','w/r...') as f2:
pass


五、参数代表意义

*args  :  接收N个位置参数,转成元祖形式
**kwargs   :  接收N个关键字参数,转成字典形式


六、lambda函数

calc = lambda x : x*9
print(calc(3))   //输出27


七、高阶函数

a :把一个函数名当做实参传给另一个函数(不修改被装饰函数,为其增加功能)

b : 返回值中包含函数名(不修改函数的调用方式)

类比数学中的函数

def dd():
print("dd")

def ee(func):
func()

ee(dd)  //输出dd


八、函数的嵌套

def test1():
def test2():
print("test2")
test2()

test1()    //输出test2


九、装饰器

小提醒:参数为funcName(*args,**kwargs)

<
4000
p>参考本地deco.py文件

def deco(func):
def fp(*args,**kwargs):
print("Parameters are : ", *args,**kwargs)
res = func(*args,**kwargs)
return res
return fp

@deco   # test1 = deco()
def test1(*args,**kwargs):
print("just test1!")

@deco
def test2():
print("just test2!")

def index():
print("Welcome to index page!")

index()
test1('rr','tt','yy',{'da':'dad'})
test2()

输出结果为:
Welcome to index page!
Parameters are :  rr tt yy {'da': 'dad'}
just test1!
Parameters are :
just test2!


十、属性方法

class Animal(object):
def __init__(self,name):
self.__name = name

@property
def animal(self):
print("the animal's name is %s"%(self.__name))

@animal.setter
def animal(self,name):
self.__name = name


an = Animal("cat")
an.animal


//输出:the animal’s name is cat

//property的作用:将方法变为一个静态变量

//静态方法标记为@staticmethod

十一、数组增删改查

1、arrays.append(obj)
2、arrays.extend([obj,...])
3、arrays.insert(num,obj)
4、arrays.remove(obj)
5、del arrays[num]
6、arrays.pop()
7、arrays[start:end]
arrays[:end]
arrays[start:]    //分片


十二、元祖tuple

不能修改元祖中的元素。

temp = (1,)
temp = 1,   //有逗号就行,空元祖为()


十三、收集参数

def test(*params):   //*params为收集参数

//test(1,2,3,4,5)
//params[1]=2


十四、全局变量

在局部函数里面用global修饰变量即可修改全局变量的值

count=10

def test():
global count
count = 5

//此时count=5


十五、三元运算符

a = result if b else c
result : (b = true) ;
result = c


十六、切片小知识

temp = [1,2,3,4,5,6]
temp[-1] = 6  //逆着来,下标以1为基准
temp[1] = 2   //顺着来,下标以0为基准


十七、字典

特性:

dict是无序的

key必须是唯一的,天生去重

十八、for表达式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: