python 学习笔记一

#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)
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值