1.Python的print语句,与字符串格式运算符(%)结合使用,可实现字符串替换功能。
>>> print("%s is number %d!"%("Python",1))
Python is number 1!
%s表示由一个字符串来替换,而%d表示由一个整数来替换。%f表示由一个浮点数来替换。
>>> print("%f is a float number."%(1.345))
1.345000 is a float number.
>>> print("%s is a float number."%(1.345))
1.345 is a float number.
Python非常灵活,所以即使将数字传递给%s。也不会引发严重错误。
2.除号“/”“//”
>>> 12/7
1.7142857142857142
>>> 12//7
1
3.Python是大小写敏感的
>>> "cAsE"=="CaSe"
False
>>> "cAsE"=="cAsE"
True
4.True=1,false=0
>>> c=True
>>> c+=1
>>> c
2
5.字符串的最后一个元素
>>> pystr='hello'
>>> pystr[-1]
'o'
[:]访问字符串的子集
6.*用于字符串重复:
>>> pystr*2
'hellohello'
>>> '- '*20
'- - - - - - - - - - - - - - - - - - - - '
7. 元组不可以修改
>>> aYu=('me',12,30,'love')
>>> aYu[1:3]
(12, 30)
>>> aYu[1]=11
Traceback (most recent call last):
File "<pyshell#56>", line 1, in <module>
aYu[1]=11
TypeError: 'tuple' object does not support item assignment
8.字典:
>>> aDict={'host':'earth'}
>>> aDict[11]=90
>>> aDict.keys()
dict_keys(['host', 11])
>>> aDict['host']
'earth'
>>> for key in aDict:
print(key,aDict[key])
host earth
11 90