>>> a=23414
>>> a += 123
>>> a
23537
##---------------------------------------------------------------------
>>> prompt = "If you tell us who you are, we can personalize the messages you see."
>>> prompt += "\nWhat is your first name?"
>>> name = input(prompt)
If you tell us who you are, we can personalize the messages you see.
What is your first name?
"""n \t在print、input等展現出內容時起做用"""
>>> prompt
'If you tell us who you are, we can personalize the messages you see.\nWhat is your first name?'
>>> print(prompt)
If you tell us who you are, we can personalize the messages you see.
What is your first name?
##---------------------------------------------------------------------
"""以 while 打頭的循環不斷運行,直到遇到break語句,
也就是說break是做用於 while 結構體的。"""
prompt = '\nTell me something, and I will repeat it back to you!'
prompt += '\nenter "quit" to end the program.'
active = True
while active :
message = input(prompt)shell
if message =='quit':
break
elif message.lower() =='fuck':
break
else:
print(message)函數
Tell me something, and I will repeat it back to you!
enter "quit" to end the program.sdaf
sdafui
Tell me something, and I will repeat it back to you!
enter "quit" to end the program.quit
>>>
"""continue語句讓Python忽略餘下的代碼,並返回到循環的開頭。"""
prompt = '\nTell me something, and I will repeat it back to you!'
prompt += '\nenter "quit" to end the program.'
active = True
while active :
message = input(prompt)對象
if message =='quit':
continue
elif message.lower() =='fuck':
continue
else:
print(message)字符串
Tell me something, and I will repeat it back to you!
enter "quit" to end the program.quitinput
Tell me something, and I will repeat it back to you!
enter "quit" to end the program.
##-------------------------------------------------------------
若是字符串內部既包含'又包含" 能夠用轉義字符\來標識hash
##--------------------------------------------------------------
"""
\\ 表明一個反斜線字符\
\" 表明一個雙引號字符"
\' 表明一個單引號字符'
\? 表明一個問號?
\0 表明空字符(NULL)
"""
>>> print('\\')
\
>>> print('\')')
')
>>> print('\"')
"
>>> print('a\0a')
a
##--------------------------------------------------------------
Python還容許用r''表示''內部的字符串默認不轉義
>>> print('\\\t\\')
\ \
>>> print(r'\\\t\\')
\\\t\\
##--------------------------------------------------------------
布爾值(注意大小寫)運算
>>> True and True
True
>>> True and False
False
>>> False and False
False
>>> True or True
True
>>> True or False
True
>>> False or False
False
>>> not True
False
>>> not False
True
##--------------------------------------------------------------
"""
dict的key是不可變對象
"""
>>> key = [1, 2, 3]
>>> d[key] = 'a list'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
##--------------------------------------------------------------
"""
對於不變對象來講,調用對象自身的任意方法,也不會改變該對象自身的內容。相反,這些方法會建立新的對象並返回,這樣,就保證了不可變對象自己永遠是不可變的。
dict set 的key必須是不可變對象,不能夠放入可變對象,由於沒法判斷兩個可變對象是否相等,也就沒法保證set內部「不會有重複元素」。把list放入set,會報錯。
str是不變對象,而list是可變對象。
"""
>>> a = 'abc'
>>> b = a.replace('a', 'A')
>>> b
'Abc'
>>> a
'abc'
│ a │─────────────────>│ 'abc' │
│ b │─────────────────>│ 'Abc' │
>>> a= set([123,(1,2,3)])
>>> a
{123, (1, 2, 3)}
>>> b= set([123,(1,[2,3])])
Traceback (most recent call last):
File "<pyshell#58>", line 1, in <module>
b= set([123,(1,[2,3])])
TypeError: unhashable type: 'list'
##--------------------------------------------------------------
"""
若是想定義一個什麼事也不作的空函數,能夠用pass語句:
def nop():
pass
pass語句什麼都不作,那有什麼用?實際上pass能夠用來做爲佔位符,好比如今還沒想好怎麼寫函數的代碼,就能夠先放一個pass,讓代碼能運行起來。
"""
if age >= 18:
pass
##--------------------------------------------------------------
"""
數據類型檢查能夠用內置函數isinstance()實現
"""
>>> def my_abs(x):
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
if x >= 0:
return x
else:
return -x
>>> my_abs('A')
Traceback (most recent call last):
File "<pyshell#55>", line 1, in <module>
my_abs('A')
File "<pyshell#53>", line 3, in my_abs
raise TypeError('bad operand type')
TypeError: bad operand type
##--------------------------------------------------------------
"""
is 與 == 區別:
is 用於判斷兩個變量引用對象是否爲同一個, == 用於判斷引用變量的值是否相等。
"""
>>>a = [1, 2, 3]
>>> b = a
>>> b is a
True
>>> b == a
True
>>> b = a[:]
>>> b is a
False
>>> b == a
Trueit