python学习基础语法总结

一 开发环境搭建

 

 

 

python学习基础语法总结

  1. 进入Python官网:www.python.org,下载与自己电脑适配的版本安装
  2. 安装完成后,验证安装是否成功。打开黑窗口,输入python-v命令,查看python版本

 二 数据类型

 

python学习基础语法总结

 

 三 数据类型

 

python学习基础语法总结

 四 运算符

 

python学习基础语法总结

 

 五 输入与输出

 

python学习基础语法总结

 

 六 程序结构

 

python学习基础语法总结

 七 组合数据类型

列表

 

python学习基础语法总结

 元组

 

 

python学习基础语法总结

 

集合

 

python学习基础语法总结

 

字典

 

python学习基础语法总结

 

八 辅助函数

 

python学习基础语法总结

python学习基础语法总结

python学习基础语法总结

九 函数操作

 

python学习基础语法总结

 

十 字符串

字符串查询

python学习基础语法总结

字符串拆分

 

python学习基础语法总结

 

字符串大小写切换

python学习基础语法总结

字符串对齐

python学习基础语法总结

字符串替换 

 

python学习基础语法总结

 

字符串内容判断

python学习基础语法总结

 

字符串空格处理

python学习基础语法总结

 

字符串占位

python学习基础语法总结

python学习基础语法总结

python学习基础语法总结

 字符串切片

python学习基础语法总结

python学习基础语法总结

十一 文件IO 操作

python学习基础语法总结

'''
普通文件操作方式
    1. 文本文件写入
    2. 文本文件的读取

    3. 二进制文件的操作
'''
# 1. 将程序中的数据,写入到文件中
file = open('./data/1.1.text', 'w', encoding='UTF-8')
# 程序中有一个字符串
message = 'hello 世界'
# 将数据写入到文件中
file.write(message)
# 关闭文件
file.close()

# 2. 将文件中的数据,读取到程序中
# 按照只读的方式打开文件
file = open(file='./data/1.1.text', mode='r', encoding='utf-8')
# 从文件中读取数据,展示到控制台中
info = file.read()
print(info)
# 关闭文件
file.close()

# 2. 文本文件的追加
file = open(file='./data/1.2.text', mode='a', encoding='UTF-8')
# 要操作的文本数据
# message = '人说,林深时见鹿,海蓝时见鲸,夜深时见你.'
# file.write(message)

# message2 = "\n但是,林深时雾起,海蓝时浪涌,夜神时梦续.\n"
# file.write(message2)

# message3 = "\r\n你可知:鹿踏雾而来,鲸随浪而起,你未曾转身,怎知我已到来..\r\n"
# file.write(message3)

# 关闭文件
file.close()

# 4. 二进制文件的操作
# 'E:/WORK_IMG/lihen/a.jpg'
# 读取计算机中的二进制文件数据
file = open(file='E:/WORK_IMG/lihen/a.jpg', mode='rb')
# 读取数据到程序中
# 双引号字符串前面有个字母b表示是二进制数据、字母u表示Unicode数据
# \x开头的是十六进制数据、\o开头的是八进制数据
print(file.read())

# 将数据重新存储到我们指定的位置
file2 = open(file='./data/test.jpg', mode='wb')
file2.write(file.read())
# 关闭文件2
file2.close()

# 关闭文件
file.close()

# 5. 重要:文件的快捷操作:with语法
with open('E:/WORK_IMG/lihen/a (20).JPG', 'rb') as file1:
    # 打开文件,将文件对象赋值给变量file1,with中的代码执行完成,文件file1自动关闭
    with open('./data/' + file1.name[file1.name.rfind('/'):], 'wb') as file2:
        # 将读取的文件存储到指定的文件夹中
        file2.write(file1.read())
# FileNotFoundError: [Errno 2] No such file or directory: 'E:/WORK_IMG/lihen/a(71).jpg'
# 文件没有发现你的错误  [错误代号:2]

 Python对象的操作

python学习基础语法总结

'''
程序中的数据,存储到文件中
'''
# 基本数据类型、组合数据类型
# 1. 将程序中的字典数据,转换成字符串存储到文件中
users = {'admin': {'username': 'admin', 'password': '123', 'nickname': '老刘'}}

# 类型能否直接转换字符串?
users_str = str(users)

# 存储到文件中
with open('./data/2.1.text', 'w') as file:
    file.write(users_str)


# 2. 将文件中的字符串数据,读取到程序中
with open('./data/2.1.text', 'r') as file:
    users = file.read()
    print(users, type(users))
    # 将字符串数据,转换成字典:该字符串的格式~和python中的字典的表达式一致
    users = eval(users)
    print(users, type(users))
    print(users.get('admin'))

json模块 

'''
python中提供一个特殊的模块,可以直接对python
    中的数据进行序列化操作
        序列化:按照指定的数据顺序定义数据格式【类似编码】
'''
import json

# # 准备操作的数据
users = {'admin': {'username': 'admin', 'password': '123', 'nickname': '老刘'}}
# 1. 将程序中的数据,直接存储到文件中
# json模块的操作
with open('./data/3.1.json', 'w') as file:
    json.dump(users, file)

# 2. 将文件中的数据,读取到程序中
with open('./data/3.1.json', 'r') as file:
    users = json.load(file)
    print(users, type(users))

 Python对象的序列化操作

python学习基础语法总结

 marshal模块

'''
python程序将多个数据存储到文件中
'''
# 数据准备
s = "字符串"
i = 19
f = 3.1415
b = True
c = 12 + 5j
l = [1,2,3]
d = {'username': 'admin', 'password': '123'}

x = [s, i, f, b, c, l, d]

# 存储多个数据的模块:marshal
import marshal

# 1. 存储多个数据到文件中
with open('./data/4.1.dat', 'wb') as file:
    # 第一次存储一个数量:有多少个数据存储到了文件中
    marshal.dump(len(x), file)
    # 存储每个数据
    for x1 in x:
        marshal.dump(x1, file)

# 2. 将多个数据从文件中依次取出
with open('./data/4.1.dat', 'rb') as file:
    n = marshal.load(file)

    # 将所有数据依次取出
    for x in range(n):
        print(marshal.load(file))

shelve模块

'''
python存取多个数据到文件中

    如果按照常规方式~open/write/read方式,每个独立的数据都的单独存储 一个文件
        1. str()数据:字符串:文件存储->文件读取:eval():数据
        2. 数据:json.dump():文件存储->json.load():数据
    如果要存储多个数据到一个文件中
        3. 多个数据:总数量:marshal.dump()->marshal.load(fp):总数量:循环->marshal.load(fp)依次读取

    多个数据,也可以按照比较友好的like dict的方式,将数据存储到文件中
        可以将数据在文件中,通过key值直接获取对应的数据
'''
# 数据准备
users = {'admin': {'username': 'admin', 'password': '123', 'nickname': '大牧'}}
articles = {'标题': {'title': '标题', 'content': '文章内容', 'author': users.get('admin')}}

import shelve

file = shelve.open('./data/5.1')

# 1. 将多个数据按照key:value的形式存储到文件中
# file['users'] = users
# file['articles'] = articles

# 2. 从文件中根据key读取数据
print(file['users'], type(file['users']))