B站教学链接 点击进入
Python语法
#输出
print("dlkjfs");
a=10
age=18;
print("sdfs %d dlfj"%age);
print("dfj%s %s"%("小张","dfjd"))
print("aaa","bbb","kkk")
print("www","baidu","com",sep=".")
print("hello",end="")
print("hello",end="\t")
print("hello",end="\n")
'''
'''
#输入
password= input("dlfj")
print("lsdjf",password)
a=input("lsdjfs")
print(type(a))
a=int("123")
int()#强制类型转化
b=a+100
print(b)
'''
'''
#判断
if True:
print("ldjf")
else :
print("fjdslf")
import random
x=random.randint(0,2)#随机数
print(x)
#循环
for i in range(5):
print(i);
for i in range(0,14,3):#从0开始到13结束
print(i)
name="chengdu"
for x in name:
print(x,end=' ')
a=["aa","bb","cc"]
for i in range(len(a)):
print(a[i])
i=0
while i<5:
print("当前第几次%d"%(i+1))
print("i=%d"%i)#注意是百分号
i+=1
i=1
sum=0
while i<=100:
sum+=i;
i+=1
print(sum)
count=10
while count<5:
print(count,"xiaoyu5")
count+=1
else:
print(count,"大于或对于5")
break
continue
for i in range(1,10,1):
print(\t)
for j in range(1,i+1,1):
print(%d*%d=%d %(i,j,i*j),end=\t)
for x in range(1, 10):
print(\t)
for y in range(1, x + 1):
result = x * y
print( % d * % d = % d % (x, y, x * y), end =\t)
for m in range(1,10):
for n in range(1,10):
if m n:
print(%s*%s=%s%(m,n,m*n),end= \t)
elif m == n:
print(%s*%s=%s%(m,n,m*n),end=\n)
for i in range(1, 10, 1):
for j in range(1, i + 1, 1):
print("%d*%d=%d" % (j, i, i * j), end=" ")
print('\t')
#字符串
word ='字符串'
sentence ="sehsdfsdf"
paragraph="""sdfjlsdfjs
dsfsdljkf
"""
print(word)
print(sentence)
print(paragraph)
my_str='dlfj\'d'
print(my_str)
my_str="Jason sair\"dsf\""
print(my_str)
str="chengdu"
print(str)
print(str[0:5])
print(str[1:7:2])#起始位置:结束位置:步进值
print(str[1:])
print(str[:5])
print(str*10+",dkjfd"*3)
print(r"hello\ndfdf")#r能取消\号效果
namelist=[545,"xdjfkd","小李"]
#print(type(namelist[0]))
for i in namelist:
print(type(len(namelist)))
i=0
while i<len(namelist):
print(namelist[i])
i+=1
#增
for name in namelist:
print(name)
nametemp=input()
namelist.append(nametemp)
for name in namelist:
print(name)
a=[1,2]
b=[3,4]
a.append(b)#将列表当作一个元素
print(a)
a.extend(b)
print(a)#将b列表元素,全部加入
a=[0,1,2]
a.insert(1,3) #第一个是下标,第二个是元素
print(a)
#删
m=["1111","2222","3333","4444","5555"]
for i in m:
print(i)
del m[2]
m.pop()
m.remove("1111")#只删除第一个
for i in m:
print(i)
#改
m=["1111","2222","3333","4444","5555"]
for i in m:
print(i)
print("-------------------")
m[1]="xioahong"
for i in m:
print(i)
#查
m=["1111","2222","3333","4444","5555"]
findname=input()
if findname in m:
print("找打")
else :
print("没有")
a=["a","b","c","a","b"]
#print(a.index("a",0,4))#可以查找指定下标范围元素,返回指定下标(范围区间为左闭右开) 找不到报错
print(a.count("a"))
#排序
a=[1,4,2,3]
print(a)
a.reverse()
print(a)
a.sort(reverse=True)
print(a)
import random
s =[["a","b"],["c","d"],["e","f"]]
print(s[0][0])
for name in s:
index=random.randint(0,2)
s[index].append(name)
for a in s:
print()
#元组
t=("sdf","kldsf",2020,2021,333)
t2=(50,)
print(t[0:5])#左闭右开
#增(连接)
t=(12,23,34,45)
t2=("ldfj","ldsjf")
t=t+t2
print(t)
#删
t=(12,23,34)
print(t)
del tup
print(t)
#字典 dict
a={"name":"dkfj","age":18}#定义
print(a["age"])
print(a.get("sdlfj"))#没有找到返回none(默认)
print(a.get("dsfj","hhh"))
print(a.get("age","20"))
#增
a={"name":"dkfj","age":18}#定义
newID=input()
a["id"]=newID
print(a["id"])
#删
a={"name":"dkfj","age":18}#定义
print("%s"%a["name"]+"a"*10)
del a["name"]
a={"name":"dkfj","age":18}#定义
print("%s"%a)
#del a
#claer 清空
a.clear()
print(a)
#改
a={"name":"dkfj","age":18}#定义
a["age"]=1223
print(a["age"])
#查(遍历)
a={"name":"wang","age":18}#定义
print(a.keys())
print(a.values())
print(a.items())
for key in a.keys():
print(key)
for value,key in a.items():
print("%s %s"%(key,value))
mylist=["a","c","b"]
print(enumerate(mylist))
#使用枚举函数同时拿到下标内容
for i,x in enumerate(mylist):
print(i,x)
#集合set
#去重
#函数
def printa():
print("-"*10)
print("test")
print("-"*10)
#printa()
def add2Num(a,b):
c=a+b
print(c)
add2Num(11,22)
def add2Num(a,b):
return a+b
res=add2Num(11,22)
print(res)
def divid(a,b):
shang=a//b
yushu=a%b
return shang,yushu
sh,yu=divid(5,2) #多个返回值
print("%d %d"%(sh,yu));
a=100
def test1():
global a#global
a=300
print(a)
def test2():
a=200
print(a)
test1()
test2()
print(a)
#文件
f=open("test.txt","w")#打开文件
f.close() #关闭文件
f=open("test.txt","w")
f.write("lsdjfkdsljfs")
f.close()
#读取指定的字符,开始读头部,往后依次读
f=open("test.txt","r")#打开文件
c=f.read(5)
print(c)
c1=f.read(5)
print(c1)
f.close()
f=open("test.txt","r")#打开文件
c=f.readlines(); #一次性读取全部列表
print(c)
i=1
for t in c:
print("%d:%s"%(i,t))
i+=1
f.close()
import os
os.rename("test01.txt","test.txt")
#异常
#捕获异常
try:
#print("-test"*10)
f=open("123.txt","r");
print(name)
print("-"*10)#不会执行
except (NameError,IOError,SyntaxError)as result:
print("错误") #捕获异常执行代码
print(result)
try:
#print("-test"*10)
f=open("123.txt","r");
print(name)
print("-"*10)#不会执行
except Exception as result:
print("错误") #捕获异常执行代码
print(result)
# try finally 和嵌套
import time
try:
f = open("test.txt", "r")
try:
while True:
c = f.readline()
if (len(c)) == 0:
break
time.sleep(2)
print(c)
finally:
f.close()
print("文件关闭")
except Exception as result:
print("发生异常")
# 厉害!