基础

2022-07-26

基础

  • 1. Python基础
    • 1.1. 安装
    • 1.2. 基本使用
      • 1.2.1. print功能
      • 1.2.2. 基础数学运算
      • 1.2.3. 变量variable
    • 1.3. while和for循环
      • 1.3.1. while循环
      • 1.3.2. for循环
    • 1.4. if判断
    • 1.5. 函数
    • 1.6. 变量作用域
    • 1.7. 模块安装
    • 1.8. 文件读写
    • 1.9. class类
      • 1.9.1. 定义
      • 1.9.2. 初始化
    • 1.10. input输入
    • 1.11. 元组、列表、字典
      • 1.11.1. 元组
      • 1.11.2. 列表
      • 1.11.3. 多维列表
      • 1.11.4. 字典
    • 1.12. 模块
      • 1.12.1. 导入模块
      • 1.12.2. 自定义模块
    • 1.13. 其它
      • 1.13.1. continue, break
      • 1.13.2. try错误处理
      • 1.13.3. zip, lambda, map
      • 1.13.4. 索引复制,浅复制copy,深复制deepcopy
      • 1.13.5. 多线程
      • 1.13.6. 多进程
      • 1.13.7. tkinter 窗口模块
      • 1.13.8. pickle保存数据
      • 1.13.9. set
      • 1.13.10. 正则表达式

参考
莫烦PYTHON

1. Python基础

1.1. 安装

python安装:python.org
第三方包安装:pip3 install xxx

1.2. 基本使用

1.2.1. print功能

print(int(‘3’)+float(‘3.2’)+3)

1.2.2. 基础数学运算

运算符:+ - * / % // **

1.2.3. 变量variable

apple = 1
apple_egg = 10
appleEgg = 12 + 3
a, b, c = 1, 2, 3

1.3. while和for循环

1.3.1. while循环

1.3.2. for循环

for i in example_list
for i in range(1, 10)

1.4. if判断

if: > < >= <= == !=
if else
if elif else

1.5. 函数

定义:def fun():
函数参数:def fun(a, b):
函数默认参数:def func(a, b=3):
函数返回值:return a

1.6. 变量作用域

全局、局部

1.7. 模块安装

安装:pip3 install numpy
卸载:pip3 uninstall numpy
更新:pip3 install -U numpy

1.8. 文件读写

打开:my_file = open(‘my_file.txt’, ‘w’)
写入:my_file.write(‘aaa’)
关闭:my_file.close()
追加打开:open(‘name’, ‘a’)
读取:my_file.read()
读取一行:my_file.readline()
读取所有行:my_file.readlines()

1.9. class类

1.9.1. 定义

class Calculator:
	price = 18
	def add(self, x, y):
		print(x+y)

1.9.2. 初始化

	def __init__(self, price, width=18):
		self.price = price
		self.w = width

1.10. input输入

a_input = input(‘Please give a number:’)

1.11. 元组、列表、字典

1.11.1. 元组

元组不可改变,列表可以改变
a_tuple = (1, 2, 4, 3)
a_list = [1, 2, 4, 3]

1.11.2. 列表

a = [1, 2, 4, 3]
添加:a.append(0)
插入:a.insert(1, 0)
移除元素:a.remove(1)
索引:a[0], a[-1], a[0:3]
查找索引:a.index(2)
元素计数:a.count(2)
排序:a.sort(), a.sort(reverse=True)

1.11.3. 多维列表

multi_dim_a = [[1,2,3],
				[2,3,4],
				[3,4,5]]

1.11.4. 字典

d = {'apple':1,'pear':2,'orange':3}
print(d['apple'])

删除元素:del d[‘pear’]
添加元素:d[‘b’] = 20

1.12. 模块

1.12.1. 导入模块

import time
import time as t
from time import time, localtime
from time import *

1.12.2. 自定义模块

任何脚本都可以当成一个模块
模块存储路径:1. 当前文件夹 2. 外部路径site-packages,例如:/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages

1.13. 其它

1.13.1. continue, break

continue: 直接进入下一次循环
break: 结束循环
pass: 什么都不做

1.13.2. try错误处理

try:
    f = open('1.txt', 'r')
except Exception as e:
    print(e)
else:
    #f.write('sss')
    print(f.read())
    f.close()

1.13.3. zip, lambda, map

zip: 竖向合并多个列表,生成一个新列表,列表元素为tuple
lambda: 定义函数的快捷方式
map: 把函数与参数绑定在一起

a = [1,2,3]
b = [4,5,6]
print(list(zip(a,b))) # [(1, 4), (2, 5), (3, 6)]

fun1 = lambda x, y: x + y
print(fun1(1, 2)) # 3

print(list(map(fun1, [1, 3], [2, 5]))) # [3, 8]

1.13.4. 索引复制,浅复制copy,深复制deepcopy

id: 对象在内存中的地址
a = [1, 2, 3]
b = a # 索引复制
b = copy.copy(a) # 浅复制,仅复制第一层
b = copy.deepcopy(a) # 深复制,复制所有层

1.13.5. 多线程

import _thread
import time

def print_time(threadName, delay):
    count = 0
    while count < 5:
        time.sleep(delay)
        count += 1
        print("%s: %s" % (threadName, time.ctime(time.time())))

try:
    _thread.start_new_thread(print_time, ('Thread-1', 2))
    _thread.start_new_thread(print_time, ('Thread-2', 4))
except:
    print('Error: 无法启动线程')

1.13.6. 多进程

1.13.7. tkinter 窗口模块

tkinter窗口模块

1.13.8. pickle保存数据

保存:pickle.dump()
读取:pickle.load()

import pickle

#a_dict = {'da':111, 2:[23,1,4], '23':{1:2, 'd':'sad'}}

#file = open('pickle_example.pickle', 'wb')
#pickle.dump(a_dict, file)
#file.close()

with open('pickle_example.pickle', 'rb') as file:
    a_dict1 = pickle.load(file)
    print(a_dict1)

1.13.9. set

set主要功能:寻找list当中不同的元素

char_list = ['a', 'b', 'c', 'c', 'd', 'd', 'd']
print(char_list)
print(set(char_list))
print(type(set(char_list)))
print(type({'key':'value'}))
print(type([1, 2]))

unique_char = set(char_list)

##添加
unique_char.add('e')
##删除
unique_char.remove('k')
unique_char.discard('k')
unique_char.clear()
##不同
unique_char.difference(set2)
##相同
unique_char.intersection(set2)

1.13.10. 正则表达式

主要用来匹配字符

本文地址:https://blog.csdn.net/gutsyfarmer/article/details/110799894

《基础.doc》

下载本文的Word格式文档,以方便收藏与打印。