Python自动化开发实践 - Python基础

浅拷贝 深拷贝

import copy
list1 = [1,2,3,4]
list2 = list1  #list2 = copy.copy(list1)   浅拷贝
list3 = copy.deepcopy(list1)    #深拷贝
list1[2] = 5
print(list1)  #1 2 5 4
print(list2)  #1 2 5 4
print(list3)  #1 2 3 4

and / or

print(10 and 20)  #20  返回较大数
print(False and 20)  #False
print(True and True)  #True
print(10 or 20)  #10  返回较小数
print(False or 20)  #20
print(False or True)  #True

in / not in
成员运算符

x = 'hello world'
y = [1,2,3,4]
print('world' in x)
print(2 not in y)

is / is not
身份运算符 判断两个对象的存储单位是否一致

x = 10
y = 10
print(x is y)  #True
y = 20
print( x is not y)  #True

字符串

a = 'AABBCCDD'
print(a[1:5:2])  #从1到5,间隔为2
print(a[::-1])   #逆序

a = a.replace('C', 'V')  #'AABBVVDD'

a.find('B')  #index()
a.find('B', 2) #开始查找位置  index()
a.find('B', 2, 5)  #结束位置  index()
a.find('E')  #return -1
a.index('E')  #抛出异常

s = 'ABCDABCD'
a = s.split('B', 1)  #a = ['A', 'CDABCD']  分割次数
a = s.split('B')  #a = ['A', 'CDA', 'CD']

st = ['hello', 'world']
s = ' '.join(st)

元组 / 列表

t = (1,2,3,(1,), [1,2])  #(1,)
l = [1,2,3,(1,), [1,2]]
t.count(1)  #统计t中1的个数
len(t)  #t的长度

append / extend / insert / remove / pop / del

st = ['hello']
st.append(['world', 'hello']) #['hello', ['world', 'hello']]
st.extend(['world', 'hello']) #['hello', 'world', 'hello']
st.insert(2, 'love')  #在第三个元素前插入
st.remove('hello')
st.pop(-1)  #通过索引删除
del st[1]
del st[0:1]

集合/字典
集合元素只支持数字、字符串、元组
二者都是无序的

cmp(dic1, dic2)  #比较两个字典
dict1.fromkeys(lis, value)  #以lis作关键字key,value为值
dict1.has_key(k)  #k是否为字典的key值
dict1.items()  #以列表形式返回键值对(元组)
dict1.keys()  #以列表形式返回键
dict1.values()  #返回所有的值
dict1.setdefault(key, default=None)  #读取元素,若key不存在,则添加key,值为default
dict1.upadte(dict2)  #将dict2中的键值对更新到dict1中

类型转换

st = 'hello world'
st_list = st.split(' ')
li = ['hello', 'world']
list_st = ' '.join(li)

str1 = '[1, 2, 3]'
li1 = eval(str1)
li2 = list(str1)  #每一个元素作为列表元素
st = str(li)  #将整个列表转换为字符串

str1 = 'hello'
str2 = 'world'
dic = dict(a = str1, b = str2)  #{'a':'hello', 'b':'world'}

import json
st = "{'a':'hello', 'b':'world'}"
dic = json.loads(st)  #键值对要求用单引号

import ast
st = "{'a':'hello', 'b':'world'}"
dic = ast.literal_eval(st)

lis1 = ['a', 'hello']
lis2 = ['b', 'world']
dic = dict([lis1, lis2])  #{'a':'hello', 'b':'world'}

类 class

class Person(object):
    #静态属性
    name = 'xxx'

    #动态属性
    def __init__(self):
        self.age = 18

    #普通方法
    def foot(self):
        print('Foot!')

    #类方法
    @classmethod
    def class_hand(cls):
        print('Hand!')

    #静态方法
    @staticmethod
    def static_mouth():
        print('Mouth!')

if __name__ == '__main__':
    print(Person.name)
    person = Person()
    print(person.name)

    print(person.age)

    person.foot()

    Person.class_hand()
    person.class_hand()

    Person.static_mouth()
    person.static_mouth()

封装

class People(object):
    def __init__(self):
        self.__name = 'XXX'
        self.age = 18

    def __get_age(self):
        return self.age
    
    def get_name(self):
        return self.__name
        
people = People()
print(people.get_name())
print(people.age)
print(people._People__name)
print(people._People__get_age())

异常处理

try:
    #...
except ImportError as ie:
    #...
except NameError as ne:
    #...
else:
    #...
finally:
    #...
    
class MyError(Exception):
    pass
    
try:
    #...
    raise MyError('ERROR!')
except MyError as me:
    #...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值