设为首页 收藏本站
查看: 728|回复: 0

[经验分享] python 学习笔记一

[复制链接]

尚未签到

发表于 2017-4-28 07:53:22 | 显示全部楼层 |阅读模式
  


#coding=UTF-8
'''
Created on 2011-5-18
@author: lingyibin
'''
import types
import math
print 'hello'
#append
a = [1,2,3,4,5,6]
a.append([2,4])
print a[6][1]
print a;
#extend
a = [1,2,3,4,5,6]
a.extend([2,4])
print a
#pop
a.pop()
print a
a.pop(2)
print a
#长度
print len(a)
#算出元素在list中出现的次数
print a.count(4)
#算出元素在list中的位置
print a.index(4)
'''
4
[1, 2, 3, 4, 5, 6, [2, 4]]
[1, 2, 3, 4, 5, 6, 2, 4]
[1, 2, 3, 4, 5, 6, 2]
[1, 2, 4, 5, 6, 2]
6
1
2
'''
# list的反转
a.reverse()
print a
# 用list来模拟栈
a = []
a.append(0)
a.append(3)
a.append(5)
a.pop()
print a

# 用list来模拟队列
a = []
a.append(0)
a.append(3)
a.append(5)
a.pop(0)
print a
#用list来模拟树
leaf1 = [0,1]
leaf2 = [2,3]
leaf3 = [4,5]
leaf4 = [6,7]
branch1 = [leaf1,leaf2]
branch2 = [leaf3,leaf4]
root = [branch1,branch2]
print root
#把字符串按一定的格式输出
a=["123","456","abc","Abc","AAA"]
k = [k.center(7) for k in a]
print k  #['  123  ', '  456  ', '  abc  ', '  Abc  ', '  AAA  ']
#得到a中仅由字母组成的字条串,并把它变成大写
k = [k.upper() for k in a if k.isalpha()]
print k  #['ABC', 'ABC', 'AAA']
k = [ k.lower() for k in a if k.isupper() ]
print k  #['aaa']
k = [int(k) for k in a if k.isdigit()]
print k  #[123, 456]
a=[123,456,"abc","Abc","AAA"]
k = [k for k in a if type(k) == types.IntType]
print k
b = """Hello , I am lingyibin.
nice to meet you!\a"""
print b
#把句子格式化,即开头大写
c = "THIS IS A SENTENCE!".capitalize() #This is a sentence!
print c
#大小写互换
print c.swapcase()
#字符串查找
print c.index("is") #2
print c.rindex("is") #反向查找,结果5
c = "ok abc abc abc"
print c.find("abc",7) #从7的位置开始查找,结果#7
print c.find("abc",4,9) #4到9的位置查找,不包含9,结果-1
print c.count("abc") #算字符串出现了几次,结果3
#按格式打印
print "%s is good,he he ,%d" % ("Food",2) #这里,在linux下,注意%与%的差别
print "%s’s height is %dcm"%("Charles",180)
#转为8进制
print "%d is %o or %#o"%(16,16,16)
#转为16进制
print "%d is %x or %#x"%(16,16,16)
#科学表示法
print "%e"%(1000) #1.000000e+03
print "%E"%(1000) #1.000000E+03
#字符转数字,数字转字符
print "%c"%(68)
print ord('0')
print ord('A')
print ord('a')
print chr(ord('d')+5)
#固定字符打印。
print "hello".ljust(10)
print "hello".rjust(10)
print "hello".center(10)
print "hello".center(10).lstrip() #去掉左边的空格
print "hello".center(10).rstrip() #去掉右边的空格
print "hello".center(10).strip() #去掉两边的空格
#分解与组合
print "\t".join(["Hello","World","Python","Said"]) #Hello    World    Python    Said
print " ".join("Hello    World    Python    Said".split()) #Hello World Python Said

#元组
a,b=(1,2)
print a,b #1 2
#巧妙地互换
b,a=a,b
print a,b #2 1
#用in来查找
str="abcd"
if "a" in str: print " ok "
x=12
l=[12,13]
if x in l : print "x is in l"
#取部分元素打印。
print str[1:] #bcd
print l[1:]  #[13]
print l[1:]*2 #[13, 13]

pricelist={"clock":12,"table":100,"xiao":100 }
print pricelist["clock"] #12
del pricelist["clock"]
print pricelist
print pricelist.items() #[('table', 100), ('xiao', 100)]
print pricelist.values() #[100, 100]
print pricelist.keys() #['table', 'xiao']
# tuple中有两个元素,一个是key,一个是value
pricelist=dict([("clock",12),("table",100),("xiao",100)])
print pricelist
#加入一个元素
pricelist["apple"]=12
print pricelist #{'table': 100, 'apple': 12, 'xiao': 100, 'clock': 12}
#复制
a = pricelist.copy()
print a  #{'table': 100, 'clock': 12, 'apple': 12, 'xiao': 100}
b = pricelist
a["cool"] = 101
print pricelist #{'table': 100, 'apple': 12, 'xiao': 100, 'clock': 12}
#下面的是直接引用。
b["cool"] = 101
print pricelist #{'table': 100, 'cool': 101, 'apple': 12, 'xiao': 100, 'clock': 12}
# print pricelist["pear"] #报错
print pricelist.get("pear") # 打印None
print pricelist.has_key("pear") #False
pricelist.clear()
pricelist=dict([(x,10*x) for x in [1,2,3]])
print pricelist
print range(1,10,2) #[1, 3, 5, 7, 9]
#乘方和n次方根
print 2**10 #1024
print 27**(1.0/3)
print math.pow(1024, 1.0/10)
 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-370122-1-1.html 上篇帖子: 【转】Python 常用转换函数 下篇帖子: python 简易计算器
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表