python练习小例子(二)
1、tuple使用
1 #!/user/loacl/bin/python
2 a = (1,4,5,-10, 11)
3 b = (7,)
4 print a[2]
5 print b[0]
2、for语句使用
#!/user/local/bin/python
for a in range(1, 10):
print a
a = "qwert"
for c in a:
print c
a = range(0 , 11,5)
print a
b = range(1,1000)
print b
3、haskey使用
1 #!/user/loacl/bin/python
2 a = {
3 "userName":"zhangsan",
4 "home":"zhang home",
5 "uid":1
6 }
7 print a["userName"]
8 print a["uid"]
9
10 a["userName"]="lisi"
11 print a["userName"]
12 a["shell"] = "12331"
13 print a["shell"]
14 del a["userName"]
15 if a.has_key("userName"):
16 userName = a["userName"]
17 else:
18 userName = "unknown user"
19 print userName
20
21 if a.has_key("asd"):
22 asd=a["asd"]
23 else:
24 print "asdasddsd"
4、def函数使用实例
1 #!/bin/python
2 def choose(a,b):
3 if a > b:
4 print a
5 else :
6 print b
7 choose(1,2)
8 choose(4,3)
9 def maxNum(a,b=5):
10 if a > b:
11 return a
12 else:
13 return b
14 c = maxNum(5)
15 print c
16 d = 1
17 e = 1
18 def sda():
19 global d
20 d = 22
21 e = 22
22 sda()
23 print "d = %d, e = %d" % (d,e)