>>> 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)
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
sdaf
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.quit
Tell me something, and I will repeat it back to you!
enter "quit" to end the program.
##-------------------------------------------------------------
如果字符串内部既包含‘又包含" 可以用转义字符\来标识
##--------------------------------------------------------------
"""
\\ 代表一个反斜线字符\
\" 代表一个双引号字符"
\‘ 代表一个单引号字符‘
\? 代表一个问号?
\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
True