Python实习第三天代码

函数基础

#函数的定义
def fun1():
	print("helloworld")
fun1()
#函数的传入参数
def fun2(a=1,b=2):
	print(a+b)
fun2()
#返回值函数[返回一个值]
def fun3(a,b):
	return a+b
#返回值函数[返回多个值,返回的是个元组]
def fun4(a,b):
	return a,b
sum=fun3(3,4)
print("sum={}".format(sum))
#元组承接
tuple=fun4(1,2)
print(tuple)
#拆分承接
c,d=fun4(1,2)
print(c,d)

函数作用域

number=100
def fun4():
	print(number)
# 会报错因为number可读,不可改
# 要想更改number必须用global(关键字)声明全局变量
'''
def fun5():
	number=90
'''
def fun5():
	#声明number为全局变量
	global number=90
	print(number)
#number=100
fun4()
#number被修改,number=90
fun5()
#number=90(修改后得值)
fun4()

可变参数函数

def fun5(*tuple,**dict):
    print(tuple,type(tuple))
    print(dict,type(dict))
fun5(1,2,3)
fun5(1,2,3,a=1,b=2)
list=[1,3,5,7,9]
fun5(list)#省略第二个参数
#拆分list
fun5(*list)
dict={"a":'1',"b":'2',"c":'3'}
fun5(dict)#省略第一个参数
fun5(*dict)#拆分key值
fun5(**dict)#拆分字典

面向对象学习

创建对象并调用属性、方法

# 面向对象,文件,异常处理
# csv
# 模型
class Phone:
    # 类属性
    # brand = '华为'
    # color = '黑色'
    # 对象方法  self表示对象本身
    def call(self):
        print('---->正在打电话....')
print(Phone)
# 实体
# 实例:对象
phone1 = Phone()
phone1.call()
phone1.brand = '小米'  # 动态添加属性
phone1.price = '3999'  # 对象属性
print(phone1.brand)
print(phone1.price)

phone2 = Phone()
phone2.call()
print(phone2.brand)

构造方法(魔术方法)

class Phone:
    # 类属性
    # brand = '华为'
    # color = '黑色'
    # 对象属性
    # 对象属性 类似构造方法   初始化赋值
    def __init__(self, brand, color, price):  # 魔术方法:不用手动调用,在某种场景下自动调用
        print('----->init')
        # 在self的空间中动态添加了三个属性  __名字 私有
        self.__brand = brand
        self.__color = color
        self.__price = price

    def getBrand(self):
        return self.__brand
    def getColor(self):
        return self.__color
    def setBrand(self, brand):
        self.__brand = brand

    # 对象方法  self表示对象本身
    def call(self):
        print('正在{}品牌{}的打电话....'.format(self.__brand, self.__color))


# 创建对象

p1 = Phone('华为', '黑色', '3999')
print("p1的品牌{}".format(p1.getBrand()))
print("p1的颜色{}".format(p1.getColor()))
p1.call()
p2 = Phone('小米', '红色', '3999')
print("p2的品牌{}".format(p2.getBrand()))
print("p2的颜色{}".format(p2.getColor()))
p2.call()

类的继承

# 继承
# 子类继承父类
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.__money = 1000

    def eat(self):
        print('{}吃饭....'.format(self.name))

    def sleep(self):
        print('{}正在睡觉...'.format(self.name))


class A:
    def printA(self):
        print('多继承class A 成功')
#多继承(python特有)
#Student继承Person,A
class Student(Person, A):
    def __init__(self,name,age):
        print('我是Student的魔术方法....')
        super(Student, self).__init__(name,age)

    def study(self):
        print('{}在学习python'.format(self.name))
        # 不能调用父类私有方法
        # print(self.__money)

class Teacher(Person):
    def teach(self):
        print('{}在教授python'.format(self.name))

class Admin(Person):

    def manage(self):
        print('{}在管理这个系统'.format(self.name))

# 创建对象,实例化对象
s = Student('cqg',18)
# 调用本类里的study()方法
s.study()
# 调用父类里的eat()方法
s.eat()
print('偶的姓名:')
print(s.name)
print('偶的年龄')
print(s.age)
# print(s.__money)
s.printA()
# C3算法深度执行顺序(冲突判定)
print(Student.mro())

文件操作

文件的写入

# 首先要在根目录下建立目录(文件夹)files
with open('files/hello.txt',mode='w',encoding='utf-8') as fw:
    fw.write("Helloworld")

文件的读取

with open('files/hello.txt',mode='r',encoding='utf-8') as fr:
    content=fr.read()
    print(content)

mode(模式参数)

'''
 open(file,mode)
 file: 要操作的文件的路径: 绝对,相对
 mode: r: read  只能读取文件   默认
       w:write  只能写入文件
       a: append  追加内容到文件
       t: text 文本文件 默认
       b: binary 二进制文件  图片,音视频,文本文件

'''

uncode(编码参数)
utf-8、GBK

csv文件操作

import csv
# 一个元组列表
student=[('18413001','程麒阁'),('18413002','王海同'),('18413010','王楠')]
#写入csv文件
with open('files/file.csv',mode='w',encoding='utf-8',newline='') as fw:
    print("写入csv格式")
    #进入csv管道
    cw=csv.writer(fw)
    for i in student:
        cw.writerow(i)
#读取csv文件
with open('files/file.csv',mode='r',encoding='utf-8',newline='') as fr:
    print("读取csv文件")
    #进入csv管道
    cr=csv.reader(fr)
    print(cr)
    for i in cr:
        print(i)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值