Python重拾

1.Python标识符规则

字母,下划线,数字;数字不开头;大小写区分;不能用保留字(关键字)

2.保留字有哪些

python 复制代码
import keyword
print(keyword.kwlist)

'''['False', 'None', 'True', 'and',
 'as', 'assert', 'async', 'await', 
'break', 'class', 'continue', 'def', 
'del', 'elif', 'else', 'except', 
'finally', 'for', 'from', 'global', 
'if', 'import', 'in', 'is', 'lambda',
 'nonlocal', 'not', 'or', 'pass', 'raise',
 'return', 'try', 'while', 'with', 'yield']'''

3.注释与缩进

''' """

tab Python注重缩进换行,一般缩进是有从属关系;

Python一般用换行,不用分号,除非非要两个句子写在一行上面

多行语句:

python 复制代码
# 多行语句(三种情况)

print("换行自动生成引号"
      "多"
      "行"
      "")

print("多\    #这里没加号,直接斜杠就行
      行")

a = [1,
     2,
     3]#列表变量(数组)
b = (1,
     2,
     3)#元组变量(不可改的数组)
c={"k1":0,
"k2":4} #字典

4.基本数据类型及操作


5.语句

5.1判断结构

python 复制代码
if (2 < 3) and (1 > 0):
    print("yes")
elif (2 == 3) or (1 == 0):
    print("eql")
else:
    print("no")

listA = ["ni", "hao", "p", "y"]
if "p" in listA:
    print("yes")
else:
    print("no")

1.不用括号,用缩进

2.注意连接词是and和or,不是&&和||

3.可以直接 ××in×

5.2循环结构

python 复制代码
# 循环
listB=[0,1,2,3]
#循环下标
for i in range(0,len(listB),1):
    if listB[i]>1:
        print(listB[i])
    else:
        continue#continue是跳过此次,进入下次;break是全部终止,没有下次


#循环列表内容   
for i in listB:
    if i>1:
        print(i)
    else:
        continue

6.函数

1.函数声明:

python 复制代码
def func1(list1):
    list1.reverse()
    return
listC=["hh","jj","oo"]
func1(listC)
print(listC)



def func2(a,b):
    if a<b:
        return a
    else:
        return b
min=func2(1,2)
print(min)

7.输入输出语句

python 复制代码
#输入1------input默认是str
a=input()

#输入2------字符型列表
list1=input().split(" ")  """以空格为分隔,a是字符型列表"""
b=list1[0]

#最原始的输入(input的底层,这样写会比直接input内存小点)
import sys
list2=sys.stdin.readline().split() #注意这里的split括号里面没有东西!


"""========================================================================="""

#输出
print(a,b,list1[0])
print("这是a")
print("这是{}".format(a))


其他:

python没有++--,有+=1

相关推荐
默_笙3 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
qq_426003963 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技3 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时3 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
C语言小火车3 天前
C/C++ 为什么需要编译器?
开发语言·c++
奇思妙想聪明勤奋的小羊3 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1233 天前
2026年第38周GitHub趋势周报
python·科技·github