1.数值
1.Decimal模块:精确的浮点运算
# 浮点型运算,结果不是4.3,主要是因为计算机处理机制的原因
>>> 1.1+3.2
4.300000000000001
# 导入decimal模块,结果为4.3
>>> import decimal
>>> decimal.Decimal('1.1')+decimal.Decimal('3.2')
Decimal('4.3')
2.浮点型转为整型
a. math模块(math.floor(),math.trunc())
math.floor() 逼近数轴左边一个整数
>>> math.floor(3.3)
3
>>> math.floor(-3.3)
-4
>>> math.floor(3.9)
3
>>> math.floor(-3.9)
-4
math.trunc() 逼近原点一个整数
>>> math.trunc(3.3)
3
>>> math.trunc(-3.3)
-3
>>> math.trunc(3.9)
3
>>> math.trunc(-3.9)
-3
b. round 四舍五入化为整型
>>> round(3.3)
3
>>> round(3.9)
4
2.字符串
2.1字符串索引
str | 索引 |
---|---|
str[i] | 索引第i个元素 |
str[:] | 索引全部 |
str[0] | 索引第一个元素 |
str[-1] | 索引最后一个元素 |
str[::2] | 对全部元素按步长2索引 |
str[0:2] | 索引第0、1个元素 |
2.2常用转义字符
Python | 代表含义 |
---|---|
\\ | \ |
\n | ENTER |
\t | TAB |
\b | 空格 |
’ | ’ |
" | ‘’ |
2.3字符串占位符‘{0}’
个人理解,它的作用应该是使代码更加简洁直观。等我再查一下
>>> '3+2={0}'.format(5)
'3+2=5'
.2f小数点后取两位,float型
>>> '{0:.2f}'.format(3.333)
'3.33'
占位符省了一些拼接的操作
>>> d='name:{0},score:{1}'.format(a.get('name'),a.get('score'))
>>> d
'name:orange,score:100'
如果不用占位符,要达到相同的效果
>>> d='name:'+a.get('name')+','+'score:'+str(a.get('score'))
>>> d
'name:orange,score:100'
占位符方便直观
>>> '{0}=>{1}'.format('orange','apple')
'orange=>apple'
>>> '{a}=>{o}'.format(a='orange',o='apple')
'orange=>apple'
2.4字符串基本操作
函数 | Val功能 |
---|---|
str1.replace(‘a’,‘b’) | 将str1里的a用b代替 |
str1.split(’,’) | 将str1以逗号分开,放到列表中去 |
url.startswith(‘http://’) | 是否以‘http://’开头 |
url.endswith(‘org’) | 是否以org结尾 |
url.find(‘o’) | 返回url第一个o的位置,也可’o’ in url |
url.upper() | 字母变大写 |
注意:.replace()和.split()需重新赋值,即str1=str1.replace(),str1=str1.split()
>>> a='apple,orange,pen'
>>> a.split(',')
['apple', 'orange', 'pen']
>>> a=a.split(',')#按逗号分隔
>>> a[1]
'orange'
>>>
2.5字符串不可以随意改变
字符串初始定义好后,就不可以随意替换里面的元素。
>>> str1='apple'
>>> str1[0]='p'
Traceback (most recent call last):
File "<pyshell#72>", line 1, in <module>
str1[0]='p'
TypeError: 'str' object does not support item assignment
但如果必须替换提供以下三种方法
1.切片
>>> str1='p'+str1[1:]
>>> str1
'ppple'
2.替换
>>>str1= str1.replace('a','p')
>>> str1
'ppple'
3.转为列表再改变某个元素再join拼接
>>> a='apple.com'
>>> l=list(a)
>>> l
['a', 'p', 'p', 'l', 'e', '.', 'c', 'o', 'm']
>>> l[-1]='c'
>>> a=''.join(l) #用空将l的字符连接起来,拼成新的字符串
>>> a
'apple.coc'
>>> a='i'.join(l)#用i将l的字符连接起来,拼成新的字符串
>>> a
'aipipiliei.icioic'
3.列表
列表功能比数组强大很多,列表内的元素可以是多种多样的
如:list1=[1,‘apple’,[1,2]]
3.1列表遍历,以及通过某个列表创建新列表
>>> for c in url:
print(c,end=' ')
o r a n g e . o r g
# 通过某个列表创建新列表*
>>> a=[1,2,3]
>>> b=[]
>>> for i in a:
b.append(i**2)
>>> b
[1, 4, 9]
3.2列表推导
>>> b
[1, 4, 9]
>>> l1=[i**2 for i in b]
>>>
>>> l1
[1, 16, 81]
>>> l1=[i*2 for i in 'orange']
>>> l1
['oo', 'rr', 'aa', 'nn', 'gg', 'ee']
注意:l1=[i*2 for i in 'orange']等价于下列语句,故大大提高了语句的简洁性
>>> l1=[]
>>> for i in 'orange':
l1.append(i*2)
>>> l1
['oo', 'rr', 'aa', 'nn', 'gg', 'ee']
注意:考点
>>>['ab']*3
['ab', 'ab', 'ab']
3.3常用操作
函数 | 功能 |
---|---|
l.append() | 列表后追加元素 |
l.extend() | 扩展一个列表 |
l.sort() | 列表内容从小到大排序 |
l.reverse() | 列表反转 |
l.pop() | 弹出最后一个元素 |
del(l[0]) | 删除第0个元素 |
l.index(2) | 索引第二个元素的值 |
l.count(5) | 数一下l里有几个5 |
‘_’.join(‘FishC’) | ‘F_i_s_h_C’ |
l1=l3.copy()和l1=l3[:] | ip 地址不同,修改l3,l1不变 |
l1=l3 | ip地址相同,修改l3,l1改变 |