目录
- 1.函数
- 函数概括
- 内置函数
- 自定义函数
- 1.无参
- 2.有参
- 3.有默认参数
- 4.多参与默认参数结合
- 5.匿名函数,lamba表达式
- 2.格式化输出
- 3.异常
- 4.导包
- 5.面向对象
- 6.时间日期类
- 7.文件读写
- 1.函数
- 函数概括
- 内置函数
- 自定义函数
- 1.无参
- 2.有参
- 3.有默认参数
- 4.多参与默认参数结合
- 5.匿名函数,lamba表达式
- 2.格式化输出
- 3.异常
- 4.导包
- 5.面向对象
- 6.时间日期类
- 7.文件读写
1.函数
函数概括
python中函数 =》方法 【区别不大】
scala 【有区别】
python中函数分为以下三类
1.内置函数 【 常用类的api】
1.数学相关的 【math 包下的】
2.自定义函数
3.匿名函数【没有名字的函数】
内置函数
# 1.内置函数
print(abs(10)) #绝对值
print(max(1, 2, 3, 4, 5, 6))
print(min(1, 2, 3, 4, 5, 6))
print(sum([1, 2, 3]))
# 可以alt + enter导包,不是必须先导它
import math
print(math.sqrt(16))
print(math.ceil(3.5)) #上取整
print(math.floor(3.5)) #下取整
自定义函数
1.无参
# 2.1 无参
def fun():
print("hello function")
fun()
2.有参
def myabs(num):
if(num>=0):
return num
else:
return -num
print(myabs(-10))
3.有默认参数
# 2.3带有默认参数的函数,不传有默认值,传了就覆盖
# f是字符串插值
def play(name,age=30):
print(f"{name} is say,and his age is {age}")
play("xianyu")
play("xianyu",20)
play(name = "xianyu",age = 20)
play(age = 20,name = "xianyu")
4.多参与默认参数结合
# 多参可以和默认函数结合在一起
def play01(*args,name="xianyu"):
print(args[0],args[1],args[2],name)
play01(1,2,3)
5.匿名函数,lamba表达式
# 匿名函数,lamba表达式
p = (lambda : print("hello function"))
p()
def f1(x,y):
return x*y
print(f1(1,2))
p1 = (lambda x,y : x*y)
print(p1(2, 3))
2.格式化输出
print(1,2,3,4,end=',')
print(1,2,3,4,sep='0')
print(1,2,3,4)
# 格式化输出
name="xianyu"
age="21"
print(f"{name},{age}")
print("{0},{1}".format(name,age))
3.异常
java 异常:Throwable
error :
exception:
1.编译时异常
2.运行时异常
异常处理:
1.try [catch ] ..[finally]
2.throws
3.throw
python:
异常处理:
1.try except
2.finally
3.raise 主动抛出异常
语法结构:
try:
//可能发生异常的代码
except xxxERROR
todo。。。。
except xxxERROR
todo。。。。
except xxxERROR
todo。。。。
finally:
# 异常
try:
1/0
except ZeroDivisionError as e :
print(e)
# 不管异常发不发生,均执行
finally:
print("---finally---")
# python继承的东西放在小括号里,直接写最高级也ok
try:
x = int(input("---:"))
1 / 0
except BaseException as e :
print(e)
# 直接输出异常
raise
# 不管异常发不发生,均执行
finally:
print("---finally---")
def f1(n1,n2):
if(n1>0 and n2 >0):
return n1+n2
else:
# 直接输出异常
raise BaseException("输入的不是正数")
try:
print(f1(-1,3))
except BaseException as e:
print(e)
4.导包
# 导包
# 1
import math
print(math.sqrt(16))
# 2
from math import sqrt
print(sqrt(16))
# 3
import math as mm
print(mm.sqrt(16))
# 4
from math import *
print(sqrt(16))
# Auto Import 自动导包设置
5.面向对象
# 5.面向对象
class User:
# pass是占位符
# pass
# 构造器__init__,self理解为this
def __init__(self,name,age):
self.name = name
self.age = age
# self一定要加
def play(self):
print("---")
print(f"{self.name} is playing")
print("---")
def p2(self,id):
print("---")
print(f"{id} ")
print("---")
xy = User("xianyu",21)
print(xy.name)
print(xy.age)
xy.play()
# 继承,允许多继承,把继承的东西写在括号里就OK,类中还可以额外自定义方法
class student(User):
pass
stu = student("laoyu",18)
print(stu.name)
print(stu.age)
stu.play()
stu.p2(10)
# 继承的时候可以重写,不能重载
class student(User):
def play(self):
print(f"{self.name}hahaha")
# 但是可以重载本类的方法
def p01(self):
print(f"{self.name}hahaha")
def p01(self,age):
print(f"{self.name}hahaha,{age}")
stu = student("laoyu",18)
print(stu.name)
print(stu.age)
stu.play()
stu.p01(10)
6.时间日期类
from datetime import datetime
dt = datetime(2022,3,30)
print(dt)
print(dt.year)
print(datetime.now())
s1 = "2022-03-01"
dt2 = datetime.strptime(s1,'%Y-%m-%d')
print(type(dt2))
7.文件读写
# 文件读写
# 1.读文件
# 直接读,要手动关
f = open(r"D:\ \python-sk\data\1.txt")
res = f.read()
print(res)
f.close()
# with open 不用手动关
with open(r"D:\ \python-sk\data\1.txt") as f:
f.read()
print(res)
# 一行一行读
f = open(r"D:\ \python-sk\data\1.txt")
line = f.readline()
while(line):
print(line,end="")
line = f.readline()
f.close()
f = open(r"D:\ \python-sk\data\1.txt")
for line in f:
print(line,end="")
f.close()
with open(r"D:\ \python-sk\data\1.txt") as f:
line = f.readline()
while (line):
print(line, end="")
line = f.readline()
# r读 w写 a追加
with open(r"D:\ \python-sk\data\1.txt",mode='a') as f:
# 覆写
f.write("a,a,a,a\n")
# 覆写多行
f.writelines(["1,1,1,1\n","2,2,2,2\n"])
1.函数
函数概括
python中函数 =》方法 【区别不大】
scala 【有区别】
python中函数分为以下三类
1.内置函数 【 常用类的api】
1.数学相关的 【math 包下的】
2.自定义函数
3.匿名函数【没有名字的函数】
内置函数
# 1.内置函数
print(abs(10)) #绝对值
print(max(1, 2, 3, 4, 5, 6))
print(min(1, 2, 3, 4, 5, 6))
print(sum([1, 2, 3]))
# 可以alt + enter导包,不是必须先导它
import math
print(math.sqrt(16))
print(math.ceil(3.5)) #上取整
print(math.floor(3.5)) #下取整
自定义函数
1.无参
# 2.1 无参
def fun():
print("hello function")
fun()
2.有参
def myabs(num):
if(num>=0):
return num
else:
return -num
print(myabs(-10))
3.有默认参数
# 2.3带有默认参数的函数,不传有默认值,传了就覆盖
# f是字符串插值
def play(name,age=30):
print(f"{name} is say,and his age is {age}")
play("xianyu")
play("xianyu",20)
play(name = "xianyu",age = 20)
play(age = 20,name = "xianyu")
4.多参与默认参数结合
# 多参可以和默认函数结合在一起
def play01(*args,name="xianyu"):
print(args[0],args[1],args[2],name)
play01(1,2,3)
5.匿名函数,lamba表达式
# 匿名函数,lamba表达式
p = (lambda : print("hello function"))
p()
def f1(x,y):
return x*y
print(f1(1,2))
p1 = (lambda x,y : x*y)
print(p1(2, 3))
2.格式化输出
print(1,2,3,4,end=',')
print(1,2,3,4,sep='0')
print(1,2,3,4)
# 格式化输出
name="xianyu"
age="21"
print(f"{name},{age}")
print("{0},{1}".format(name,age))
3.异常
java 异常:Throwable
error :
exception:
1.编译时异常
2.运行时异常
异常处理:
1.try [catch ] ..[finally]
2.throws
3.throw
python:
异常处理:
1.try except
2.finally
3.raise 主动抛出异常
语法结构:
try:
//可能发生异常的代码
except xxxERROR
todo。。。。
except xxxERROR
todo。。。。
except xxxERROR
todo。。。。
finally:
# 异常
try:
1/0
except ZeroDivisionError as e :
print(e)
# 不管异常发不发生,均执行
finally:
print("---finally---")
# python继承的东西放在小括号里,直接写最高级也ok
try:
x = int(input("---:"))
1 / 0
except BaseException as e :
print(e)
# 直接输出异常
raise
# 不管异常发不发生,均执行
finally:
print("---finally---")
def f1(n1,n2):
if(n1>0 and n2 >0):
return n1+n2
else:
# 直接输出异常
raise BaseException("输入的不是正数")
try:
print(f1(-1,3))
except BaseException as e:
print(e)
4.导包
# 导包
# 1
import math
print(math.sqrt(16))
# 2
from math import sqrt
print(sqrt(16))
# 3
import math as mm
print(mm.sqrt(16))
# 4
from math import *
print(sqrt(16))
# Auto Import 自动导包设置
5.面向对象
# 5.面向对象
class User:
# pass是占位符
# pass
# 构造器__init__,self理解为this
def __init__(self,name,age):
self.name = name
self.age = age
# self一定要加
def play(self):
print("---")
print(f"{self.name} is playing")
print("---")
def p2(self,id):
print("---")
print(f"{id} ")
print("---")
xy = User("xianyu",21)
print(xy.name)
print(xy.age)
xy.play()
# 继承,允许多继承,把继承的东西写在括号里就OK,类中还可以额外自定义方法
class student(User):
pass
stu = student("laoyu",18)
print(stu.name)
print(stu.age)
stu.play()
stu.p2(10)
# 继承的时候可以重写,不能重载
class student(User):
def play(self):
print(f"{self.name}hahaha")
# 但是可以重载本类的方法
def p01(self):
print(f"{self.name}hahaha")
def p01(self,age):
print(f"{self.name}hahaha,{age}")
stu = student("laoyu",18)
print(stu.name)
print(stu.age)
stu.play()
stu.p01(10)
6.时间日期类
from datetime import datetime
dt = datetime(2022,3,30)
print(dt)
print(dt.year)
print(datetime.now())
s1 = "2022-03-01"
dt2 = datetime.strptime(s1,'%Y-%m-%d')
print(type(dt2))
7.文件读写
# 文件读写
# 1.读文件
# 直接读,要手动关
f = open(r"D:\ \python-sk\data\1.txt")
res = f.read()
print(res)
f.close()
# with open 不用手动关
with open(r"D:\ \python-sk\data\1.txt") as f:
f.read()
print(res)
# 一行一行读
f = open(r"D:\ \python-sk\data\1.txt")
line = f.readline()
while(line):
print(line,end="")
line = f.readline()
f.close()
f = open(r"D:\ \python-sk\data\1.txt")
for line in f:
print(line,end="")
f.close()
with open(r"D:\ \python-sk\data\1.txt") as f:
line = f.readline()
while (line):
print(line, end="")
line = f.readline()
# r读 w写 a追加
with open(r"D:\ \python-sk\data\1.txt",mode='a') as f:
# 覆写
f.write("a,a,a,a\n")
# 覆写多行
f.writelines(["1,1,1,1\n","2,2,2,2\n"])