Python Notebook print() print(1 ) print("we're the best" ) print('we are the best' ) print('we\'re the best' )  print("apple" +"car" ) print('apple' +'tree' ) print('apple' +'4' ) print('apple' +str(4 )) print(1 +2 ) print("1+2" ) print(int('1' )+2 ) print(float(1.2 )+2 ) 
 
数学: print(1 +1 ) print(1 -1 ) print(2 *2 ) print(2 ^1 )   print(2 **3 )  print(8 %3 )   print(8 //3 ) 
 
自变量 variable temp=999 +1  temp_2=1 +temp print(temp,temp_2)
 
while循环 a=1 while  a<10 :     print(a)     a+=1 
 
 
for循环 example_list = [1 ,2 ,3 ,4 ,5 ]for  i in  example_list:     print(i)//-----     print(i+1 )//---这两行都在for 循环内(python十分看重结构) print("end" )
 
for  i in  range(1 ,10 ,2 ):      print(i)
 
a = [7 ,6 ,5 ,4 ,3 ,2 ,1 ]for  i in  range(0 ,6 ):      print(a[i])
 
if条件 x=1  y=2  z=0 if  x<y>z:     print('x is less than y,and y is greater than z' ) a=1  b=1 if  a==b:     print('a is equal to b' )
 
x=1  y=2  z=0 if  x>y:     print(1 )else :     print(2 )
 
x=1  y=2  z=0 if  x==1 :     print(x)elif  y==2 :     print(y)else :     print(z) print('finished' )
 
def函数 def  f () :     print('this is a f' ) f()
 
def  f (a,b) :     c=a*b     print(c) f(1 ,2 )
 
def  sale (price,color,brand,is_second=True) :       print("price:" ,price,         "color:" ,color,         "brand:" ,brand,         "second:" ,is_second) sale(1000 ,"red" ,True ,"bmw" )
 
def  fun (a) :     return  a*a b=1  b+=fun(5 ) print(b)
 
全局&局部变量 def  fun () :     global  a     a=20      return  a*a print(fun())  print(a)   
 
安装numpy模块 cmd 里直接pip install numpy 
(如果需要更新输入python -m pip install —upgrade pip )
文件读写 写入内容
text="this is my first text.\nThis is next line."  my_file=open('my file.txt' ,'w' )   my_file.write(text) my_file.close()
 
增加内容
text="\nThis is appended line."  my_file=open('my file.txt' ,'a' )   my_file.write(text) my_file.close()
 
打印内容
my_file=open ('my file.txt' ,'r' )  #如果没有这个文件就会创建一个,且保存在.py 文件同一个文件夹中 content=my_file.read ()print (content)
 
readline/readlines
my_file=open('my file.txt' ,'r' )   content=my_file.readline() print(content) content=my_file.readlines() print(content)
 
class 类 class  Calculate :     name = 'Good calculator'      price=18      def  plus (self,x,y) :         ans=x+y         return  ans     def  minus (self,x,y) :         ans=x-y         return  ans a=Calculate()  print(a.name) print(a.price) print(a.plus(2 ,3 ))
 
类init功能 含参构造函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class  Calculate :     name = 'Good calculator'      price=18      def  __init__ (self,name,price,height) :         self.name=name         self.price=price         self.h=height     def  plus (self,x,y) :         ans=x+y         return  ans     def  minus (self,x,y) :         ans=x-y         return  ans a=Calculate('bad calculator' ,15 ,12 ) print(a.name) print(a.price) print(a.plus(2 ,3 ))
 
a=input()   print(a) a=int(input())   b=input('please give a number' )  print(b)
 
元组 列表 这两者十分相似
a_tuple=(12 ,3 ,5 ,15 ,6 )   another_tuple=12 ,3 ,5 ,15 ,6     a_list=[12 ,4 ,12 ,18 ,12 ]  for  content in  a_list:        print(content)for  content in  a_tuple:       print(content)for  index in  range(len(a_list)):     print('index=' ,index,'number=' ,a_list[index])for  index in  range(len(a_tuple)):     print('index=' ,index,'number=' ,a_list[index])
 
a_list=[12 ,4 ,12 ,18 ,12 ]   a_list.insert(1 ,0 )   a_list.append(123 )   a_list.remove(12 )   print(a_list) print(a_list[2 ]) print(a_list[-1 ])   print(a_list[0 :3 ])     print(a_list[0 :])      print(a_list.index(4 ))   print(a_list.count(12 ))  a_list.sort()   print(a_list) a_list.sort(reverse=True )   print(a_list)
 
多维列表 multi_list=[[1 ,2 ,3 ],[2 ,3 ,4 ],[3 ,4 ,5 ]] print(multi_list[1 ]) print(multi_list[0 ][2 ])
 
字典 a_list=[1 ,2 ,3 ,4 ,5 ] d={'a' :1 ,'b' :2 ,'c' :3 }  d2={1 :'a' ,2 :'b' ,3 :'c' } print(d['a' ]) print(d2[2 ])del  d['b' ]   print(d) d['d' ]=4      print(d)
 
字典套字典/字典套列表
d={'a' :[1 ,2 ,3 ],'b' :{'c' :3 ,5 :'e' }} print(d['a' ][0 ]) print(d['b' ]['c' ]) print(d['b' ][5 ])
 
载入模块 载入模块的四种方式
import  time print(time.localtime())  
 
import  time as  t    print(t.localtime())
 
from  time import  time,localtime   print(localtime())   print(time())
 
from  time import * print(localtime())   
 
做一个自己的模块/脚本 先自己写一个模块,并放在.py文件同根目录下
这里我给他命名为 my_mod.py
def  printdata (data) :     print(data)
 
调用
import  my_mod my_mod.printdata('test my mod' )
 
continue/break while  True :     b=int(input())     if  b==1 :         break      else :         pass 
 
错误处理Try 下面这中方法可以输出错误信息,但是在pychram也可以做到,所以几乎用不到这种方法。
try :     file=open('eeee' ,'r' )except  Exception as  e:     print(e)
 
正确的用法出现在这种情况下:
try :     file=open('eeee' ,'r+' )except  Exception as  e:     print("no such file" )     re=input('do you wanna creat a new file' )     if  re=='yes' :         file = open('eeee' ,'w' )         file.write("1234567" )         file.close()     else :         pass else :     file.write("12345" )     file.close()
 
zip/lambda/map zip
a=[1 ,2 ,3 ] b=[4 ,5 ,6 ] c=list(zip(a,b))   # zip是一个操作,需要转换成list就是列表的形式了 print(c)          # [(1 , 4 ), (2 , 5 ), (3 , 6 )]for  i,j in  zip(a,b):     print(i/2 ,i*2 ) #   0.5  2  	1.0  4  	1.5  6  print(list(zip(a,a,b))) #三元组 [(1 , 1 , 4 ), (2 , 2 , 5 ), (3 , 3 , 6 )]
 
lambda
和函数一样,常用来定义简单的函数
fun=lambda  x,y:x+y print(fun(1 ,2 )) 
 
map
fun=lambda x,y:x+y ans=list (map (fun,[3 ],[5 ]))print (ans)  #[8 ] ans=list (map (fun,[3 ,4 ,5 ],[6 ,7 ,8 ])) #[9 , 11 , 13 ]print (ans)
 
浅复制/深复制 copy&deepcopy 赋值操作中的copy
a=[1 ,2 ,3 ] b=a print(id(a),id(b))  b[0 ]=15    print(a)  print(b) 
 
copy模块中的浅复制 copy
import  copy a=[1 ,2 ,3 ] b=copy.copy(a) print(id(a),id(b))   b[0 ]=15  print(a)   print(b)  
 
copy模块中的深复制 deepcopy
什么是深复制?为什么要深复制?
看一下下面这个例子
import  copy a=[1 ,2 ,[3 ,4 ]] b=copy.copy(a) print(id(a[0 ])==id(b[0 ]))   print(id(a[2 ][0 ])==id(b[2 ][0 ]))   b[2 ][0 ]=15  print(a)   print(b)  
 
可以发现列表中的列表并没有被改变地址
因此需要deepcopy 相当于完完全全的重新复制出的一个东西,地址都不同
import copy a=[1,2,[3,4]] b=copy.deepcopy(a) print(id(a[0])==id(b[0]))    #true print(id(a[2 ][0 ])==id(b[2 ][0 ]))   #true b[2 ][0 ]=15 print(id(a[2 ][0 ])==id(b[2 ][0 ]))   #flase print(a)  #[1, 2, [3, 4]] print(b)  #[1, 2, [15, 4]]
 
Q:上面这个例子有一点很奇怪,就是用了deepcopy/copy后为什么地址还是不变呢? 
 A:因为python里万物都是对象,所以数字也是对象。a[0]和b[0]是两个不同的指针,但他们指向的都是”数字1”这个对象的地址,所以他们的id()是相同的。 
id(a[2])!=id(b[2])是因为值不同了 
pickle模块 有时候用python处理数据需要很长时间,暂停后我们希望下次接着处理,这时候就要用到pickle模块
写入pickle文件
import  pickle a={'a' :1 ,'b' :2 ,'c' :3 } file=open('example.pickle' ,'wb' )    pickle.dump(a,file)  file.close()
 
import  pickle file=open('example.pickle' ,'rb' ) b=pickle.load(file)   file.close() print(b)  
 
也可以直接这样用with,就免去了file.close()这一步
import  picklewith  open('example.pickle' ,'rb' ) as  file:     b=pickle.load(file)   print(b)
 
set a={'e' ,'a' ,'b' ,'a' ,'a' ,'c' ,'a' } print(set(a))   s='this is my test'   print(set(s))
 
a={'e' ,'a' ,'b' ,'a' ,'a' ,'c' ,'a' } b=set(a)   b.add('x' )   print(b)     b.add('a' ) print(b)    b.remove('x' )   print(b)    b.discard('w' )   print(b) b.clear()   print(b)    set1={'a' ,'b' ,'c' } set2={'a' ,'x' ,'y' } print(set1.difference(set2))   print(set1.intersection(set2)) 
 
set中怎么sort?
a={'e' ,'a' ,'b' ,'a' ,'a' ,'c' ,'a' } b=set(a)     print(b)     c=list(b)    c.sort()   print(c)    
 
RegEx正则表达 正则表达式 (Regular Expression) 又称 RegEx, 是用来匹配字符的一种工具. 在一大串字符中寻找你需要的内容. 它常被用在很多方面, 比如网页爬虫, 文稿整理, 数据筛选等等. 最简单的一个例子, 比如我需要爬取网页中每一页的标题. 而网页中的标题常常是这种形式.
不用特意记忆,用的时候查一下即可