zoukankan      html  css  js  c++  java
  • python2和python3的区别

    进入不同语言版本的Python交互环境

    py -2

    py -3

    pip 命令的执行 :

    py -2 -m pip install xxxx

    py -3 -m pip install xxxx

    pip3 install nose 这个也可以

    执行python文件

    py -2 a.py

    py -3 a.py

    也可以进入到python3.6的安装目录

    把python.exe 修改为python3.exe

    在环境变量里面加入这个Python3.6的路径

    Python3改变的点:

    Print重大变化,print 后加括号

    D: estpytyon3>py -3

    Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32

    Type "help", "copyright", "credits" or "license" for more information.

    >>> print ("hello")

    hello

    >>> print ("hello",end="")

    hello>>>

    print ("hello",end=""),没有end默认以回车结尾,end表示以什么结尾,就不是以回车结尾了,

    python3里没有xrange了,只有range,会生成一个迭代器,相当于python2的xrange

    >>> range(1,10)

    range(1, 10)

    >>> list(range(1,10))

    [1, 2, 3, 4, 5, 6, 7, 8, 9]

    >>> 

    >>> for i in range(1,10,2):

    ...     print (i)

    ...

    1

    3

    5

    7

    9

    a=iter(range(10))生成一个迭代器,用next(a)访问

    >>> a=iter(range(10))

    >>> next(a)

    0

    >>> next(a)

    1

    >>> next(a)

    2

    str类型-重大变化

    >>> s="s"

    >>> type(s)

    <class 'str'>

    >>> 

    python3:str类型---》相当于py2的unicode类型

    str类型encode之后变为了bytes类型---》相当于python2的str类型

    >>> s="s"

    >>> type(s)

    <class 'str'>

    >>> type(s.encode('utf-8'))

    <class 'bytes'>

    >>> s="我们"

    >>> print (s)

    我们

    >>> print (s.encode("gbk"))

    b'xcexd2xc3xc7'

    >>> print (s.encode("gbk").decode("gbk"))

    我们

    Python3下即使encode成gbk-和cmd默认编码格式一样,也不能显示,因为encode后是bytes类型,要在unicode格式下才可以显示

    Python3默认是unicode类型

    读文件:可以执行编码格式

    fp=open("d:\a.txt","r",encoding="utf-8")

     

    >>> fp=open("d:\a.txt","r",encoding="utf-8")

    >>> fp.read()

    'ufeff光荣之路学习, 夏晓旭 嘻嘻嘻嘻 '

    With open

    >>> with open("d:\a.txt","r",encoding="utf-8") as fp:

    ...     print (fp.read())

    ...

    光荣之路学习,

    夏晓旭

    嘻嘻嘻嘻

    >>> 

    Python3 MySQL安装

    pip3 install PyMySQL

    import pymysql

    # 打开数据库连接

    db = pymysql.connect("localhost","testuser","test123","TESTDB" )

    # 使用 cursor() 方法创建一个游标对象 cursor

    cursor = db.cursor()

    # 使用 execute()  方法执行 SQL 查询

    cursor.execute("SELECT VERSION()")

    # 使用 fetchone() 方法获取单条数据.

    data = cursor.fetchone()

    print ("Database version : %s " % data)

    # 关闭数据库连接

    db.close()

    pickle序列化

    pickle可以把变量的放在文本里,下次打开时可以从文本里取

    import pickle

    data2 = [1,2,3,4]

    det_str = pickle.dumps(data2)

    print(det_str)

    pickle参考博客

    https://www.cnblogs.com/zhangxinqi/p/8034380.html

    thread改成了threading

    thread 模块已被废弃。用户可以使用 threading 模块代替。所以,在 Python3 中不能再使用"thread" 模块。为了兼容性,Python3 将 thread 重命名为 "_thread"。

    Import queue,首字母不能大写

    >>> import queue

    >>> import Queue

    Traceback (most recent call last):

      File "<stdin>", line 1, in <module>

    ModuleNotFoundError: No module named 'Queue'

    >>> queue.Queue(10)

    <queue.Queue object at 0x000001C4882AE860>

    >>> q=queue.Queue(10)

    >>> q.put(0)

    >>> q.get()

    0

    想要用时间字符串中加中文,需要引入locale的包

    >>> import time

    >>> import locale

    >>> locale.setlocale(locale.LC_CTYPE, 'chinese')

    'Chinese_China.936'

    >>> print(time.strftime('%Y年%m月%d日'))

    2018年08月16日

    写文件时需要注意:不能用encode之类的

    写文件后返回一个写入的字节数

    >>> with open("e:\x.txt","w",encoding="utf-8") as fp:

    ...     fp.write("我们 你们 ")

    ...

    6

    >>> 

    >>> with open("d:\a.txt","w",encoding="utf-8") as fp:

    ...     fp.write("我们")

    ...

    2

    Python3里没有cmp函数了

  • 相关阅读:
    MyBatis学习笔记
    通过Executor来启动线程比用Thread的start()更好
    步骤2:JMeter 分布式测试(性能测试大并发、远程启动解决方案)
    步骤1:JMeter 录制脚本接口测试
    adb常用命令(手机测试)
    电商购物核心功能测试点
    APP测试基本流程以及APP测试要点
    Jmeter-jtl性能测试报告转换-2种导出方法
    Linux之find查找命令
    linux之top命令详解
  • 原文地址:https://www.cnblogs.com/xiaxiaoxu/p/9529025.html
Copyright © 2011-2022 走看看