第一章练习

来源:互联网 发布:杰钢队长 知乎 编辑:程序博客网 时间:2024/05/17 23:19
1-1 安装Python。请检查Python是否已经安装到你的系统上,如果没有,请下载安装它。
答:已安装
tmyyss@ubuntu:~$ python
Python 2.7.3 (default, Feb 27 2014, 19:58:35) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

1-2 执行Python。有多少种运行Python的不同方法?你喜欢哪一种?为什么?
答:(1)命令行上的交互式解释器(python解释器)
(2)从命令行上启动脚本
(3)集成开发环境

1-3 Python标准库。
(a)请找到系统中Python执行程序的安装位置和标准库模块的安装位置。
(b)看看标准库里的一些文件,比如string.py。这会帮助你适应阅读Python脚本。
答:(a)我用的Ubuntu,python执行程序的安装位置是/usr/bin/python,Python标准库模块的安装位置是/usr/lib/python2.7

(b)略

1-4 交互执行。启动你的Python交互解释器。你可以通过输入完整的路径名来启动它。当然,如果你已经在搜索路径中设置了它的位置,那么只输入它的名字(python或者python.exe)就行了(你可以选者最适合你的的Python实现方式,例如命令行、图形用户接口/集成开发环境、Jython, IronPython或者Stackless)。启动界面看上去就像本章描述的一样,一旦你看到>>>提示符,就意味着解释器准备好要接受你的Python命令了。
试着输入命令print 'Hello World!'(然后按回车键),完成著名的Hello World!程序,然后退出解释器。在Unix系统中,按下Ctrl+D会发送EOF信号来终止Python解释器。在DOS系统中,使用的组合键是Ctrl+Z。如果要从Macintosh、PythonWin、Windows或者Unix中的IDLE这样的图形用户环境推出,只要简单的关闭相关窗口就可以了。
答:tmyyss@ubuntu:~$ python
Python 2.7.3 (default, Feb 27 2014, 19:58:35) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello world!'
hello world!
>>> 
tmyyss@ubuntu:~$ 


1-5 编写脚本。作为练习1-4的延续,创建“Hello World!”的Python脚本其实和上面的交互性练习并不是一回事。如果你在使用Unix系统,尝试建立自动运行代码行,这样你就可以在没有调用Python解释器的情况下运行程序了。
答:
tmyyss@ubuntu:~/python/core_program$ more hello.py 
#!/usr/bin/env python
#filnename:hello.py


print 'Hello world!'

tmyyss@ubuntu:~/python/core_program$ python hello.py 
Hello world!
tmyyss@ubuntu:~/python/core_program$ 

1-6 编写脚本。使用Print语句编写脚本,在屏幕上显示你的名字、年龄、最喜欢的颜色和与你相关的一些事情(背景、兴趣、爱好等)。
答:
tmyyss@ubuntu:~/python/core_program$ more print.py 
#!/usr/bin/python
#filename:print.py

print "My name is tmyyss."
print "My age is 27."
print "My hobby is programming."
print "My favorite color is green."


tmyyss@ubuntu:~/python/core_program$ ./print.py 
My name is tmyyss.
My age is 27.
My hobby is programming.
My favorite color is green.
tmyyss@ubuntu:~/python/core_program$
0 0
原创粉丝点击