Python 运算符
+ - * / %
关系运算符: a==b a >b a<b a!=b >= <=
赋值运算符 a=b += -= *= /=
a+=b a= a+b
逻辑运算符 and or not if a==b and a!= 10:
数据类型: 整型 int a= 10 print (a)
python 2 print a
bool 类型: True False
>=1 True
<=0 False
浮点型 小数点 round a=3.1415926
m=round(3.1415926,3)
print (m)
round(float,ndigits)
float 代表的数字,ngigits 代表的是精度
大的规则是四舍五入
在无的情况上是有所不同
字符串str
str1='abc ' or str1=‘abc’” or str1='''abc '''
string='abcefg' print (string)find ()
print (string[3]) result=string.find('def') print (result)replace()
print (string.replace('a','AAA'))split()分隔符
print (string.split('a'))['', 'bc', 'de', 'fgh', 'i', 'gh']
strip() 去除字符串前后的空字符
print (string.strip())
format()
print ('my sting is :%s'% string ) print ('my string is :{0}'.format(string)) 推荐使用,一定要用这个
print('hello'+'world')
join() 后面接可迭代的对象,一般是一个list
newList=string.split('a') print(' ### '.join(newList))
List
列表是由一序列特定顺序排列的元素组成的,可以把字符串,数字,字典等加入到列表中,
常用方法:
append 末尾增加一个元素,没有返回值
l= [1,2,3.1415926,4,'a','b',True,{"name":"lin"}] print(l) l.append('hello') print (l)
pop 末尾删除一个元素,返回删除的元素
m=l.pop(2) print(l)
index l.index('a') 返回该元素的下标
print(l.index('a'))remove 删除元素 remove(value)
l.remove("c")sort () 排序
m=[1,24,345,654,76,34,34,654,7,34] print (l) m.sort() print (m)
reverse()反序
m.reverse() print (m)insert 插入新元素 insert(index,value)