没有合适的资源?快使用搜索试试~ 我知道了~
首页Python编程教程:开源授权与资源链接
Python编程教程:开源授权与资源链接
需积分: 13 0 下载量 126 浏览量
更新于2024-07-17
收藏 857KB PDF 举报
"《[维基百科书籍]Python编程》是一本由wikibooks.org提供的关于Python编程的在线教程,最初发布日期为2012年6月22日。该书的内容在2012年4月28日被授权使用Creative Commons Attribution-ShareAlike 3.0 Unported许可证,这意味着用户可以根据这一许可协议自由地复制、修改和分享作品,但要求在再创作的作品中保持相同的或兼容的授权声明。章节143包含了所有贡献者的名单,体现了开源社区的合作精神。 书中还提到了GNU通用公共许可证(GPL)、GNU Lesser General Public License(LGPL)以及GNU Free Documentation License(GFDL),这些开源许可可能适用于本书的部分或全部内容。如果使用了这些许可证,读者需要在使用相应内容时遵循相应的条款。在149页的列表中列出了所有图片的许可证信息。 LaTeX排版软件被用于生成这个PDF版本,源代码作为附件(source.7z.txt)附带在文档中。提取源代码的建议方法是通过访问http://www.pdfLaTeX相关的工具。这体现了本书对开放源码原则的实践,不仅内容共享,连编排工具也鼓励开源和可访问性。《Python Programming》是一本适合学习Python编程语言,同时也是开源文化的一个实例,它强调了代码透明度和社区合作的重要性。"
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/11571131/bg10.jpg)
Creating Python programs
4.1.1 Windows
•
Create a folder on your computer to use for your Python programs, such as
C:\pythonpractice, and save your hello.py program in that folder.
•
In the Start menu, select "Run...", and type in
cmd
. This will cause the Windows terminal to open.
•
Type
cd \pythonpractice
to
c
hange
d
irectory to your
pythonpractice
folder, and hit
Enter.
• Type python hello.py to run your program!
If it didn't work, make sure your PATH contains the python directory. See Getting Python
4
.
4.1.2 Mac
•
Create a folder on your computer to use for your Python programs. A good suggestion would be to
name it
pythonpractice
and place it in your Home folder (the one that contains folders for
Documents, Movies, Music, Pictures, etc). Save your hello.py program into this folder.
• Open the Applications folder, go into the Utilities folder, and open the Terminal program.
•
Type
cd pythonpractice
to
c
hange
d
irectory to your
pythonpractice
folder, and hit
Enter.
• Type python hello.py to run your program!
4.1.3 Linux
•
Create a folder on your computer to use for your Python programs, such as
˜/pythonpractice
,
and save your hello.py program in that folder.
•
Open up the terminal program. In KDE, open the main menu and select "Run Command..." to open
Konsole. In GNOME, open the main menu, open the Applications folder, open the Accessories
folder, and select Terminal.
•
Type
cd ˜/pythonpractice
to
c
hange
d
irectory to your
pythonpractice
folder, and
hit Enter.
• Type python hello.py to run your program!
Note:
If you have both python version 2.6.1 and version 3.0 installed (Very possible if you are using
Ubuntu, and ran
sudo apt-get python3
to have python3 installed), you should run
python3
hello.py
An Alternative
There is a file called
idle.py
in your Python file. It is in the idlelib folder, located in the Lib folder.
This is a Python programmer written in Python. You might find it a bit easier to use than cmd.
4 Chapter 2 on page 5
12
![](https://csdnimg.cn/release/download_crawler_static/11571131/bg11.jpg)
Exercises
4.1.4 Result
The program should print:
Hello, world!
Congratulations! You're well on your way to becoming a Python programmer.
4.2 Exercises
1.
Modify the
hello.py
program to say hello to a historical political leader (or to Ada
Lovelace
5
).
2. Change the program so that after the greeting, it asks, "How did you get here?".
3.
Re-write the original program to use two
print
statements: one for "Hello" and one for
"world". The program should still only print out on one line.
Solutions
6
4.3 Notes
5 http://en.wikipedia.org/wiki/Ada%20Lovelace
6 http://en.wikibooks.org/wiki/Python%20Programming%2FCreating%20Python%
20programs%2FSolutions
13
![](https://csdnimg.cn/release/download_crawler_static/11571131/bg12.jpg)
![](https://csdnimg.cn/release/download_crawler_static/11571131/bg13.jpg)
5 Basic syntax
There are five fundamental concepts in Python
1
.
5.0.1 Case Sensitivity
All variables are case-sensitive. Python treats 'number' and 'Number' as separate, unrelated entities.
5.0.2 Spaces and tabs don't mix
Because whitespace is significant, remember that spaces and tabs don
'
t mix, so use only one or the
other when indenting your programs. A common error is to mix them. While they may look the same
in editor, the interpreter will read them differently and it will result in either an error or unexpected
behavior. Most decent text editors can be configured to let tab key emit spaces instead.
Python's Style Guideline described that the preferred way is using 4 spaces.
Tips: If you invoked python from the command-line, you can give -t or -tt argument to python to
make python issue a warning or error on inconsistent tab usage.
pythonprogrammer@wikibook: python − tt myscript. py
This will issue an error if you have mixed spaces and tabs.
5.0.3 Objects
In Python, like all object oriented languages, there are aggregations of code and data called Objects,
which typically represent the pieces in a conceptual model of a system.
Objects in Python are created (i.e., instantiated) from templates called Classes
2
(which are covered
later, as much of the language can be used without understanding classes). They have "attributes",
which represent the various pieces of code and data which comprise the object. To access attributes,
one writes the name of the object followed by a period (henceforth called a dot), followed by the
name of the attribute.
1 http://en.wikibooks.org/wiki/Python%20Programming
2 Chapter 19 on page 79
15
![](https://csdnimg.cn/release/download_crawler_static/11571131/bg14.jpg)
Basic syntax
An example is the
'
upper
'
attribute of strings, which refers to the code that returns a copy of the string
in which all the letters are uppercase. To get to this, it is necessary to have a way to refer to the object
(in the following example, the way is the literal string that constructs the object).
'bob'.upper
Code attributes are called "methods". So in this example, upper is a method of
'
bob
'
(as it is of all
strings). To execute the code in a method, use a matched pair of parentheses surrounding a comma
separated list of whatever arguments the method accepts (upper doesn
'
t accept any arguments). So to
find an uppercase version of the string 'bob', one could use the following:
'bob'.upper()
5.0.4 Scope
In a large system, it is important that one piece of code does not affect another in difficult to predict
ways. One of the simplest ways to further this goal is to prevent one programmer
'
s choice of names
from preventing another from choosing that name. Because of this, the concept of scope was invented.
A scope is a "region" of code in which a name can be used and outside of which the name cannot be
easily accessed. There are two ways of delimiting regions in Python: with functions or with modules.
They each have different ways of accessing the useful data that was produced within the scope from
outside the scope. With functions, that way is to return the data. The way to access names from other
modules lead us to another concept.
5.0.5 Namespaces
It would be possible to teach Python without the concept of namespaces because they are so similar
to attributes, which we have already mentioned, but the concept of namespaces is one that transcends
any particular programming language, and so it is important to teach. To begin with, there is a
built-in function
dir()
that can be used to help one understand the concept of namespaces. When
you first start the Python interpreter (i.e., in interactive mode), you can list the objects in the current
(or default) namespace using this function.
Python 2.3.4 (#53, Oct 18 2004, 20:35:07) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['__builtins__', '__doc__', '__name__']
This function can also be used to show the names available within a module namespace. To
demonstrate this, first we can use the type() function to show what __builtins__is:
>>> type(__builtins__)
<type 'module'>
Since it is a module, we can list the names within the __builtins__namespace, again using the
dir()
function (note the complete list of names has been abbreviated):
>>> dir(__builtins__)
['ArithmeticError', ... 'copyright', 'credits', ... 'help', ... 'license', ...
16
剩余158页未读,继续阅读
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/default.jpg!1)
chunyangsuhao
- 粉丝: 102
- 资源: 7382
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
最新资源
- AirKiss技术详解:无线传递信息与智能家居连接
- Hibernate主键生成策略详解
- 操作系统实验:位示图法管理磁盘空闲空间
- JSON详解:数据交换的主流格式
- Win7安装Ubuntu双系统详细指南
- FPGA内部结构与工作原理探索
- 信用评分模型解析:WOE、IV与ROC
- 使用LVS+Keepalived构建高可用负载均衡集群
- 微信小程序驱动餐饮与服装业创新转型:便捷管理与低成本优势
- 机器学习入门指南:从基础到进阶
- 解决Win7 IIS配置错误500.22与0x80070032
- SQL-DFS:优化HDFS小文件存储的解决方案
- Hadoop、Hbase、Spark环境部署与主机配置详解
- Kisso:加密会话Cookie实现的单点登录SSO
- OpenCV读取与拼接多幅图像教程
- QT实战:轻松生成与解析JSON数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)