Python学习

1.ASCII(1个字节) ->Unicode(多语言的出现 改为两个字节)->UTF-8(考虑存储资源 多数字符为英文 改为多字节表示 类似可变长编码 在最新的Python 3版本中,字符串是以Unicode编码)

 

# -*- coding: utf-8 -*-

告诉Python解释器,按照UTF-8编码读取源代码,否则,你在源代码中写的中文输出可能会有乱码

申明了UTF-8编码并不意味着你的.py文件就是UTF-8编码的,必须并且要确保文本编辑器正在使用UTF-8 编码(ubuntu 默认是UTF Windows 默认Unicode,将.py文件上传到Ubuntu上注意文件是UTF 否则中文有可能乱码)

 

2.replace 函数是创建了一个新对象  不改变原数据

a="bbcv"
b=a.replace('b','d',1)
print (a)

print (b)

结果:

bbcv
dbcv

.Python内置的enumerate函数可以把一个list变成索引-元素对,这样就可以在for循环中同时迭代索引和元素本身:

a=[1,4,"ad"]
my_function(a)
for id,key in enumerate(a):
    print (id,key)

0 1
1 4
2 ad

4.杨辉三角

def triangles(n):
    a = [1]
    if n==0:
        pass
    else:
        print (a)
        while n>1:
            b=[1]
            for id,key in enumerate(a):
                if id==0:
                    continue
                else:
                    b.append(a[id-1]+a[id])
            b.append(1)
            n=n-1
            print (b)
            a=b

triangles(10)

4.assert 类似print 错误信息 然后退出-1 好处是:启动Python解释器时可以用-O(大写字母)参数来关闭assert,此时就相当于一个pass

def test1(a):
    assert a!=0 ,'a is 0'
    print (123)
test1(0)

E:\PycharmProjects\untitled\venv\Scripts\python.exe E:/PycharmProjects/untitled/tes1.py
Traceback (most recent call last):
  File "E:/PycharmProjects/untitled/tes1.py", line 14, in <module>
    test1(0)
  File "E:/PycharmProjects/untitled/tes1.py", line 12, in test1
    assert a!=0 ,'a is 0'
AssertionError: a is 0

Process finished with exit code 1

5.windows 下执行python:cmd ->输入“python“ +“空格”+文件的绝对位置(直接将文件拖入光标处)

  多行注释快捷键 ctr+/

6.doctest:将注释代码按照某些规定编写,通过doctest库可以执行注释代码 从而拥有了注释代码能测试代码的效果

def fact(n):
    '''
    Calculate 1*2*...*n

    >>> fact(1)
    1
    >>> fact(2)
    1
    >>> fact(3)
    6
    >>> fact(-1)
    ?
    '''
    if n < 1:
        raise ValueError()
    if n == 1:
        return 1
    return n * fact(n - 1)
if __name__ == '__main__':
    import doctest
    doctest.testmod()



E:\PycharmProjects\untitled\venv\Scripts\python.exe E:/PycharmProjects/untitled/tes1.py
**********************************************************************
File "E:/PycharmProjects/untitled/tes1.py", line 17, in __main__.fact
Failed example:
    fact(2)
Expected:
    1
Got:
    2
**********************************************************************
File "E:/PycharmProjects/untitled/tes1.py", line 21, in __main__.fact
Failed example:
    fact(-1)
Exception raised:
    Traceback (most recent call last):
      File "C:\Users\sangfor\AppData\Local\Programs\Python\Python37-32\lib\doctest.py", line 1329, in __run
        compileflags, 1), test.globs)
      File "<doctest __main__.fact[3]>", line 1, in <module>
        fact(-1)
      File "E:/PycharmProjects/untitled/tes1.py", line 25, in fact
        raise ValueError()
    ValueError
**********************************************************************
1 items had failures:

7.操作文件

import os
print (os.path.abspath('.'))  #获取文件的绝对路径
my_mkdir = os.path.join(os.path.abspath('.'),'nextdir') #创建目录名
os.mkdir(my_mkdir) #创建目录
os.rmdir(my_mkdir) #删除目录

8.序列化:将对象或数据类型转换成可以存储或者传输的形式 并且可以通过反序列化获得对象 

常用的json模型

import json
dict={"name":"zwg","score":100,"age":25}
with open("test.json","w") as f:
    json_str = json.dumps(dict) #将dict数据类型转换为str
    json.dump(json_str,f)       #将str数据类型写到json文件
with open("test.json","r") as f:
    json_str = json.load(f)     #从json文件读出数据为str
dict = json.loads(json_str)     #将str类型转换成dict类型
print (dict)


E:\PycharmProjects\untitled\venv\Scripts\python.exe E:/PycharmProjects/untitled/filetest.py
{'name': 'zwg', 'score': 100, 'age': 25}

Process finished with exit code 0

8.获取时间

print time.time()
print time.localtime(time.time())
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
1532911465.1
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=30, tm_hour=8, tm_min=44, tm_sec=25, tm_wday=0, tm_yday=211, tm_isdst=0)
2018-07-30 08:44:25

1.元组 列表 字符串 集合 字典

列表类似数组 可以修改某一个元素 例:a= [1,2,3,4] a[0]=2 元组用小括号表示(1,23)它和字符串一样 不能修改某一个元素

集合(set)类似C++set 是一个无序不重复元素的序列

字典有点类似C++中的map 键值对

 

2.Python也是面向对象的 类似C++

 

3.loads load dump dumps

loads将str转换成dic dumps 将dic转换成str(内置数据之间的转换)

dump和load都是针对文件的操作 dump是将Python数据保存json load是读取json数据(结果是str)

json.dumps : dict转成str     json.dump是将python数据保存成json

json.loads:str转成dict          json.load是读取json数据 

 

4.Python的copy函数

Python的赋值是传对象的引用

 

copy是浅拷贝 copy.copy(a)

deepcopy 深拷贝类似C++用法

 

4.python get和post方法

https://www.cnblogs.com/goldd/p/5457229.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值