没有合适的资源?快使用搜索试试~ 我知道了~
首页Python编程基础:从指令到交互与实践
Python编程基础:从指令到交互与实践
需积分: 9 7 下载量 159 浏览量
更新于2024-07-18
收藏 9.5MB PDF 举报
"《Python编程基础》是Rick Halterman撰写的一本教材,它深入探讨了计算机程序设计的基本原理和使用Python语言进行编程的方法。在书中,作者从软件开发的宏观视角出发,强调了程序作为一系列指令,如何通过控制计算机内部电路的电信号流动,实现对内存的操作、与输入设备如屏幕、键盘和网络交互,从而帮助人们完成各种任务,包括计算金融数据和进行游戏对战。 章节1介绍了编程的背景,讨论了软件的概念、开发工具的选择以及学习编程,特别是使用Python的动机。通过编写简单的Python程序,读者能够理解程序执行的基本流程,以及Python交互式shell的作用,这是一个实时测试和调试代码的重要环境。 在第2章中,核心概念如数值类型(整数、浮点数)和变量被深入解析。讲解了如何声明和赋值变量,以及标识符的使用规则。控制代码嵌入字符串中、用户输入的处理以及打印功能的控制都得到了详细的介绍。此外,字符串格式化和多行字符串的处理也是这一部分的重点。 第三章转向表达式和算术运算,包括不同类型的表达式及其组合,解释了运算符的优先级和结合性,这对于正确理解和编写复杂的计算逻辑至关重要。同时,书中还涉及了注释的使用,这对于代码的可读性和维护性非常重要。 《Fundamentals of Python Programming》旨在为初学者提供一个坚实的基础,使他们掌握基本的编程概念和语法,并通过实践练习提升技能。通过阅读和跟随书中的例子,读者将逐步学会如何用Python构建实用的程序,从而进入更高级别的问题解决和软件开发领域。这本书不仅适合大学入门课程,也适合自学者和希望深化Python基础的开发者阅读。"
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/10396025/bg10.jpg)
1.4. WRITING A PYTHON PROGRAM 5
Python is used for software development at companies and organizations such as Google, Yahoo, Face-
book, CERN, Industrial Light and Magic, and NASA. Experienced programmers can accomplish great
things with Python, but Python’s beauty is that it is accessible to beginning programmers and allows them
to tackle interesting problems more quickly than many other, more complex languages that have a steeper
learning curve.
More information about Python, including links to download the latest version for Microsoft Windows,
Mac OS X, and Linux, can be found at http://www.python.org.
In late 2008, Python 3.0 was released. Commonly called Python 3, the current version of Python is
incompatible with earlier versions of the language. Currently the Python world still is in transition between
Python 2 and Python 3. Many existing published books cover Python 2, but more Python 3 resources now
are becoming widely available. The code in this book is based on Python 3.
This book does not attempt to cover all the facets of the Python programming language. Experienced
programmers should look elsewhere for books that cover Python in much more detail. The focus here is on
introducing programming techniques and developing good habits. To that end, our approach avoids some
of the more esoteric features of Python and concentrates on the programming basics that transfer directly to
other imperative programming languages such as Java, C#, and C
++
. We stick with the basics and explore
more advanced features of Python only when necessary to handle the problem at hand.
1.4 Writing a Python Program
The text that makes up a Python program has a particular structure. The syntax must be correct, or the
interpreter will generate error messages and not execute the program. This section introduces Python by
providing a simple example program.
A program consists of one or more statements. A statement is an instruction that the interpreter executes.
The following statement invokes the print function to display a message:
print("This is a simple Python program")
We can use the statement in a program. Listing 1.1 (simple.py) is one of the simplest Python programs that
does something:
Listing 1.1: simple.py
print("This is a simple Python program")
We will use Wingware’s WingIDE 101 to develop our Python programs. This integrated development
environment is freely available from http://wingware.com/downloads/wingide-101, and its target au-
dience is beginning Python programmers. Its feature set and ease of use make WingIDE 101 an ideal
platform for exploring programming in Python.
The way you launch WingIDE 101 depends on your operating system and how it was installed. Fig-
ure 1.1 shows a screenshot of WingIDE 101 running on a Windows 8.1 computer. The IDE consists of a
menu bar at the top, along with a tool bar directly underneath it, and several sub-panes within the window.
The large, unlabeled pane in the upper left portion of the window is the editor pane in which we type in
our program’s source code. The versions of WingIDE 101 for Apple Mac OS X and Linux are similar in
appearance.
To begin entering our program, we will choose the New item from the File menu (File→New menu
sequence), as shown in Figure 1.2. This action produces a new editor pane for a file named Unititled-1.py.
©2017 Richard L. Halterman Draft date: January 18, 2018
![](https://csdnimg.cn/release/download_crawler_static/10396025/bg11.jpg)
1.4. WRITING A PYTHON PROGRAM 6
Figure 1.1 WingIDE 101 running under Microsoft Windows
Figure 1.2 The menu selection to create a new Python program.
©2017 Richard L. Halterman Draft date: January 18, 2018
![](https://csdnimg.cn/release/download_crawler_static/10396025/bg12.jpg)
1.4. WRITING A PYTHON PROGRAM 7
Figure 1.3 The new, untitled editor pane ready for code.
Figure 1.4 The code for the simple program after typed into the editor pane.
As Figure 1.3 shows, the file’s name appears in the editor’s tab at the top. (We will save the file with a
different name later.)
We now are ready to type in the code that constitutes the program. Figure 1.4 shows the text to type.
Next we will save the file. The menu sequence File→Save, also shown in Figure 1.5, produces the
dialog box shown in Figure 1.6 that allows us to select a folder and filename for our program. You should
name all Python programs with a .py extension.
The WingIDE-101 IDE provides two different ways to execute the program. We can run the program
by selecting the little green triangle under the menu bar, as shown in Figure 1.7. The pane labeled Python
Shell will display the program’s output. Figure 1.8 shows the results of running the program.
Another way to execute the program is via the Debug button on the menu, as shown in Figure 1.9.
When debugging the program, the executing program’s output appears in the Debug I/O pane as shown in
Figure 1.10.
Which the better choice, the Run option or the Debug option? As we will see later (see Section 5.7), the
debugging option provides developers more control over the program’s execution, so, during development,
Figure 1.5 Save the Python program
©2017 Richard L. Halterman Draft date: January 18, 2018
![](https://csdnimg.cn/release/download_crawler_static/10396025/bg13.jpg)
1.4. WRITING A PYTHON PROGRAM 8
Figure 1.6 The file save dialog allows the user to name the Python file and locate the file in a particular
folder.
Figure 1.7 Running the program
Figure 1.8 WingIDE 101 running under Microsoft Windows
Figure 1.9 Debugging the program
Figure 1.10 Debugger output
©2017 Richard L. Halterman Draft date: January 18, 2018
![](https://csdnimg.cn/release/download_crawler_static/10396025/bg14.jpg)
1.5. THE PYTHON INTERACTIVE SHELL 9
Figure 1.11 WingIDE 101 running under Microsoft Windows
we prefer the Debug option to the Run option.
When you are finished programming and wish to quit the IDE, follow the menu sequence File→Quit as
shown in Figure 1.11.
Listing 1.1 (simple.py) contains only one line of code:
print("This is a simple Python program")
This is a Python statement. A statement is a command that the interpreter executes. This statement
prints the message This is a simple Python program on the screen. A statement is the fundamental unit of
execution in a Python program. Statements may be grouped into larger chunks called blocks, and blocks can
make up more complex statements. Higher-order constructs such as functions and methods are composed
of blocks. The statement
print("This is a simple Python program")
makes use of a built in function named print. Python has a variety of different kinds of statements that we
can use to build programs, and the chapters that follow explore these various kinds of statements.
While integrated development environments like Wingware’s WingIDE-101 are useful for developing
Python programs, we can execute Python programs directly from a command line. In Microsoft Windows,
the command console (cmd.exe) and PowerShell offer command lines. In Apple Mac OS X, Linux, and
Unix, a terminal provides a command line. Figure 1.12 shows the Windows command shell running a
Python program. In all cases the user’s PATH environment variable must be set properly in order for the
operating system to find the Python interpreter to run the program.
Figure 1.8 shows that WingIDE 101 displays a program’s output as black text on a white background. In
order to better distinguish visually in this text program source code from program output, we will render the
program’s output with white text on a black background, as it would appear in the command line interpreter
under Windows as shown in Figure 1.12. This means we would show the output of Listing 1.1 (simple.py)
as
This is a simple Python program
1.5 The Python Interactive Shell
We created the program in Listing 1.1 (simple.py) and submitted it to the Python interpreter for execution.
We can interact with the interpreter directly, typing in Python statements and expressions for its immediate
execution. As we saw in Figure 1.8, the WingIDE 101 pane labeled Python Shell is where the executing
program directs its output. We also can type commands into the Python Shell pane, and the interpreter
©2017 Richard L. Halterman Draft date: January 18, 2018
剩余668页未读,继续阅读
![](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)
jyszsz
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的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)