python快速入门

1.输出 print "Hello World"
2.执行脚本文件 python helloworld.py
3.在命令行里执行脚本 execfile("helloworld.py")
4.退出 import sys
sys.exit()
或者raise SystemExit
5.变量赋值,for循环,输出
principal = 1000
rate = 0.05
numyears = 5
year = 1
while year <= numyears:
principal = principal*(1+rate)
print "%3d %0.2f" % (year,principal)
year += 1
6. if a < b : z = b
else : z = a
7. pass //do nothing
8. if b >= a and b <= c:
print "b is between a and c"
if not (b < a or b > c)
print "b is still between a and c"
9.elif代替其他语言中的switch
if a == '+':
op = PLUS
elif a == '-':
op = MINUS
elif a == '*':
op = MULTIPLY
else:
raise RuntimeError, "Unknown operator"
10.文件输入输出
f = open("foo.txt")
line = f.readline()
while line:
print line
line = f.readline()
f.close()
11.写文件
f=open("foo.txt","w")
while year <= numyears:
principal = principal*(1+rate)
print >>f, "%3d %0.2f" % (year,principal)
year += 1
f.close()
12.字符串
a = 'hello'
b = "hello"
c = """hello"""
print '''Content-type: text/html

<h1>hello world</h1>
'''
13.字符串处理
a = "Hello World"
b = a[4] #b='o'
c = a[0:5]
d = a[6:]
e = a[3:8]
g = a + "test"

str(x) repr(x) `x`:将其他类型转化为字符型
14.列表和数组
len(a):获得a数组长度
append(""):将一个新元素插入到列表末尾
a.insert(index,member):将member插入到指定位置之前

b = names[0:2]
c = names[2:]
names[0:2]=['a','b','c']
a = [1,2,3]+[4,5]
a = [1,"dave",3.14,["mark",7,9,[100,101]],10]

15.
import sys
f = open(sys.argv[1])
svalues = f.readlines()
f.close()

fvalues = [float(s) for s in svalues]
print "The min vlaue is : ",min(fvalues)
print "The max vlaue is : ",max(fvalues)

16.range
for i in range(1,10):
print "2 to the %d power is %d" %(i,2**i)
17.xrange
xrange()函数只有在需要值时才临时通过计算提供值

18.字典
关联数组(哈希表)通过关键字索引,使用{}来创建
a = {
"username":"testuser"
"home":"/home/test"
"uid":400
}
u = a["username"]
d = a["home"]

if a.has_key("username"):
username=a["username"]
else:
username = "default"
username=a.get("username","default")
keys=a.keys()
del a["username"]

19.函数
def test(a,b=120):
q=a/b
r=a-q*b
return (q,r)
quotient,remainter=test(12345,12)

def connect(hostname,port,timeout=100)
connect("www.python.org",80)
connect(port=80,hostname="www.python.org")

20.全局变量
global:
def foo():
global a
a = 8.8

21类
class Stack(object):
def __init__(self):
self.stack=[]
def push(self,object):
self.stack.append(object)
def pop(self):
return self.stack.pop()
def length(self):
reutrn len(self.stack)

使用:
s = Stack()
s.push("Dave")
s.push(42)
s.push([3,4,5])
x=s.pop()
y=s.pop()
del s

22异常
try:
f = open("file.txt","r")
except IOError, e:
print e

raise语句用来有引发异常:
raise RuntimeError, "Unrecoverable error"

23模块
#file : div.py
def divide(a,b):
q=a/b
r=a-q*b
return (q,r)

import div # as foo
a,b = div.divide(234,54)

from div import divide #只导入指定对象
a,b = divide(234,54)#不再需要前缀

import string
import sys
dir(string)#查看模块的可用函数和变量
dir(sys)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值