文件类型一
源代码
-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.py<module>s //二进制的乱码文件
[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 > >[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, num1num2)
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
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com