python学习笔记(一)一些基础用法
原文链接http://python.svenapps.com/post/python-xue-xi-bi-ji-yi#content
注:本资料是个人学习笔记,仅供参考
1.** 幂运算 比取反优先级高 -3**2 等价 -(3**2)
2.// (地板除)整除运算
5/6 0
5//6 0
5.0/6 0.83333333
5.0//6 0.0
声明from __future__ import division后
5/6 0.83333333
5//6 0
5.0/6 0.83333333
5.0//6 0.0
3.中文编码 #_*_ coding:UTF-8 _*_
4. 程序用的环境的路径,/usr/bin/python是linux用的路径 #! /uer/bin/python
5. if name==‘iPhone’:
print ‘This is iPhone’
elif name == ‘Android’:
print 'This is Android'
else:
print 'This is other phone'
6. while循环
i = 1
while i<=5:
print str(i)
else:
print '输出结果完毕‘
7. for循环
yiqie=['iphone','Android','wp7']
for nei in yiqie:
print nei
else:
print 'over'
meinv={'赵飞燕':'水色萧前流玉霜','王昭君':'落燕北飞情未消'}
for key in meinv:
print key,'值是:',meinv[key]
string ='yang'
for s in string:
print s
结果:y
a
n
g
shuzu =[(1,2),(3,4),(5,6)]
for (a,b) in shuzu:
print a,b
for a in shuzu:
print a
结果1 1 2 结果2 (1,2)
3 4 (3,4)
5 6 (5,6)
8 打开文件pen('a.py')
读取内容
for redline in open('a.py')
print redline
9.打印乘法口诀
for i in range(1, 10):
print " ".join(["%d*%d=%d" % (j, i, i*j) for j in range(1, i+1)])
10.zip 并行迭代
names=['Lily','Sven','Wendale']
ages = [23,22,20]
for (name,age) in zip(names,ages):
print name,'age:',age
11.#enumerate
如果for index in theNames index=‘lily’等
如果for index,name in enumerate(theNames): index = 0,1,2
theNames=['Lily','Sven','Wendale']
for index,name in enumerate(theNames):
print theNames[index]
for index,name in enumerate(theNames):
if name=='Lily':
theNames[index] = 'Rader'
for n in theNames:
print n
12.查找字符串 string 是否包含‘a’
string = ‘abacad’
if ‘a’ in string:
print string
13.del删除 只删除名称,不删除值本身
names='duanchunyang'
keys=names
del names
print keys
结果:duanchunyang
14.exec语句用来动态创造pyton代码
exec "print 'iPhone' "
15.判断是否是数字
tel.isdigit()
16.python中文字符串
import sys
type = sys.getfilesystemencoding()
print myname.decode('UTF-8').encode(type)