首页 > 代码库 > python几种常见语法

python几种常见语法

NO1.type自动匹配类型

name="zhangsan"
age=18

print (type(name))
#type自动匹配类型

print ("name;%s,age: %d"%(name,age))
#%s 代表字符类型 %d 代表数字

print ("hi your nme:"+name+" your age:"+str(age) )

#str 将数字改成字符串进行打印

 

NO2.for循环

a="hello"

for i in a:
print (i)

#for (int i=0;i<10;i++)

for x in range(1,10,1):
print (x)

#for x in range(a,b,c): a代表第一个参数,b代表循环次数,c代表循环递增大小

c=0
while c<10:
c+=1
print (c)

a=[‘a‘,1,‘b‘,‘hello‘]

a.append("word")
#a后面追加信息word

for i in a:
print (i)
#print type(i)

z={"key":"value","username":"zhangsan","password":123}
for i in z.keys(): # 输出key
print (i)

for x in z.values(): #输出values
print (x)

for k,v in z.items():
print (k,v)

NO3.if语句


a="hello"
b="hello word!"
if a not in b:
print ("zai")
else:
print ("no")

#in 包含 not in 不包含

a=True
if a:
print ("true")

#==相等
#<> >< != 不相等

 

NO4.调用time中的sleep

#from ...import .
from selenium import webdriver
from time import sleep as sl
#调用time中的sleep

#from time import *
#*代表调用全部)

def sleep(): #自定义sleep
print ("seleep")

sl(5)
print (‘end‘)

NO5.try异常处理

a=10
b=0
try:
print(a/b)
except:
print("error")
finally:
print("always excute")

python几种常见语法