>> '"Isn\'t," they said.'
'"Isn't," they said.'
>>> print('"Isn\'t," they said.')
"Isn't," they said.
>>> s = 'First line.\nSecond line.' # \n means newline
>>> s # without print(), \n is included in the output
'First line.\nSecond line.'
>>> print(s) # with print(), \n produces a new line
First line.
Second line.
>>>('Put several strings within parentheses '
... 'to have them joined together.')
>>>text
>'Put several strings within parentheses to have them joined together.'
这个功能只能用于两个字面值,不能用于变量或者表达式:
>>> prefix = 'Py'
>>> prefix 'thon' # can't concatenate a variable and a string literal
File "stdin>", line 1
prefix 'thon'
^
SyntaxError: invalid syntax
>>> ('un' * 3) 'ium'
File "stdin>", line 1
('un' * 3) 'ium'
^
SyntaxError: invalid syntax
import copy
a = (1,2)
b = copy.copy(a)
c =copy.deepcopy(a)
print(b == c)
print(id(b)==id(c))
lista = [5,6]
listb = copy.copy(lista)
listc = copy.copy(lista)
print(listb == listc)
print(id(lista) == id(listb))
输出结果:
True
True
True
False
Python中的对象包含三个基本要素,分别是:
id:用来唯一标识一个对象,可以理解为内存地址;
type:标识对象的类型;
value:对象的值;
== :比较两个对象的内容是否相等,即两个对象的 value 是否相等,无论 id 是否相等,默认会调用对象的 __eq__()方法
is: 比较的是两个对象是不是完全相同,即他们的 id 要相等。
也就是说如果 a is b 为 True,那么 a == b 也为True
四、for循环
for n in range(2,10):
for x in range(2,n):
if n % x == 0:
print(n ,'equals',x ,'*', n//x)
break
else:
print(n,'is a prime number')
输出结果:
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3