2018-01-22 Python 基础

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)








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值