python小笔记

  python支持编译和解释、面向对象、无须考虑内存管理,很强的可扩展性,可以把部分功能用C或C++实现,免费、开源。很强的可移植性和丰富类库。
第一个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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值