![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Python
zhenge6
这个作者很懒,什么都没留下…
展开
-
input和raw_inpt
#raw_input()x=raw_input("x:")x=int(x)x=x+1print x#input()x=input("x:")print xx=raw_input("x:")print x输出===================== RESTART: C:/Python27/input_raw.py ===========转载 2017-07-23 15:57:40 · 295 阅读 · 0 评论 -
字典 拷贝
print'调用sorted()排序'dict={"a":"apple","b":"banana","c":"orange","d":"grape"}print dictprintprint'按照key 排序'print sorted(dict.items(),key=lambda d:d[0])printprint '按照value排序'print sorted(转载 2017-07-24 10:00:21 · 1204 阅读 · 0 评论 -
序列
print'索引操作'tuple=("apple","banana","grape","orange") #元组,列表,字符串统称为序列list=["apple","banana","grape","orange"]str="apple"print tuple[0]print tuple[-1]print list[0]print list[-1]print str转载 2017-07-24 10:17:51 · 204 阅读 · 0 评论 -
import_
#-*- coding:UTF-8 -*-print'自定义模块'from copy import deepcopycount=1def func(): global count count=count+1 return count#-*- coding:UTF-8 -*-import myModule1print"count=",转载 2017-07-24 11:06:19 · 273 阅读 · 0 评论 -
内联函数
#-*- coding:UTF-8 -*-print'apply()'def sum(x=1,y=2): return x+yprint apply(sum,(1,3))printprint'filter()'def func(x): if x>0: return xprint filter(func,range(-9,10转载 2017-07-24 12:00:33 · 441 阅读 · 0 评论 -
函数参数
#-*- coding:UTF-8 -*-from __future__ import divisiondef append(args=[]): #定义函数,接受一个列表 if len(args) args=[] args.append(0) print argsappend()append([1])append()转载 2017-07-24 14:39:28 · 265 阅读 · 0 评论 -
函数的嵌套
#-*- coding:UTF-8 -*-print'嵌套函数'def sum(a,b): return a+bdef sub(a,b): return a-bdef func(): x=1 y=2 m=3 n=4 return sum(x,y)*sub(m,n)print func()转载 2017-07-24 14:56:06 · 434 阅读 · 0 评论 -
递归
def refunc(n): i=1 if n>1: #递归的结束判断 i=n n=n*refunc(n-1) #递推 print"%d!=" %i,n return n #回归refunc(5)printprint'使用reduce计算阶乘转载 2017-07-24 15:13:59 · 410 阅读 · 0 评论 -
lambda函数
def func(): x=1 y=2 m=3 n=4 sum=lambda x,y:x+y print sum sub=lambda m,n:m-n print sub return sum(x,y)*sub(m,n)print func()printprint'lambda的函转载 2017-07-24 15:20:14 · 410 阅读 · 0 评论 -
字典的更新,遍历
dict={"a":"apple","b":"banana","c":"grape","d":"orange"}print'输出key的列表'print dict.keys()print'输出value的列表'print dict.values()print'每个元素是一个key和value组成的元组,以列表的方式输出'print dict.items()print转载 2017-07-24 09:37:58 · 736 阅读 · 0 评论 -
列表和字典遍历
dict={"a":"apple","b":"banana","g":"grape","o":"orange"}dict["w"]="watermelon"del(dict["a"])dict["g"]="grapefruit"print dict.pop("b")print dictdict.clear()print dictprint#字典的遍历转载 2017-07-23 22:11:34 · 1989 阅读 · 0 评论 -
for in 语句
#for in 语句for x in range(-1,2): if x>0: print"正数:",x elif x==0: print"零:",x else: print"负数:",xelse: print"循环结束"#传统for 循环的实现x=0while x转载 2017-07-23 16:52:28 · 634 阅读 · 0 评论 -
文章标题
contnue 语句x=input(“输入x的值:”) y=0 for y in range(0,100): if x<>y: print”y=”,y continue else: print”x=”,x break ======================= RESTART: C:/Python2转载 2017-07-23 16:59:55 · 165 阅读 · 0 评论 -
range()二元元组遍历
#使用range()循环遍历tuple=(("apple","banana"),("grape","orange"),("watermelon",),("grapefruit",))for i in range(len(tuple)): print "tuple[%d]:" %i,"", for j in range(len(tuple[i])):转载 2017-07-23 18:33:33 · 1273 阅读 · 0 评论 -
list 列表遍历
list=["apple","banana","grape","orange"]print listprint list[2]list.append("watermelon") #末尾附加元素list.insert(1,"grapefruit") #插入第二个位置元素print listlist.remove("grape") #移除元素 print list转载 2017-07-23 18:53:20 · 346 阅读 · 0 评论 -
list2
list=["grape","orange"]list2=["apple",list,"orange"]print listprint list2list3=[i for i in list if i not in list2]print list3======================= RESTART: C:/Python27/list2.py =转载 2017-07-23 19:13:57 · 411 阅读 · 0 评论 -
list
list=["apple","grape","banana","orange"]list.remove("grape")print listlist=["apple","banana","grape","orange"]print list.index("grape")print list.index("orange")print "orange" in list转载 2017-07-23 21:14:29 · 204 阅读 · 0 评论 -
list4
#堆栈的实现list=["apple","banana","grape"]list.append("orange")print listprint"弹出的元素:",list.pop()#默认弹出末位print list#队列的实现list=["apple","banana","grape"]list.append("orange")print l转载 2017-07-23 21:22:15 · 281 阅读 · 0 评论 -
字典
dict={"a":"apple","b":"banana","g":"grape","o":"orange"}print dictprint dict["a"]#使用数字作为索引dict={1:"apple",2:"banana",3:"grape",4:"orange"}print dictprint dict[2]#字典的添加、删除dict={转载 2017-07-23 21:44:35 · 281 阅读 · 0 评论