def checkpoint():
while True:
res = input("Confirm to Continue? (Y/N) \n")
if res == 'Y' or res == 'y':
return True
else:
print('Cancelled')
break
"\r" 是将光标移到一行的开始,所以\r之后的内容会覆盖掉上次打印的内容
"\n" 结尾换行
"\t" 制表符 表示中间加一个tab
r"xy\nz" 字符串前加字母r表示后面字符串中不进行转义 => 'xyz'
"\b" 退格符,将光标前移,覆盖
import random
>>>dic={k:random.randint(4,9) for k in ["a","b","c","d"]}
>>>dic
{'a': 7, 'b': 7, 'c': 5, 'd': 8}
s = """ this is a very
long string if I had the
energy to type more and more ..."""
s.splitlines()
["this is a very", "long string if I had the", "energy to type more and more ..."]
try:
print(v)
except Exception as e:
print("Error code %d - %s" % (10,e))
>>>Error code 10 - name 'v' is not defined
def Fnc():
try for i in range(5):
print (i)
if i > 3:
rasie Exception ("Not much left")
except Expection as Err
print (Err)
continue
try..except..else没有捕获到异常,执行else语句
try..except..finally不管是否捕获到异常,都执行finally语句
method-1:
>>> vars_in_string = "Angela {} is {} years old.".format(variable1, variable2)
>>> print("Number {:03d} is here.".format(11))
Number 011 is here.
>>> 'A formatted number - {:.4f}'.format(.2)
'A formatted number - 0.2000'
method-2:
>>> vars_in_string = f"Eric {variable1} is {variable2} years old."
method-3:
>>> vars_in_string = "Eric %s is %d years old." % (variable1, variable2)
"1.2.3.4".split(".") == ['1', '2', '3', '4']
".".join(['1', '2', '3', '4']) == "1.2.3.4"
res=requests.get("http://www.google.ie")
os.rename(old, new)
os.getcwd()
os.listdir()
os.environ()
sys.argv[0]
sys.stderr()
sys.exit()
# NOT 优于 AND 优于 OR