Python基础详解三

一,函数的多返回值

python 复制代码
def methodReturn():
    return 1,2

x,y=methodReturn()
print(x,y)

1 2

二,函数的多种参数使用形式

缺省参数:

python 复制代码
def method7(name,age,address="淄博"):
    print("name:"+name+",age="+str(age)+",address="+address)

method7("袁震",20)

name:袁震,age=20,address=淄博

设置默认值必须在最后

可变参数:

python 复制代码
def method8(*args):
    print(args)

method8(20,"袁震")

(20, '袁震')

键值对可变参数:

python 复制代码
def method9(**kwargs):
    print(kwargs)

method9(name="袁震",age=20,address="淄博")

{'name': '袁震', 'age': 20, 'address': '淄博'}

三,匿名函数

3.1函数作为参数传递

python 复制代码
def method10(test,name,age):
    result =test(name,age)
    print(result)
def test(name,age):
    return name+","+str(age)
method10(test,"袁震",20)

袁震,20

3.2lambda匿名函数

python 复制代码
lambda 传入参数:函数体(一行代码)
python 复制代码
def method10(test,name,age):
    result =test(name,age)
    print(result)
def test(name,age):
    return name+","+str(age)


method10(lambda name,age:name+","+str(age),"袁震",20)

袁震,20

四,文件的操作

4.1 文件的读

python 复制代码
#打开文件
f =open("D:/shuju/baofa.txt","r",encoding="UTF-8")
print(f.name)

D:/shuju/baofa.txt

python 复制代码
#读取全部内容
print(f.read())
python 复制代码
#打开文件
f =open("D:/shuju/baofa.txt","r",encoding="UTF-8")

#读取全部行

list =f.readlines()
print(list)
python 复制代码
#打开文件
f =open("D:/shuju/baofa.txt","r",encoding="UTF-8")

#一次读取一行

list1 =f.readline()
print(list1)
python 复制代码
#打开文件
f =open("D:/shuju/baofa.txt","r",encoding="UTF-8")

#for循环每一次读取一行数据
for line in f:
    print(line)

文件的关闭:

python 复制代码
f.close()

with open语法:

python 复制代码
#不需要手动关闭文件
with open("D:/shuju/baofa.txt","r",encoding="UTF-8") as f:
    for line in f:
        print(line)

4.2 文件的写

python 复制代码
f=open("D:/shuju/yuanzhen.txt","w",encoding="UTF-8")

f.write("我是袁震")
f.flush()

或者

python 复制代码
f=open("D:/shuju/yuanzhen.txt","w",encoding="UTF-8")

f.write("我是袁震11")
f.close()

w模式会覆盖之前的内容

4.3 文件的追加

python 复制代码
f=open("D:/shuju/yuanzhen.txt","a",encoding="UTF-8")

f.write("\n我是袁震22")
f.flush()
f.close()

五,异常

5.1 捕获异常

python 复制代码
#基本语法
try:
  可能发生错误的代码
except:
  如果出现异常执行的代码
else:
  没有异常时执行的代码
finally:
  无论如何都要执行的异常

5.2 异常的传递性

异常 具有传递性,可以从最内层的方法传递到最外层,不需要throw

六,模块

python 复制代码
import time  #导入time模块
time.sleep(100)
python 复制代码
from time import sleep  #导入time模块的sleep方法
sleep(100)

模块定义别名:

python 复制代码
import time as t  #导入time模块 定义别名为t

t.sleep(100)

七,python包

安装第三方包:

工具:

相关推荐
小小小米粒13 分钟前
Collection单列集合、Map(Key - Value)双列集合,多继承实现。
java·开发语言·windows
蜡台29 分钟前
Python包管理工具pip完全指南-----2
linux·windows·python
Mr.朱鹏31 分钟前
【Python 进阶 | 第四篇】Psycopg3 + Flask 实现 PostgreSQL CRUD 全流程:从连接池到RESTful接口
python·postgresql·flask·virtualenv·fastapi·pip·tornado
czhc11400756631 小时前
C# 428 线程、异步
开发语言·c#
2401_871492851 小时前
Vue.js监听器watch利用回调函数处理级联下拉框数据联动
jvm·数据库·python
FreakStudio1 小时前
亲测可用!可本地部署的 MicroPython 开源仿真器
python·单片机·嵌入式·面向对象·并行计算·电子diy·电子计算机
:1211 小时前
java基础
java·开发语言
SilentSamsara2 小时前
Python 环境搭建完整指南:从下载安装到运行第一个程序
开发语言·python
小短腿的代码世界2 小时前
Qt文件系统与IO深度解析:从QFile到异步文件操作
开发语言·qt
zhoutongsheng2 小时前
C#怎么实现Swagger文档 C#如何在ASP.NET Core中集成Swagger自动生成API文档【框架】
jvm·数据库·python