python基础学习(一) 第一个python程序

1. 使用python/python3解释器的方式

按照惯例,我们都是以Hello world作为一门程序语言的开始,进行如下的操作:

  • 在桌面上新建一个hello-python文件夹
  • 进入hello-python文件夹,新建hello.py的文件夹(python源文件是以.py结尾的)
  • 使用vim编辑器,输入print("Hello world")
  • 使用目录执行:python hello.py

对应的命令如下:

$ mkdir hello-python
$ touch hello.py
$ vim hello.py 
$ python hello.py 
Hello Python
$

至此第一个python程序完成了,感觉比java要简单不少。

2. 使用python交互命令的方式

  • 执行命令:python
  • 在Python的shell中输入:print("Hello world")

对应的命令如下:

$ python
Python 2.7.5 (default, Jun 17 2014, 18:11:42) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello world")
Hello world
>>> exit()
$  

 3. 使用IPython

  • 输入命令:ipython
  • ipython交互命令中输入:print("Hello world")

对应的命令如下:

$ ipython
Python 2.7.5 (default, Jun 17 2014, 18:11:42) 
Type "copyright", "credits" or "license" for more information.

IPython 5.8.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: print("Hello world")
Hello world

In [2]: 

 4. 使用PyCharm

  • 打开PyCharm专业版(PyCharm的安装请查看我的这篇文章:安装PyCharm),菜单中File / Open... 选择1中所创建的hello-python文件夹
  • 打开项目的方式
  • 选择文件hello.py右键/Run 'hello'

     

  •  查看控制台输出结果

猜你喜欢

转载自www.cnblogs.com/zhangfengxian/p/hello-python.html