- 原始字符串,不做任何特殊的处理
-
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 = name self.__name2 = name#双下划线代表私有 Robot.population += 1 #对象方法 def die(self): Robot.population -= 1 #类方法,用classmethod定义 @classmethod def how_many(self): print("We have {:d} robots.".format(self.population))Robot("蒋凯楠").how_many()
- 代码中有中文导致的错误加入下面的代码,注意别忘了等号
-
# coding=gbk
-
try--except--elsetry--except--finally
-
#coding=gbk'''try--except--elsetry--except--finallyelse只在没有异常时执行,有异常时不执行elsefinally一定会被执行,无论有没有异常except在异常匹配时执行'''import timetry: 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执行了")