Python基础总结(4)

来源:互联网 发布:女童白色运动鞋淘宝 编辑:程序博客网 时间:2024/06/14 10:09

元组:戴上枷锁的列表

tuple1=(1,2,3,4,5,6,7,8)

更新元素方法

temp=('aaa','vvv','ccc','ddd')temp=temp[:2]+('hehe',)+temp[2:]
print "{0}.{1}.{2}".format("www","superkingdom","cn")print "{a}.{b}.{c}".format(a="www",b="superkingdom",c="cn")
print '%c' %(97)print '%s' %('cjfsdl')print '%d' %(4)

max(tuple1)取最大值
sum(tuple1)求和
sorted(numbers)
reversed(numbers)
enumerate(numbers)枚举
zip(a,b)
a=[1,2,3,4,5,6,7,8]
b=[4,5,6,7,8]
zip(a,b)
[(1,4),(2,5),(3,6),(4,7),(5,8)]

函数与过程
Python只有函数

def discounts(price,rate):    final_price=price*rate    return final_priceold_price=float(input('请输入原价:'))rate=float(input('请输入折扣率:'))new_price=discounts(old_price,rate)print('折后价是:',new_price)

filter()过滤器
filter(function or None,iterable)

print list(filter(lambda x:x%2,range(10)))

map()加工 将序列加工之后返回

print list(map(lambda x:x*2,range(10)))
原创粉丝点击