1.函数的动态重载
def func(a, b = 5, c = 10): print("a is", a, "and b is", b, "and c is", c) func(3, 7) func(25, c = 24) func(c = 50, a = 100) func(50, c = 100,b=109)
这个深深的冲击力我以前的语言观!
动态语言果然跟静态语言不一样= =
2.函数的返回值
def func(a, b = 5, c = 10): print("a is", a, "and b is", b, "and c is", c) return a,b,c func(3, 7) func(25, c = 24) func(c = 50, a = 100) a,b,c=func(50, c = 100,b=109) print 'a=',a,'b=',b,'c=',c
可以有多个返回值
3.全局变量
global 语句
如果要为一个定义在函数外的变量赋值,那么你就得告诉Python这个变量名不是局部的,而是 全局 的。使用global语句完成这一功能。
没有global语句,是不可能为定义在函数外的变量赋值的。
你可以使用定义在函数外的变量的值(假设在函数内没有同名的变量)。然而,应避免这样做,因为这降低程序的可读性,不清楚变量在哪里定义的。
使用global语句可以清楚地表明变量是在外面的块定义的。
注:可以使用同一个global语句指定多个全局变量。例如 global x, y, z。
def func(): global x print('x is', x) x = 2 print('Changed local x to', x) # 打印: 2 x = 50 func() print('Value of x is', x)
4.数据结构列表(List)
shoplist = ["apple", "mango", "carrot", "banana"] #列表用一对方括号[]表示,每项数据之间用逗号隔开。一旦你创建了一个列表,你可以对它进行添加、删除或搜索。所以列表是可以改变的。 print("I have", len(shoplist), "items to purchase.") for i in shoplist: print i print("I also have to buy rice.") shoplist.append("rice")#添加 print("My shopping list is now", shoplist) for i in shoplist: print i shoplist.sort()#排序 for i in shoplist: print i print("The first item I will buy is", shoplist[0]) olditem = shoplist[0] del shoplist[0]#删除 print("I bought the", olditem) print("My shopping list is now", shoplist)
遍历:
shoplist = ["apple", "mango", "carrot", "banana"] #Indexing or "Subscription" operation print("Item 0 is", shoplist[0]) print("Item -1 is", shoplist[-1]) #Slicing on a list print("Item 1 to 3 is", shoplist[1:3]) print("Item 2 to end is", shoplist[2:]) print("Item 1 to -1 is", shoplist[1:-1]) print("Item 0 to 1 is", shoplist[0:1]) print("Item start to end is", shoplist[:]) #Slicing on a string name = "known" print("charactor 1 to 3 is", name[1:3])
索引同样可以是负数,在那样的情况下,位置是从序列尾开始计算的。因此,shoplist[-1]表示序列的最后一个元素而shoplist[-2]抓取序列的倒数第二个项目。
切片操作符是序列名后跟一个方括号,方括号中有一对可选的数字,并用冒号分割。注意这与你使用的索引操作符十分相似。记住数是可选的,而冒号是必须的。
切片操作符中的第一个数(冒号之前)表示切片开始的位置,第二个数(冒号之后)表示切片到哪里结束。如果不指定第一个数,Python就从序列首开始。如果没有指定第二个数,则Python会停止在序列尾。注意,返回的序列从开始位置 开始 ,刚好在 结束 位置之前结束。即开始位置是包含在序列切片中的,而结束位置被排斥在切片外。
5. 元组
元组和列表十分类似,只不过元组是不可以改变的,即不能被修改。元组是用一对圆括号()表示,每项数据之间也是用逗号隔开。元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。
zoo = ("wolf", "elephant", "penguin") print("Number of animals in the zoo is", len(zoo)) new_zoo = ("monkey", "dolphin", zoo) print("Number of animals in the new zoo is", len(new_zoo)) print("All animals in new zoo are", new_zoo) print("Animals brought from old zoo are", new_zoo[2]) print("Last animal brought from old zoo is", new_zoo[2][2])
6. 字典(dict)
有点像STL里的map表示映射关系
ab = {"user1" : "user1@test.com", "user2" : "user2@test.com"} print ab for name, address in ab.items(): print("Contact %s at %s" % (name, address)) if not "tom" in ab:#OR not ab.has_key("tom") ab["tom"] = "tom@test.com" #添加 print ab print("user1's address is %s" % ab["tom"]) del ab["user1"] #删除 print ab
7.浅拷贝与深拷贝
print("Simple Assignment") shoplist = ['apple', 'mango', 'carrot', 'banana'] mylist = shoplist # mylist is just another name pointing to the same object! del shoplist[0] print("shoplist is", shoplist) print("mylist is", mylist) # notice that both shoplist and mylist both print the same list without # the 'apple' confirming that they point to the same object print("Copy by making a full slice") mylist = shoplist[:] # make a copy by doing a full slice del mylist[0] # remove first item print("shoplist is", shoplist) print("mylist is", mylist) # notice that now the two lists are different
8. 字符串函数
name = "Swaroop" # This is a string object if name.startswith("Swa"): print("Yes, the string starts with 'Swa'") if "a" in name: print("Yes, it contains the string 'a'") if name.find("war") != -1: print("Yes, it contains the string 'war'") delimiter = "_*_" mylist = ["Brazil", "Russia", "India", "China"] print(delimiter.join(mylist))
test:
选择排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
s=[0,9,5,1,2,7,3] n=7 for i in range(0,n): max=s[i] rj=i for j in range(i+1,n): if max<s[j]: max=s[j] rj=j temp=s[i] s[i]=s[rj] s[rj]=temp print s
转引自:http://www.cnblogs.com/known/archive/2010/09/03/1817499.html