python支持编译和解释、面向对象、无须考虑内存管理,很强的可扩展性,可以把部分功能用C或C++实现,免费、开源。很强的可移植性和丰富类库。
第一个python脚本
=====================================================
python中的变量
查看变量的地址:
第一个python脚本
[root@hc8 python]# cat helloworld.py
print 'hello world'
[root@hc8 python]# python helloworld.py
hello world
第一个交互命令:
[root@hc8 python]# python
Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello world'
hello world
>>> exit();
[root@hc8 python]#
比较典型的python脚本
[root@hc8 python]# cat helloworld.py
#!/usr/bin/python
print 'hello world'
python也可以直接执行字节码,需要先编译,编译好后会生成pyc的字节码文件
[root@hc8 python]# cat compile.py
#!/usr/bin/python
import py_compile
py_compile.compile('helloworld.py')
[root@hc8 python]# ./compile.py
[root@hc8 python]# ll
总用量 12
-rwxr-xr-x 1 root root 74 1月 24 15:15 compile.py
-rwxr-xr-x 1 root root 20 1月 24 15:11 helloworld.py
-rw-r--r-- 1 root root 121 1月 24 15:16 helloworld.pyc
[root@hc8 python]# python helloworld.pyc
hello world
优化后的字节码文件
[root@hc8 python]# python -O -m py_compile helloworld.py
[root@hc8 python]# ll
总用量 16
-rwxr-xr-x 1 root root 74 1月 24 15:15 compile.py
-rwxr-xr-x 1 root root 38 1月 24 15:18 helloworld.py
-rwxr-xr-x 1 root root 121 1月 24 15:19 helloworld.pyc
-rw-r--r-- 1 root root 121 1月 24 15:23 helloworld.pyo
[root@hc8 python]# python helloworld.pyo
hello world
总之python的文件主要包含 py pyc pyo三类文件
=====================================================
python中的变量
查看变量的地址:
>>> id(a)
17802040
测试小脚本:
[root@hc8 python]# cat compute.py
#!/usr/bin/python
n1=int(raw_input("input first number:"))
n2=int(raw_input("input second number:"))
print "the result is :"
print n1+n2