python随笔

with语句:可自动关闭文件 with 可同时打开多个文件
with open("once","r",encoding="utf-8") as  f:
for line in f
print(line)
规范:一行代码不应该超过80个字符
----------------字符编码与转码-----------------
gb2312 7000
gbk 20k
unicode  中文和英文都占2个字节  16位  万国编码 所有的编码的爸爸
ascall 不能存中文 8位
Unicode 的扩展集 utf-8 英文 ascall码 中文字符三个字节 
-------------python2转码-------gbk---------------------------
#-*- coding:utf-8-*-
s="信号"   //s=u"你好"    utf-8 《=》unicode
s_to_gbk=s.decode("utf-8").encode("gbk")
print(s_to_gbk)
gbk_to_utf8=s_to_gbk.decode("gbk").encode("utf-8")
print(gbk_to_utf8)
----------
python 2.7编码
import sys
print(sys.getdefaultCoding())
----------unicode-------------Python3-------


s="你哈"          
print(s.encode("gbk"))      //encode()  默认utf-8 又变成byte
gbk_to_utf8=s.decode("gbk").encode("utf-8")
print(gbk_to_utf8)
-----------------函数与函数式方程----------------------
编程方式:
面向对象--》类 class
面向过程--》过程 def   无返回值
函数式编程--》 函数 def
函数式逻辑结构化和过程化的一种编程方法
def test(x):
"""这是文档描述"""
x+=1
return x
面向过程也被定义为函数 返回值none
---------------------------------
dimport time
def logger():
time _format='%Y-%m-%d %X
time_current = time.strftime(time_format)
with open('a.txt','ab') as  f:
f.write('end action')
def test1():
print('text1 starting action...')
def logger()
def test2():
print('text2 starting action...')
def logger()
def test3():
print('text3 starting action...')
def logger()
logger()
test1()
test2()
test3()
--------------------------
def test1():
print('in the test1')
return 1,'hello',['alex','opeiqi']   //会返回一个元组
print('test end')
x=test1()
print(x)
return test()  //可以返回一个函数
为什么要有返回值 因为想要一个的结果
--------------------------------------------------
test(1,3)   位置参数调用
test(x=1,y=2) 关键字调用
test(x=2,3)  报错
test(2,y=2)  ok
关键参数不能写在位置参数前面
test(2,z=2,y=6)
默认参数 def 形参时 给定确定的值
------------------参数组------------------
def test(*args):
print(args)
test(1,3,4,4,21,)
test(*[1,3,2,5])
def test(1,*args):
test(f,f,d,f,f,,f,f,f,):


def test(**kwargs): //把关键字参数,转换成字典的方式
test2(name='alex',age=8)  //字典
-------------------------局部变量--------------------------------
 #school="oldboy"  //global
  global school="mage" //只是内部的变量,只有 声明后才可以

   def change_name(name):
global school="mage" //只是内部的变量,只有 声明后才可以
print("before change",name,school)
name ="alex li"  ////局部变量 只在函数里生效
print("after name",name)
  


 name="alex"  
 change_name(name)
 print(name)
 print("school",school)   //不要再在函数里改全局变量
---------------------------------------------------
names=["alex","jack","Rain"]
def change_name():

names[0]="金角"
print(names)
//复杂数据类型可以在局部改全局 ,除过tupple
change_name()
print(names)
----------------recursion (999)----------递归-----调自己------------
//要有明确的结束条件
//每进入更深一层递归时,问题规模会减少
//效率不高 运行函数时 栈加一层帧 返回时减一层
def calc(n):
print(n)
if int(n/2) >0:
return calc(int(n/2))
print("->",n)

calc(10)
-----------------------------函数与函数式方程-----------------













猜你喜欢

转载自blog.csdn.net/weixin_41752144/article/details/81017707
今日推荐