>>> 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