标签:sort join etc byte 路径 python入门 into get 必须
一.函数返回值
1.函数如果返回多个值,他会把这几个值放到一个元组里面
2.也可以用多个变量来接收
返回多个值放到元组里面
def say():
num1=1
num2=2
num3=3
return num1,num2,num3
res = say()
print(res)
多个变量接收
res1,res2,res3 = say()
print(res1)
print(res2)
print(res3)
二.匿名函数
匿名函数,这个函数功能很简单,只用一次
lambda res = lambda x:x+1#冒号后面的是函数体,也是函数的处理逻辑,冒号前面的返回值 print(res(1))
三.列表生成器
import random
red_num= random.sample(range(1,34),6)
#外面是小括号的话,他就不是一个list了,他是一个生成器
# 生成器比list要节约内存,他是每次循环的时候,会根据规则计算一个元素,放到内存里面。
# list他是把所有的元素都放在内存里面。
l = [i for i in range(1,101,2)]#生成100以内的奇数
new_num = [str(num).zfill(2)for num in red_num] #列表生成式
print(new_num)
print(l)
#生成器例子
l = (i for i in range(1,101,2))
for l2 in l:
print(l2)
print(l.__next__)#表示每次打印一个值
# #下面的这一串与上面的【列表生成式】的作用一样
for num in red_num:
temp = str(num).zfill(2)
new_num.append(temp)
print(new_num)
# #练习一(列表生成式)
a = 5
b = 4
if a>b:
c= a
else:
c= b
c = a if a>b else b #三元表达式
四.字典排序
d = {‘a‘:4,‘c‘:2,‘b‘:3}
#字典是无序,直接对字典排序是不存在的
print(d.items())
print(sorted(d.items()))#sorted帮我们循环调用生成了一个二维数组,默认是按照key排序的
res=sorted(d.items(),key=lambda x:x[0])#根据key排序
res1=sorted(d.items(),key=lambda x:x[1])#根据value排序
print(res)
print(res1)
for k,v in res:#循环二维数组
print(k,v)
l =[
[1,2,3,4],
[1,2,4,3]
]
for a,b,c,d in l:
print(a,b,c,d)
def my(name:str):
print(name)
my([‘123‘,‘456‘])
五.os常用模块
# os模块常用方法:
os.listdir("d:\\")#列出目录下所有的文件夹和文件
os.remove()#删除文件
os.rename(ols,new)#重命名
print(os.sep)#当前操作系统的路径分隔符
print(os.path.dirname("e:\\syz\\lpy-code"))#获取父目录,获取上一级目录
print(os.path.exists("/user/local"))#目录/文件是否存在
print(os.path.isfile("lpy.py"))#判断文件是否存在,判断是不是文件
print(os.path.isdir("e:\\syz"))#是否是一个路径,目录是否存在
size = os.path.getsize(‘x.py‘)#获取文件的大小
res = os.system(‘ipconfig‘)#执行操作系统命令,但获取不到结果
res = os.popen(‘ipconfig‘).read()#可以获取到命令执行的结果
print(os.path.abspath(__file__))#获取绝对路径
print(os.path.dirname("e:\\syz\\lpy-code"))#获取父目录,获取上一级目录
print(os.path.isfile("lpy.py"))#判断文件是否存在,判断是不是文件
print(os.path.isdir("e:\\syz"))#是否是一个路径,目录是否存在
size = os.path.getsize(‘x.py‘)#获取文件的大小
# 可用于限制上传文件大小
print(os.path.join("root",‘hehhe‘,‘a.sql‘))#拼接成一个路径
for abs_path,dir,file in os.walk(‘E:\syz\syz-code\day6‘):
print(abs_path,dir,file)
# abs_path当前循环的绝对路径
# dir目录下所有的文件夹[ ]
# file目录下所有的文件[]
六.导入模块的顺序与方法
python导入模块时的顺序:
1.从当前目录下找需要导入的python文件,第一顺序
2.从python的环境变量中找 sys.path,第二顺序
导入模块的实质:
就是把导入的这个python文件从头到尾执行一遍
#方法一: import lpy print(lpy.name) lpy.my() #方法二: from lpy import my,name my() print(name) # 方法三: from lpy import * #所有的,最好不用这个方法,可能导入多个模块不好判断后面调用的函数属于哪个模块 my() print(name)
七.sys模块
import sys print(sys.platform)#判断操作系统 print(sys.path)#python的环境变量 sys.path.append(‘../day5‘)#导入 其他目录下的模块,要先把目录加入环境变量 print(sys.path) import zuoye sys.path.insert(0,word,‘E:\syz\syz-code\day5‘) print(sys,path)
print(sys.argv)#用来获取命令里面运行python文件的时候传入的参数,它是一个list,这个list默认就有一个参数,就是当前的文件名
八.时间模块
1.时间戳:从unix元年到现在过了多少秒
2格式化好的时间
# 先转成时间元组
import time
print(time.time())#获取当前时间戳
# time.sleep(10)
today = time.strftime(‘%Y-%m-%d %H:%M:%S‘)
print(today)
print(time.gmtime())#默认取的是标准时区的时间
s = print(time.localtime(1514198608))#取的是当前时区的时间
print(time.strftime(‘%Y-%m-%d %H:%M:%S‘,s))
# 时间戳转换时间元组
# 1.时间戳转成时间元组 time.localtime()
# 2.再把时间元组转成格式化的时间
def timestamp_to_fomat(timestamp=None,format=‘%Y-%m-%d %H:%M:%S‘):
# 1.默认返回当前格式化好的时间
# 2.传入时间戳的话,把时间戳转化成格式化好的时间,返回
if timestamp:
time_tuple= time.localtime(timestamp)
res = time.strftime(format,time_tuple)
else:
res = time.strftime(format)#默认取当前时间
return res
2018-4-21
tp = time.strptime(‘2018-4-21‘,‘%Y-%m-%d‘)#把格式化好的时间转成时间元组的
print(time.mktime(tp))#把时间元组转成时间戳
def strToTimestamp(str=None,format=‘%Y%m%d%H%M%S‘):
#20180421165643
#默认返回当前时间戳
if str:
tp = time.strptime(str,format)#转成时间元组
res = time.mktime(tp)#再转成时间戳
else:
res = time.time()#默认取当前是按戳
return int(res)
print(strToTimestamp())
print(strToTimestamp(‘20181129183859‘))
import datetime
print(datetime.datetime.today())#获取当前时间精确到秒
print(datetime.date.today())#精确到天
res = datetime.date.today()+datetime.timedelta(days=5)#获取到5天后,-5五天前
res = datetime.date.today()+datetime.timedelta(minutes=5)#获取到5分钟后,-5五天前
print(res)
九.加密模块
import hashlib
# 字符串不能直接加密
m = hashlib.md5()
passwd = ‘123‘
passwd.encode()#把字符串专车个bytes类型
m.update(passwd.encode())#不能直接对字符串加密,要先把字符串转成bytes类型
print(m.hexdigest())
def my_md5(str):
new_str = str.encode()#把字符串转成bytes类型
m = hashlib.md5()#实例化md5对象
m.update(new_str)#加密
areturn m.hexdigest()#获取结果返回
m = hashlib.sha256()
m.update(passwd.encode())
十.操作mysql
# 1.连接上数据库 账号,密码,IP,端口号,数据库
# 2.建立游标
# 3.执行sql
# 4.获取结果
# 5.关闭游标
# 6.连接关闭
import pymysql
coon = pymysql.connect(
host=‘118.24.3.40‘,user=‘jxz‘,passwd=‘123456‘,
port=3306,db=‘jxz‘,charset=‘utf8‘
#port必须写成int类型
#charset这里必须写成utf8
)
cur = coon.cursor()
# cur.execute(‘select * from stu;‘)
cur.execute(‘insert into stu (id,name,sex) VALUE (1,"koukou","女");‘)
# delete update insert 必须commit
coon.commit()
res = cur.fetchall()
print(res)
cur.close()
coon.close()
十一.excel模块
import xlwt book = xlwt.Workbook()#新建一个excel sheet = book.add_sheet(‘sheet1‘)#加sheet页 sheet.write(0,0,‘姓名‘)#行,列,要写入的内容 sheet.write(0,0,‘年龄‘) sheet.write(0,0,‘性别‘) book.save(‘stu.xls‘)
标签:sort join etc byte 路径 python入门 into get 必须
原文地址:https://www.cnblogs.com/koukoupy/p/8992355.html