环境 python3
类型转换
int(str(8))
float
int
str
变量类型
type(var)
运算:
num=10
print (num**2) # 100
list使用:
months=[]
months.append('ONE')
months.append('TWO')
months.append('THREE')
print (months)
print (months[0]) #索引取值
print (len(months)) #长度
print (months[len(months)-1]) #取最后一个元素
print (months[-2]) #索引倒着取
print (months[0:2]) #从0取到1索引
print (months[0:]) #从0取到最后
['ONE', 'TWO', 'THREE']
ONE
3
THREE
TWO
['ONE', 'TWO']
['ONE', 'TWO', 'THREE']
循环
#while循环
citys=['beijing','hfei','jinan','shanghai']
i=0
while i < 3:
i +=1;
print(i)
#for循环
citys=['beijing','hfei','jinan','shanghai']
for city in citys :
print(city)
#for循环
citys=['beijing','hfei','jinan','shanghai']
for i in range(len(citys)) :
print(citys[i])
#for循环
citys=[['beijing','hfei','jinan','shanghai'],['beijing','hfei','jinan','shanghai']]
for city in citys :
for c in city :
print(c)
boolean类型
#boolean类型
blo = True
bfs = False
print(blo)
print(bfs)
print(9==5)
print("9"=="9")
if判断
#if判断 在python非0的值都为真
if 1:
print(1)
else:
print(2)
字典
#字典结构
scores = {}
scores['jim1']=80
scores['jim2']=90
scores['jim3']=92
print(scores)
print(scores['jim1'])
print(scores.keys())
print(scores.values())
print('jim3' in scores)
文件操作
#文件操作
f = open("test.txt","r")
g = f.read()
print(g)
f.close()
f2 = open("test.txt","w")
f2.write('你好 file')
f2.write('\n')
f2.close()
函数
#函数
def printhello(a):
print('hello'+a)
printhello(' world')
#函数 参数为函数对象
def funback(printhello):
printhello
funback(printhello(' world'))