您的位置:首页 > 编程语言 > Python开发

一些基础Python基础用法--采自《Python核心编程》

2017-05-24 21:36 567 查看
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
13.Python是大小写敏感的
>>> "cAsE"=="CaSe"
False
>>> "cAsE"=="cAsE"
True4.True=1,false=0
>>> c=True
>>> c+=1
>>> c
25.字符串的最后一个元素
>>> 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 assignment8.字典:
>>> 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: