【Love Python】【01】认识可爱的python

2013-09-26 01:13:39 56 7905 2
一,python特性概要
     1,python是解释型语言,我们和c比较一下
root@GK:~# cat hello.c 
#include "stdio.h"

int main(void){
        printf("hello t00ls \n");
}
root@GK:~# gcc hello.c -o hello
root@GK:~# ./hello
hello t00ls
root@GK:~#
每次修改源文件都要重新编译一下,python则不用
root@GK:~# cat hello.py 
print "hello t00ls!"

root@GK:~# python hello.py
hello t00ls!
脚本解释性语言的内部机制
            1,python先将脚本编译成字节码(pyc,pyo)
            2,python虚拟机解释并运行字节码文件
        
        
            编译型语言的内部机制
            1,先将源代码编译成机器码,生成可执行文件
            2,运行可执行文件
            
            所以编译型语言的效能要比脚本解释性高;
            
            python特性总结:字节码,动态,缩进。
            1    字节码
            2    动态语义(在赋值时确定数据类型)
            3    缩进(需要注意不同的编辑器中tab键实现的有时不是4个空格,这时候就需要设置下编辑器,否则很容易出错并且不容易排查,想起了freebuf上某大牛说的买了个exp回来后自己一行行缩进后才能用。。。。)
            4    python之禅(了解下)

root@GK:~# python
Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>>

二,无规矩不成方圆,简述python编程的基本方式。
vi  one.py
#coding=utf-8
            
#上面其实你随便怎么写都可以。只要符合 coding[:=]\s*([-\w.]+)
#比如这种卖萌写法:-*- coding:utf-8 -*-
            
"这是一个标准模块脚本的写作范式,此处为该脚本文档。"
            
#这里给下面语句做一个注释
new_str = "这是一个全局变量" #这样写注释也可以
            
def hello():
        """
        这是一个函数定义的多行注释。
        继续多行。
        没有问题。
    """
            
        return "hello world"
            
        #程序主体
if __name__ == "__main__":
        print hello()
vi one_1.py
#coding=utf-8

import one

print one.__doc__
print one.new_str
print one.hello()
执行one_1.py看下什么是字节码文件(.pyc)
root@GK:~# ls
hello.c                hello.py        one.py                one_1.py
root@GK:~# python one_1.py
这是一个标准模块脚本的写作范式,此处为该脚本文档。
这是一个全局变量
hello world
root@GK:~# ls
hello.c                hello.py        one.py                one.pyc                one_1.py
root@GK:~#

自己打开pyc文件了解下import带来的便利:)

友情提示本文档为连载系列:)

关于作者

抚菊深思49篇文章1528篇回复

评论56次

要评论?请先  登录  或  注册