1.else语句
if else语句
age=20
if age>=18:
print('your age is',age)
print('adult')
else:
print('your age is',age)
print('teenager')
while else语句
count = 0
while count < 5:
print("%d is less than 5" % count)
count = count + 1
else:
print("%d is not less than 5" % count)
try except else语句
try:
print('try...')
r=10/int('2')
print('result:',r)
except ValueError as e:
print('ValueError:',e)
except ZeroDivisionError as e:
print('ZeroDivisionError:',e)
else:
print('no error!')
finally:
print('finally...')
print('end')
try...
result: 5.0
no error!
finally...
end
with语句
一些对象定义了标准的清理行为,无论系统是否成功的使用了它,一旦不需要它了,那么这个标准的清理行为就会执行。关键词 with 语句就可以保证诸如文件之类的对象在使用完之后一定会正确的执行它的清理方法。
with open('/Users/lenovo/Desktop/text.txt','r') as f:
print(f.read())
123