Python学习笔记
知识图谱

一、Python的基础环境及语法基础
python的运行环境:python解释器、Anaconda
? python环境可以多版本共存
Python的IDE:DOS\IDE\TXT\PYCHARM\JUPYTER\VSCODE\ECLIPSE
语法规则:
1、引入工具包
import package
from xx import package
import package as pk
2、标识符:用于标识某一实体的符号,用于给变量、函数命名
python标识符命名规则
由字母、下划线、数字组成
不能由数字开头
不能和关键字重名
3、关键字
获取方式:
import keyword
keyword.kwlist
4、变量
pyhon中的变量是对数据存储地址的引用
给变量赋值,并不是将数据赋予变量,二十将变量指向了数据存储的地址
5、命名法则:驼峰命名、匈牙利命名法
6、变量
适用范围:局部变量–定义在函数内的变量,作用范围函是数内部范围
? 全局变量-定义在函数和对象外部,作用范围是整个模块
a=10
def func():
b=10
7、注释:对代码的解释说明
"""
多行注释
多行注释
"""
二、基础函数
print,用于输出,可以通过end指定结尾符号
input,用于接收用户输入
print("python",end='---')
dir,返回对象的属性和方法列表
id,查看对象的内存地址
type,用于查看对象类型、作为元类使用
del,删除对象
len,查看数据长度
range,用于生成序列
for a in range(3):
for i in range(4):
name1 = input("name:")
print(name1)
del name1

三(1)数据类型
六种内置数据类型
Number(数值)\String(字符串)\List(列表)\Tuple(元组)\Dictionary(字典)\Set(集合)
1.Number(数值):int,float,bool,complex(实数+虚数j)
数据类型分类:有序(有下标)、无序(无下标)、可变(可以被修改)、不可变(不可被修改)

运算符:
-
+加
-
-减 -
*乘 -
/除 -
取余/取整 % // -
**乘方 -
不同类型数字进行计算,结果为精度较高的类型
计算函数
-math模块
abs()绝对值,round()四舍五入,divmod(y,x)返回两个数值的商和余数,max(),min()求取最值,sum()求和

2.String(字符串)
创建字符串:‘aaa’, “aaa”,’’‘aaa aaa’’’,""“aaa aaa aaa”""


索引和下标
str="abcde"
str[0]
切片:批量获取
str="python"
str[起:止:步长]
str[-4:-1:1]
str[::]

3.List(列表)
列表是有序可变的序列,安全性差,但灵活性高
Python的内置数据结构中没有数组,需要import numpy工具包实现
列表支持的运算符和元组一样
List = list((obj1,obj2,obj3…))
List = [obj1,obj2,obj3…]


4.Tuple(元组)
元组是有序不可变序列,一旦生成无法更改
Tuple = (obj1,obj2,obj3…)

元组的拼接
t = (1,2,3)
t+t #(1,2,3,1,2,3)
t*2 #(1,2,3,1,2,3)
5.Dictionary(字典)
字典的每个元素由两部分组成 键-值
键不可变且唯一,如果有相同的键则后面的键对应的值会把前面的值覆盖
数据量大时,字典数据的访问速度比列表快
字典是无序可变的序列
Dict = {k:v,k1:v1…}
Dict = dict(k=v,k1=v1…)
Dict = dict([(k,v),(k1,v1)…])


6.Set(集合)
集合中的元素是唯一的
Set = set()
Set = {obj1,obj2,…}


数据拷贝

因为L2 = L1 只是把L2指向了L1所指向的地址
为了防止出现上述问题,可以拷贝生成一份新数据
深拷贝&浅拷贝


运算符
比较运算符:> < == != >= <=
赋值运算符: += -= = /= %= *= //=
? 多变量赋值 a,b,c=1,2,3 A=B=C=1
逻辑运算符:and or not
位运算符:
成员运算符:in 、not in 
身份运算符:

三(2)数据结构
线性表、数组、链表、队列、树、图、堆栈、散列表
四、判断语句&循环语句
判断语句
if 判断条件:
对的执行
else:
错的执行
if 判断条件:
对的执行
elif 判断条件2:
对的执行
else:
错的执行
循环语句
for iter in iters:
循环语句块
else:
语句块
while 条件语句:
循环执行语句块
else:
语句块
n=0
while n<100:
if n==24:
continue
if n==54:
break
我错了
n=n+1
for i in range(1,13):
for j in range(1,i+1):
print("%s x %s = %d\t"%(j,i,i*j),end='')
if i==j:
continue
if j==12:
break
print('')
reak结束整个循环,如果触发了,循环对应的else也不会执行 我错了 n=n+1
#12以内乘法表------------------------- for i in range(1,13): for j in range(1,i+1): print("%s x %s = %d\t"%(j,i,i*j),end=’’) if ij: continue if j12: break print(’’) #默认end=’/n’ 换行
#空循环:循环体是空的,不执行任务 死循环:不停执行循环
#递归:程序调用自身的编程技巧称为递归
|