文件类型一
源代码 -Python源代码文件以“py”为扩展名,由Python程序解释,不需要编译 >>[root@lyon-01 ~]# mkdir python [root@lyon-01 ~]# cd python/ [root@lyon-01 python]# vim 1.py //写一个代码
>>#! /usr/bin/python
>>print 'hellow world'
>>[root@lyon-01 python]# python 1.py //执行 hellow world
文件类型二 字节代码 -Python源代码文件经编译后,生成的扩展名为“pyc”的文件 -编译方法: >>import py_compile py_compile.compile('hello.py')
>>[root@lyon-01 python]# vim 2.py
>>#! /usr/bin/python
>>import py_compile py_compile.compile('./1.py') [root@lyon-01 python]# python 2.py [root@lyon-01 python]# ls 1.py 1.pyc 2.py [root@lyon-01 python]# file 1.pyc 1.pyc: python 2.7 byte-compiled [root@lyon-01 python]# cat 1.py #! /usr/bin/python
>>print 'hellow world' [root@lyon-01 python]# cat 1.pyc
»顁@s dGHdS(s hellow worldN((((s./1.pys //二进制的乱码文件 [root@lyon-01 python]# [root@lyon-01 python]# python 1.pyc //用Python查看 hellow world
[root@lyon-01 python]# -优化代码 -经过优化的源码文件,扩展名为“pyo” -Python -O -m py_compile hello.py
>>[root@lyon-01 python]# python -O -m py_compile 1.py [root@lyon-01 python]# ls 1.py 1.pyc 1.pyo 2.py [root@lyon-01 python]# python 1.pyo
hellow world [root@lyon-01 python]#
python 的变量 --变量是计算机内存中的一块区域,变量可以存储规定范围内的值,而且值可以改变 --Python下的变量是对一个数据的引用 --变量的命名 由字母 数字 下划线组成 不能以数字开头 不可以使用关键字 --变量的赋值 是变量的声明和定义的过程 a = 1 id(a)
>>[root@lyon-01 python]# vim 3.py #! /usr/bin/python num1 = input("please a number: ") num2 = input("please a number: ") print num1 + num2 print num1 - num2 print num1 * num2 print num1 / num2 [root@lyon-01 python]# python 3.py please a number: 5 please a number: 3 8 2 15 1 [root@lyon-01 python]# vim 3.py #! /usr/bin/python num1 = input("please a number: ") num2 = input("please a number: ") print "%s + %s = %s" % (num1, num2, num1+num2) print "%s - %s = %s" % (num1, num2, num1-num2) print "%s * %s = %s" % (num1, num2, num1*num2) print "%s / %s = %s" % (num1, num2, num1/num2) [root@lyon-01 python]# python 3.py please a number: 7 please a number: 3 7 + 3 = 10 7 - 3 = 4 7 * 3 = 21 7 / 3 = 2
python的数据类型 案例 123和‘123’一样吗? 123 代表数值 ‘123’代表字符串 还有 列表 元祖 字典 数值类型 长整型 >In [9]: a = 12999999999999999999999 In [10]: a Out[10]: 12999999999999999999999L In [11]: type (a) Out[11]: long 也可以把小的数值长整型 In [12]: a = 100l In [13]: type (a) Out[13]: long
浮点型 例如 0.0 12.0 -18.8 3e+7 复数型 换行符 >In [23]: a = "hello\nworld" In [24]: print a hello world