Python 基础【第二篇】python操作模式

一、交互模式

1 #python
2 
3 Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
4 
5 [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
6 
7 Type "help", "copyright", "credits" or "license" for more information.
8 
9 >>> 

 

 

如上所示:

其中”>>>” 为Python的交互命令行这里直接输入代码即可得到结果

例:

输入1+1 Python运算得出结果 2

1 >>>1+1
2 
3 2

 

 

Print “this is python!” 则打印输出字符串

>>> print 'this is python!'

this is python!

退出Python “exit ()”

>>> exit ()

 

 

二、 文本模式

所谓的文本模式其实就是将代码写到一个文件里 然后用Python去执行

编辑一个文件输入如下代码:

 1 [root@fengyuba_server py]# vim py
 2 
 3 print 'this is python'
 4 
 5 print 100+200
 6 
 7 [root@fengyuba_server py]# python py
 8 
 9 this is python
10 
11 300

 

 

*(上面每次执行脚本的时候都会敲python来执行感觉很麻烦其实有更简单的,直接在文件中指定python的bin路径这样每次执行就不用敲python了)*

当然首先确定你python的bin路径

查看python命令路径

[root@fengyuba_server py]# which python

/usr/bin/python

 

 

文件中指定python

[root@fengyuba_server py]# vim pytest

#!/usr/bin/python

print 'this is python'

print '100+200=',100+200

执行结果

[root@fengyuba_server py]# ./pytest

this is python

100+200= 300

 

 

三、 两种方式各有优点和缺点

1. 交互模式

优点:快速得到结果

缺点:退出不保存

适合简短代码测试

2. 文本模式

优点:代码能够得以保存

缺点:操作不方便

适合复杂代码编程

posted on 2015-12-08 16:30  Alanpy  阅读(1268)  评论(0编辑  收藏  举报

导航