python基础
python介绍
python语言:python语法风格
python解释器:专门来识别python这门语法风格并解释执行的一个应用程序
运行python的两种方式
- 交互式:即时得到程序的运行成果,多用于调试
- 脚本式:程序写到
.py
的文件中,然后使用python解释器
执行其中的内容
python应用程序的运行的三个步骤
- 先启动python解释器
- 解释器会发送系统调用,把
.py
的文件内容加载到内存中,此时内容全为普通字符,无任何语法意义 - 解释器开始解释执行读入的
.py
文件代码,开始识别python语法
对比文本编辑器:
1. 先启动文本编辑器
2. 文本编辑器会发送系统调用,把`.py`的文件内容加载到内存中
3. 文本编辑器将文件内容输出到显示器上让用户看到。
二者前两步都是一样的,先启动应用程序,然后将内容加载到内存中。差异在于第三步的python文件内容的处理方式上
变量
- 基本使用:原则是先定义,后引用
name = 'lixiao' # 存入
print(name) # 读取
# 如果先引用后定义会出现错误 NameError: name 'name' is not defined
- 内存管理:垃圾回收机制,垃圾是指
当一个变量值被绑定的变量名的个数为0时,该变量无法访问
称之为垃圾
# 引用计数增加
x = 20 # 20的引用数为1
y = x # 20的引用数为2
z = y # 20的引用数为3
# 引用计数减少
del x # 20的引用数为2
del z # 20的引用数为1
# del y # 20的引用数为0
# y = 10 # 20的引用数为0
- 变量组成:变量名+赋值符+变量值
- 变量的命名规则:原则是见名知其意
- 字符、数字、下划线任意组成
- 首字符不能为数字
- 关键字不能作为变量名
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from','global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
- 变量名的命名风格
- 纯小写+下划线
- 驼峰命名
- 变量的三个特征
- id:变量值的内存地址
- value:值本身
- type:值的类型
name = "hf" print(id(name)) # 48254336 print(type(name) # <type 'str'> print(name) # hf
python中用户的输入
- python3中 input会将输入的所有内容全部转为字符串类型
- python2中 raw_input()和python3中一样,input()要求用户必须输入一个明确的数据类型
- 字符串的格式化输出
# 1、 %百分号,值与%s的位置一一对应,少一个或多一个都不行, # 1.1 %s可以接收任意类型 res = "my name is %s and age is %s" %("hyf",66) print(res) # my name is hyf my age is 66 # 1.2 字典形式传值 res = "my name is %(name)s and age is %(age)s" %{"name":'hyf',"age":88} print(res) # my name is hyf my age is 88 # 2、str.format():兼容性好 # 2.1 按照位置传值 res = "my name is {} and age is {}".format("hyf",55) print(res) # my name is hyf and age is 55 # 2.2 按照索引传值 res = "my name is {0}{0} and age is {1}".format("hyf",55) print(res) # my name is hyfhyf and age is 55 # 2.3 按照key=value传值 res = "my name is {name} and age is {age}".format(name="hyf",age=44) print(res) # my name is hyf and age is 44 # 2.4 f:python3.5以后才推出。{}内的字符串可以被当做表达式运行 name = "hyf0001" age = 89 res = f"my name is {name} and age is {age}" print(res) # my name is hyf0001 and age is 89 res=f'{10+3}' print(res) # 13 f'{print("aaaa")}' # aaaa
- 填充与格式化:
{下标:对齐方式填充}.format('xxx')
- *<10:左对齐,不够10位用
*
号代替
print('{0:*<10}'.format('开始执行')) # 开始执行******
- *>10:右对齐,不够10位用
*
号代替
print('{0:*>10}'.format('开始执行')) # ******开始执行
- *^10:居中对齐,不够10位用
*
号代替
print('{0:*>10}'.format('开始执行')) # ***开始执行***
- *<10:左对齐,不够10位用
- 精度与进制
print('{0:.3f}'.format(3945.21234)) # 保留三位小数 print("{0:b}".format(231)) # 二进制 11100111 print("{0:o}".format(231)) # 8进制 347 print("{0:x}".format(17)) # 16进制 11 print("{0:,}".format(28394102)) # 千分位 28,394,102
可变不可变类型
- 可变类型:值改变,id不变,list,dict
- 不可变类型:值改变,id跟着改变,证明产生新值。int、float、str
运算符
- 算数运算符:
+、-、*、/(带小数)、%(取余数)、//(取整数部分)、**(平方)
- 比较运算符:
<、>、=、<=、>=、==、!=
- 赋值运算符:
- =:变量赋值
- 增量赋值:
+=、*=、**=
- 链式赋值:
x=y=z=10
- 交叉赋值:
x,y = y,x
- 解压赋值:
arg_list = [1,2,3]
value1,value2,value3 = arg_list
,多一个少一个都不行- 引入
*
:可以取两头的值,无法取中间的值value1,*_ = arg_list # print(_) [2,3] *_,value3 = arg_list # print(_) [1,2] value1,*_,value3 = arg_list # print(_) [2]
- 解压字典默认解压出来的是字典的key
dict_arg = {"name":"hyf","age":33} x,y = dict_arg print(x,y) # name age
- 逻辑运算符
- not:将紧跟其后的条件取反
- and:两侧条件都为真时才为真
- or:两侧条件只要有一方为真整个条件语句为真
- 优先级not>and>or,然后从左到右依次运算
- 成员运算符
- in:
print("hello" in "hello word") # True
- not in:
print("hellos" not in "hello word") # True
- in:
- 身份运算符
- is: 判断的是id是否相等
条件
- 显示条件:比较运算符和布尔值
True、False
- 隐式条件: 除了0、None、空(空列表、空字典、空字符串)为False,其他都为True
流程控制语句
-
if语句
if 条件: # do
if 条件: # do else: # do
if 条件1: # do elif 条件2: # do elif 条件3: # do else: # do
-
while语句
2.1 基本使用while 条件: 代码1 代码2
2.2 死循环与效率问题:纯计算无IO操作的死循环会导致致命的效率问题
2.3 退出循环的两种方式:- 方式一:将条件改为False,等到下次循环判断条件时才会生效
- break,只要运行到break就会立刻终止本层循环,嵌套结束时注意每一层都必须配一个break
2.4
while +continue
:结束本次循环,直接进入下一次强调:在continue之后添加同级代码毫无意义,因为永远无法运行
2.5
while +else
:针对break,while循环结束并且未被break打断的情况下才会执行 -
for循环
-
使用:
for key in 可迭代对象:# 列表、字典、字符串、元组、集合 代码1 代码2 代码3
总结for循环与while循环的异同:都是循环,for循环可以干的事,while循环也可以干,不同之处在于while循环称之为条件循环,循环次数取决于条件何时变为假,for循环称之为"取值循环",循环次数取决in后包含的值的个数
-
for循环控制循环次数:
range(start,end,step)
for i in range(30): print(i)
-
for+else、for+break、for+continue
同while操作
-
len(list)
:获取列表长度
-
print()
print
逗号使用print('hello','world','egon') # hello world egon
- 换行符
print('hello\n') print('world')
print
值end
参数的使用print('hello',end="*") print('world') # hello*world
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署