zoukankan      html  css  js  c++  java
  • Python学习笔记(二)-Python文件类型及编程模式

    Python环境搭建:
    linux,Windows。。。

    Linux下:
    [root@localhost StudyPython]# python #进入交互模式
    Python 2.7.11 (default, Jan 3 2017, 02:04:00)
    [GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 1+1
    2
    >>> print 'Hello World!'
    Hello World!
    >>> exit()#退出交互模式


    [root@localhost StudyPython]# vim 20170804-01.py #文本模式,就是把代码写在文件里,执行执行文件

    print'Hello World!'

    [root@localhost StudyPython]# python 20170804-01.py
    Hello World!


    Python文件类型:
    1.源代码:
    python源代码文件以".py"为拓展名,由Python程序解释,不需要编译


    #!/usr/bin/python
    Python代码首行加上这一段,在linux下,再赋予chmod +x 01.py可执行权限,可以直接用./执行

    [root@localhost 20170804]# ll
    总用量 4
    -rw-r--r-- 1 root root 20 1月 3 05:19 01.py

    --没有x执行权限,就必须用python程序来执行,python 01.py
    [root@localhost 20170804]# vi 01.py

    #!/usr/bin/python
    print'Hello World!'
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    "01.py" 2L, 38C 1,1 全部


    [root@localhost 20170804]# chmod +x 01.py
    [root@localhost 20170804]# ll
    总用量 4
    -rwxr-xr-x 1 root root 38 1月 3 05:33 01.py
    [root@localhost 20170804]# ./01.py
    Hello World!

    特殊注释
    比如#! /usr/bin/python 这句注释的意思就是告诉LINUX/UNIX去找到python的翻译器,大部分文件都不需要这个,只在要求也执行的文件中添加。

    关于蛋疼的中文编码: # coding = utf-8 【注:这句代码蛋疼的必须放在第二行啊,而且多个空格都不行啊!】

    2.字节代码:
    python源文件经编译后生成的拓展名为".pyc"的文件
    编译方法:
    import py_compile
    py_compile.compile("hello.py")

    import py_compile

    py_compile.compile('01.py')


    3.优化代码:
    经过优化的源文件,拓展名为".pyo"
    python -O -m py_compile hello.py

    ====以上三种均可直接运行。
    [root@localhost 20170804]# python 01.pyo
    Hello World!
    [root@localhost 20170804]# python 01.py
    Hello World!
    [root@localhost 20170804]# python 01.pyc
    Hello World!
    [root@localhost 20170804]#

    C:>python
    Python 2.7.7 (default, Jun 1 2014, 14:17:13) [MSC v.1500 32 bit (Intel)] on win
    32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import py_compile
    >>> py_compile.compile('01.py')
    >>>==此时就会在C:下生成01.pyc文件

  • 相关阅读:
    rabbitmq-高级(死信队列)
    rabbitmq-高级(TTL过期时间)
    springboot整合rabbitmq(topic主题模式)
    springboot整合rabbitmq(direct路由模式)
    glide图片加载库
    自己封装的OKhttp请求
    手机上搭建微型服务器
    listview实现点击条目上的箭头展开隐藏菜单。
    recycleview + checkbox 实现单选
    recycleview中使用checkbox导致的重复选中问题
  • 原文地址:https://www.cnblogs.com/helloworldcc/p/7736866.html
Copyright © 2011-2022 走看看