>> '"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))
輸出結(jié)果:
True
True
True
False
Python中的對象包含三個基本要素,分別是:
id:用來唯一標識一個對象,可以理解為內(nèi)存地址;
type:標識對象的類型;
value:對象的值;
== :比較兩個對象的內(nèi)容是否相等,即兩個對象的 value 是否相等,無論 id 是否相等,默認會調(diào)用對象的 __eq__()方法
is: 比較的是兩個對象是不是完全相同,即他們的 id 要相等。
也就是說如果 a is b 為 True,那么 a == b 也為True
四、for循環(huán)
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')
輸出結(jié)果:
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