python 基本对象类型
2022-04-05 16:54 jym蒟蒻 阅读(382) 评论(0) 编辑 收藏 举报文章目录
- python对象类型
- 1.数字
- 2.字符串
- 3.列表
- 4.字典
- 5.元组
- 6.文件
- 7.其他核心类型
例子
>>> 123+222
345
>>> 1.5*399
598.5
>>> 2**10
1024
>>> 3.1415*2
6.283
>>> 9.9999+1
10.9999
>>> import math
>>> math.pi
3.141592653589793
>>> math.sqrt(80)
8.94427190999916
>>> import random
>>> random.random()
0.3359936409813997
>>> random.choice([1,2,3,4])
1
>>> random.choice([5,2,3,4])
4
可以看出:
1.python支持一般的数学运算
2.还可以导入一些数学模块,比如math,random模块。
>>> S='span'
>>> len(S)
4
>>> S[0]
's'
>>> S[1]
'p'
>>> S[-1]
'n'
>>> S[-2]
'a'
>>> S[len(S)-1]
'n'
>>> S[1:3]
'pa'
>>> S[1:]
'pan'
>>> S[:-1]
'spa'
>>> S[:0]
''
>>> S[0:]
'span'
>>> S[:]
'span'
>>> S
'span'
>>> S+'xyz'
'spanxyz'
>>> S*8
'spanspanspanspanspanspanspanspan'
>>> S[0]='z'
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
S[0]='z'
TypeError: 'str' object does not support item assignment
>>> S='z'+S[1:]
>>> S
'zpan'
>>> S.find('pa')
1
>>> S.replace('pa','xyz')
'zxyzn'
>>> S
'zpan'
>>> line='aaa,bbb,ccc,ddd'
>>> line.split(',')
['aaa', 'bbb', 'ccc', 'ddd']
>>> S='jymmm'
>>> S.upper()
'JYMMM'
>>> S.isalpha()
True
>>> line
'aaa,bbb,ccc,ddd'
>>> line=line+'\n'
>>> line
'aaa,bbb,ccc,ddd\n'
>>> line='aaa,bbb,ccc,ddd\n'
>>> line
'aaa,bbb,ccc,ddd\n'
>>> line=line.rstrip()
>>> line
'aaa,bbb,ccc,ddd'
>>> '%s,eggs,and%s'%('span','sjdi!')
'span,eggs,andsjdi!'
>>> '{0},eggs,and {1}'.format('spann','isjdi')
'spann,eggs,and isjdi'
>>> S=