python学习第03篇-20181031

今天学习的是python的类与实例的一些知识,还对于python进行了一些小的总结:如下

print("--------类与实例-------")


class Student(object):
    pass


class Student2(object):
    def __init__(self, name, tell):
        '''
        self: 表示创建的实例本身,python解析器会自动把实例变量传进去
        :param name: 名称
        :param tell: 电话
        '''
        self.name = name
        self.tell = tell

    # 数据封装
    def print_tell(self):
        # 除了第一个参数是self以外,其他与普通函数一样
        print("%s: %s" % (self.name, self.tell))


big = Student()
# 类可以自由的给一个实例变量绑定属性
big.name = '李峰'
print(big.name)

big2 = Student2('jack', 18297935403)
print(big2.name)
print(big2.tell)
big2.print_tell()

# 私有变量与私有方法
'''
私有变量与私有方法只有在类内部才能访问
在普通变量或方法名(即公有变量名或方法名)前面加两个'_',即变成了私有变量与方法
'''


class Apple(object):
    pub = "这是公有变量"
    __pri = "这是私有变量"

    def __init__(self):
        self.other = "公有变量也可以这样定义"

    def out_pub(self):
        print("公有方法:", self.pub, self.__pri)

    def __out_pri(self):
        print("私有方法:", self.pub, self.__pri)


apple = Apple()
apple.out_pub()  # 访问公有方法
print(apple.pub, apple.other)  # 访问公有变量
try:
    apple.__out_pri()
except Exception as e:
    print("调用私有方法发生错误")

try:
    apple.__pri
except Exception as e:
    print("调用私有变量发生错误")

print("-------小结--------")

'''
小结:
测试变量类型: type(变量)
转换变量类型: str(变量) # 将变量转换为str
            int(变量) # 将变量转换为int
查询已经安装的模块:
            help('modules')
            
dir() ---> 可以查看指定模块中所包含的所有成员或者指定对象类型所支持的操作
help() ---> 返回指定模块或者函数的说明文档
id() ---> 查询变量的存储地址
ord() ---> 查询字符的ASCII码(十进制)
chr() ---> 把编码转换成对应的字符
'''

apple1 = Apple()
apple2 = Apple()
print(id(apple1))
print(id(apple2))
print(ord('b'))
print(chr(97))
print(len('qwe'))

ss = 'python is good'
print(ss[1])
print(ss.index('g'))

'''
tuple list string函数相同点
1.每一个元素都可以通过索引来获取
2.都可以使用len函数检测长度
3.都可以使用"+"和数乘"*" ,数乘表示将tuple函数,list函数,string函数重复数倍
'''
tuple1 = (1, 2, 3, 4, 5)
list1 = [1, 2, 3]
str = "abc"
print(tuple1 * 2)
print(list1 * 2)
print(str * 2)

'''
修改相关的方法均不能用于tuple和str函数,因为它们是不可变的
'''
str1 = "I love py , and you?"
print(str1.split(","))  # 将字符串变成list
print(str1.split())  # 如果split()函数中不传参数,将按照空格切分

# split的逆运算 join
print('sep'.join(str1.split()))
list2 = ['a', 'b', 'c']
print("-------------------")
print("p".join("hello"))  # hpeplplpo
print("p".join(list2))  # apbpc
'''
join的效果是,若join的是一个list,在每个元素后面加上p,不在最后一个元素后面加
若join是一个string ,则相当于把string切分为一个一个字符,在除了最后一个字符的后面加
'''

# 列表和元素可以相互转换
tuple2 = (1, 2, 3, 4)
list3 = [1, 2, 3]
print(list(tuple2))
print(tuple(list3))

# 字符串检验开头与结尾
print("hello".endswith('o'))
print("hello".startswith('h'))

# re.sub(被替词, 替换词, 替换域, flags=re.IGNORECASE) 这条语句用于查找和替换,忽略大小写
import re

S = "I love py, do you love Py?"
SS = re.sub('py', 'R', S)  # 替换S中py为R
print(SS)
SSS = re.sub('py', 'R', S, flags=re.IGNORECASE)  # 替换时候忽略大小写
print(SSS)

'''
注意的坑:
关于数据备份:
1.list6 = list4[:]
2.使用函数copy进行备份
list7 = list4.copy()
'''
list4 = [1, 2, 3, 4, 5]
print(id(list4))
list5 = list4
print(id(list5))  # 与上面存储地址一样,表示没有这种操作达不到备份效果
list6 = list4[:]  # 把list4中所有元素赋值给list6
print(id(list6))
print(list6)

list7 = list4.copy()
print(id(list7))
print(list7)

猜你喜欢

转载自blog.csdn.net/qq_27466827/article/details/83579364