python常用知识点

【常用知识点】
1 字符串翻转、fibnacci数列、main使用:

#!/usr/bin/python
#Filename:using_sys.py
#python using_sys.py we are
import sys
import os
print 'the command line:'

"""
print 'hello'
"""
def test_main():
    print "we are in %s"%__name__

if __name__=="__main__":
    test_main()

print __file__

for i in sys.argv:
    print i#

print '\n\n PythonPath is',sys.path,'\n'

#fibonacci数列
a,b = 0,1
while b<10:
    print(b),
    a,b=b,a+b
print"\n"

#fibonacci数列
a=1
b=1
c=0
while c<30:
    c=a+b
    a=b
    b=c
    print(c)

#reverse
def reverse(str):
    str=str[::-1]

2 类class

#file:class.py
#普通函数定义
def foo(x): #函数定义必须带(),foo()
    print "executing foo(%s)"%(x) #函数实现必须空格或TAB键起始
#类定义 可不带参数(),class A:
class A(object):
    def foo(self,x):
        print "executing foo(%s,%s)"%(self,x)
    @classmethod
    def class_foo(cls,x):
        print "executing class_foo(%s,%s)"%(cls,x)
    @static method 实例引用,入参必须有self,self命名可任意;调用方式:a=A();a.static_foo(b)
    def static_foo(self,x):
        print "executing static_foo(%s)"%(self,x)
    @此时x即为self,调用:a.static_foo1()
    def static_foo1(x):
        print "executing static_foo(%s)"%x
a=A()

class B(object):
    count = 1
    def __init__(self,x=0,y=0):
        self.x=x
        self.y=y
    def run():
        print "run B"
class B1(B):
    def run():
        print"enter B1"
        super(B1,self).run()    
        print "leave B1"
class B2(B)
    def run():
        print"enter B2"
        super(B2,self).run()    
        print"leave B2"
class B3(B1,B2)
    def run():
        print"enter B3"
        super(B3,self).run()
        print"leave B3"

b=B()
bb=B()
b.count=2
#id(b)!=id(bb);id(B)!=id(B1)
print"id(B)={0};id(b)=(1);id(bb)={2}",.format(id(B),id(b),id(bb))
#print id(B)
#b.count=2;bb.count=1
print"b.count=%s;bb.count=%s"%(b.count,bb.count)
#print(b.count)

b1=B1()
print"id(B1)={0}",.format(id(B1))
b1.count=3
print"b1.count=%s"%b.count

b2=B2()
#id(B2)!=id(B1)
print"id(B2)={0}",.format(id(B2))
#b2.count=1
print"b2.count=%s"%b2.count

b3=B3()
#run顺序: B B2 B1 B3
b3.run()

\ 实例方法 类 方法 静态方法
a = A() a.foo(x) a.class_foo(x) a.static_foo(x)
A 不可用 A.class_foo(x) A.static_foo(x)

3 and or ; reversed(); len()

# (bool&a)|b;  (bool=0 or !0)
def choose(bool, a, b):
    return (bool and [a] or [b])[0]
choose(1,0,2) #2
sequence=1,2,3#sequence=(1,2,3);sequence=[1,2,3]均可
sequence = range(1,10)
for x in reversed(sequence):
    print(x)#9...2,1
print"len=%d"%len(sequence) #len=9

浅析 Python 的类、继承和多态 Python入门之类(class)

4 tuple ;list

#空元组
tuple0=()
#一个元素的元组
tuple1=(2,)
tuple2=(4,5,'ab','xyz')
tuple3=tuple1+tuple2
#()
print tuple0
#tuple3=(2,4,5,'ab','xyz')  注意:tuple[0]=3非法
print'tuple3=',tuple3
#tuple3[0] tuple3[1:4]=2 (4,5,'ab')
print'tuple3[0] tuple3[1:4]=',tuple3[0],tuple3[1:4]

#list
list1=[1,"ab",'xyz']
tuple1=tuple(list1)
#list1=[1,'ab','xyz']
print'list1=',list1
#tuple1=(1,'ab','xyz')
print'tuple1=',tuple1
#转换为元组
l = tuple()

dict={"key1":11,'key2':'xy'}

python中list/tuple/dict/set的区别
5 字符串 格式化% 和 .format
参考:python的str.format方法

#% 和 .fromat
name= 1
namearray=(1,2,3)
print "name=%s"%name --1
print "name={0}".format(name)--1 注意:{0},{1}...
print "namearray=%s"%(namearray,) --1,2,3 注意:(namearray,)
print "namearray={0}".format(namearray)--1,2,3注意:不要使用“,”;

6 *args和**kwargs
用*args和**kwargs只是为了方便并没有强制使用它们.
当你不确定你的函数里将要传递多少参数时你可以用*args.例如,它可以传递任意数量的参数:

#*args  **kwargs
def print_args(*args):
#0.apple  1.banana  2.cabbage 注意:enumerate()
for count,thing in enumerate(args):
    print'{0}.{1}'.format(count,thing)
print_args('apple','banana','cabbage')
#**kwargs 允许你使用没有事先定义的参数名:
def print_kwargs(**kwargs):
#cabbage=vegetable  apple=fruit 注意:items()
for name,value in kwargs.items():
    print'{0}={1}'.format(name,value)
print_kwargs(apple='fruit',cabbage='vegetable')

7 python学习笔记1-numpy/enumerate

8 repr 与 链表操作.

class Node(object):
    def __init__(self,sName):
        #lChildren定义为一个空链表list
        self._lChildren = []
        self.sName = sName
    def __repr__(self):
        #<Node 'name'>
        return "<Node '{}'>".format(self.sName)
    def append(self,*args,**kwargs):
        #添加链表
        self._lChildren.append(*args,**kwargs)
    def print_all_1(self):
        print self
        for oChild in self._lChildren:
            oChild.print_all_1()
    def print_all_2(self):
        def gen(o):
            lAll = [o,]
            while lAll:
                #pop(0)指定索引删除list,remove为指定元素删除
                oNext = lAll.pop(0)
                lAll.extend(oNext._lChildren)
                yield oNext
        for oNode in gen(self):
            print oNode

oRoot = Node("root")
oChild1 = Node("child1")
oChild2 = Node("child2")
oChild3 = Node("child3")
oChild4 = Node("child4")
oChild5 = Node("child5")
oChild6 = Node("child6")
oChild7 = Node("child7")
oChild8 = Node("child8")
oChild9 = Node("child9")
oChild10 = Node("child10")

oRoot.append(oChild1)
oRoot.append(oChild2)
oRoot.append(oChild3)
oChild1.append(oChild4)
oChild1.append(oChild5)
oChild2.append(oChild6)
oChild4.append(oChild7)
oChild3.append(oChild8)
oChild3.append(oChild9)
oChild6.append(oChild10)
#child1,4,7,5,2,6,10,3,8,9
oRoot.print_all_1()
oRoot.print_all_2()

python list中append()与extend()用法
python笔记:list–pop与remove的区别

【其他】
python面试题大全(一)
https://www.cnblogs.com/goodhacker/p/3366618.html
python常见面试题(三)
很全的 Python 面试题

猜你喜欢

转载自blog.csdn.net/eleven_xiy/article/details/81278794