python代码
- 原始字符串,不做任何特殊的處理
- print("Newlines are indicated by \n")#Newlines are indicated by print(r"Newlines are indicated by \n")#Newlines are indicated by \n
- 格式輸出,轉化為字符串由format自動完成
- age = 20 name = 'Swaroop' print('{0} was {1} years old when he wrote this book'.format(name, age))
- 元組字典做函數參數
- def total(a=5, *numbers, **phonebook): print('a', a) for single_item in numbers: print('single_item', single_item)for first_part, second_part in phonebook.items(): print(first_part,second_part) print(total(10,1,2,3,Jack=1123,John=2231,Inge=1560))
- 字符串連接鏈表
- delimiter = '____' mylist = ['Brazil', 'Russia', 'India', 'China'] print(delimiter.join(mylist))#Brazil____Russia____India____China
- 類變量、方法,對象變量、方法
- class Robot:'''表示有一個帶有名字的機器人。'''#類變量population = 0__population2 = 0#雙下劃線代表私有def __init__(self, name):#對象變量self.name = nameself.__name2 = name#雙下劃線代表私有Robot.population += 1#對象方法def die(self):Robot.population -= 1#類方法,用classmethod定義 @classmethoddef how_many(self):print("We have {:d} robots.".format(self.population)) Robot("蔣凱楠").how_many()
- 代碼中有中文導致的錯誤加入下面的代碼,注意別忘了等號
- # coding=gbk
- try--except--else try--except--finally
- #coding=gbk
'''
try--except--else
try--except--finally
else只在沒有異常時執行,有異常時不執行else
finally一定會被執行,無論有沒有異常
except在異常匹配時執行
'''
import time
try:for i in range(1, 2, 1):print(i)time.sleep(1)raise KeyboardInterrupt()
except KeyboardInterrupt:print("CTRL+C后,except執行了")
finally:print("CTRL+C后,finally執行了")
?
?
轉載于:https://www.cnblogs.com/jkn1234/p/8797947.html
總結
- 上一篇: 做梦梦到别人孩子死了是什么意思
- 下一篇: Python高级网络编程系列之基础篇