python简单学习



a=10; print (a); a1=234.5 print (a1,type(a1)) if 1>0: a= 'asdasd' print ((a),type(a)) b='234343434' print (b) #=========================== a=('hello',1,66,45,False) print(a) print (a,type(a)) print (a[0]) print (a[2:5]) #=========================== b=[123,345,678] print (b) print (b,type(b)) print ((">>>>") , (b[1])) print ("\n=============") #=========================== b2='abcdefg' print (b2[2:5]) print ("b2最后一个字符:",b2[-1]) print (3==4) #======================== b3=[234,234,63,2,6] #print (3 not in b3) if 63 in b3: print ('3不在啊啊啊啊') elif 4 in b3: print ('啊啊啊啊') else: print ('333') print ("========================") x=1 y=2 if x>y: m=3 elif x<y: m=4 else: m=2 print ('结果:',m*4) print ("for循环========================") for a in [1,2,4,'qwe','456']: print (a) c=list( range(1,5)) print (c) for q in c: print ('2等平方》',q**2) print ("while=======================") i=0 while i<5: print ("===",i) i = i+1 print ("continue在循环的某一次执行中,如果遇到continue, 那么跳过这一次执行,进行下一次的操作") for m in list(range(10)): if m==3: continue print (">>",m) print ("break停止执行整个循环") for n in list(range(5)): print (">>>",n) if n==3: break print (">>>>>>>>>>>>>>>>>>>函数定义") print ("当程序执行到return的时候,程序将停止执行函数内余下的语句。return并不是必须的,当没有return, 或者return后面没有返回值时,函数将自动返回None。") def plus(a,b): print ('这个是函数') c=a*2+b return c print (plus(2,5)) q10=[1,2,6,7,8,5,4,3,2] def test10(q11): q10[0]=q10[3]+6 return q10 print (test10(q10)) print ("这个表已经发生了变化》》》",q10) #不知道是不是这样 def year(y): if ((y%4==0 and y%100!=0 ) or y%400==0): return "闰年" return "不是闰年" print (year(2015)) print (">>>>>>>>>>>>>>>>>面向对象") class people(): have_eyes=True def walk( x,y): p=[0,0] p[0]=x p[1]=y return (p) me=people() print ("走路啊啊:",people.walk(12,33)) print (">>>>>>>>>>>>>>>>>list") a25=[1,2,3,6,4,2,9] a25.sort() #排序 a25.append(1) a25.remove(3) a25.insert(3,90) a25.pop() #去掉最后一个 print (a25) print ("长度:",len(a25)) print (dir(list)) #print (help(list)) print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>词典") ci={"zhang":90,"li":88,"wang":93} print (ci['li']) ci['liu']=95 #添加 for ci2 in ci: print (ci[ci2]) print ("所有键:",ci.keys()) print ("所有值:",ci.values()) print ("所有:",ci.items()) print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>文件") ''' f2=open("123.txt","w+") f2.write("123456789abc") print ("指针位置:",f2.tell()) f2.close() ''' f=open("123.txt","r") content=f.readlines() #读取所有行,储存在列表中,每个元素是一行 for content_all in content: print (content_all) f.close() print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>模块") import test2 for i5 in list(range(5)): test2.mm() print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>参数部分") def canshu(a,b,c): return a+b+c print ("结果:",canshu(1,5,6)) def canshu2(a,b,c=4): return a+b+c*2 print ("结果:",canshu2(1,5,6)) print ("结果:",canshu2(1,5)) print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>包裹传递") def baoguo(*n): print (type(n)) print (n) baoguo(1,2,3,4,2,2,8) #传递字典 def baoguo2(**n): print (type(n)) print (n) baoguo2(a=2,b=7) baoguo2(a=2,b=6,c=8) print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>解包裹") def baoguo3(a,b): print (a,b) b333=(2,4) baoguo3(*b333) b444={'a':1111,'b':222} baoguo3(**b444) print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>循环设计range") s1='abcdefgh' for i in list(range(0,len(s1),2)): print (s1[i]) print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>循环设计enumerate") S = 'abcdefghijk' for (index,char) in enumerate(S): print (index)#下标 print (char)#元素 print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>循环设计zip") print ("如果你多个等长的序列,然后想要每次循环时从各个序列分别取出一个元素,可以利用zip()方便地实现:") ta = [1,2,3] tb = [9,8,7] tc = ['a','b','c'] for (a,b,c) in zip(ta,tb,tc): print(a,b,c) print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>循环对象") for i in open("123.txt","r"): print (i) print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>生成器:") def gen(): a = 100 yield a a = a*8 yield a yield 1000 for i in gen(): print (i) print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>表推导:") L = [] for x in range(10): L.append(x**2) print (L) #表推导 方法 生成表L L = [x**2 for x in range(10)] print (L) xl = [1,3,5] yl = [9,12,13] L = [ x**2 for (x,y) in zip(xl,yl) if y > 10] print (L) print (">>>>>>>>>>>>>>>>>>>>>>>>>>>lambda函数") func = lambda x,y: x + y print (func(3,4)) #其实就是这样 def func(x, y): return x + y print (">>>>>>>>>>>>>>>>>>>>>>>>>>函数作为参数传递") def test(f, a, b): print ('test') print (f(a, b)) test(func, 3, 5) #可以直接写成这样 test((lambda x,y: x**2 + y), 6, 9) #太简单了!!! print (">>>>>>>>>>>>>>>>>>>>>>>>>>map函数") #map()有两个参数,一个是lambda所定义的函数对象,一个是包含有多个元素的表。map()的功能是将函数对象依次作用于表的每一个元素 a=map( ( lambda x:x+1 ),[89,90]); print (a) print (list(a)) print (">>>>>>>>>>>>>>>>>>>>>>>>>>filter()函数") def func2(a): if a > 100: print ('true') return True else: print ('false') return False print( filter(func2,[10,56,101,500])) print (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") def f(x): x = 100 print (x) a = 1 f(a) print (a) print (">>>>>>>>>>") def f(x): x[0] = 100 print (x) a = [1,2,3] f(a) print (a) 

你可能感兴趣的:(python)