Python 3.6.0入门教程:领略强大与简洁

需积分: 15 5 下载量 163 浏览量 更新于2024-07-18 收藏 5.01MB PDF 举报
Python 3.6.0入门指南提供了一个全面的起点,旨在让初学者快速理解和掌握这门流行的编程语言。Python以其简洁的语法和动态类型系统著称,使得它在脚本编写和应用程序开发中具有很高的效率。作为一门面向对象的语言,Python支持高效的数据结构,如列表、元组和字典,这些是实现复杂逻辑的基础。 在学习过程中,用户可以从官方Python网站获取最新版本的解释器和标准库,以及丰富的第三方模块和资源。Python的可扩展性体现在能够通过C或C++等语言编写扩展函数和数据类型,将其作为定制应用的补充。 本指南的核心内容包括: 1. **开胃菜**:引导读者熟悉Python的基本环境,如如何调用解释器、参数传递方式以及交互模式。这里强调了源程序编码的重要性,为后续学习奠定基础。 2. **使用Python解释器**: - **调用Python解释器**:介绍了命令行中的基本操作,如启动解释器、运行脚本和处理输入输出。 - **交互模式**:提供实时的编程环境,便于测试和调试代码。 3. **Python简介**: - **作为计算器**:展示Python处理数字、字符串和列表的基本运算和操作。 - **编程第一步**:教授基础语法,如变量定义、数据类型和控制结构的初步运用。 4. **深入Python流程控制**: - **if语句**:讲解条件判断的基础。 - **for语句**:介绍循环结构,用于重复执行一段代码。 - **range()函数**:演示序列迭代。 - **break和continue语句**:理解如何控制循环的流程。 本指南的目标并非详尽无遗,而是着重于突出Python的关键特性,以便读者能够迅速上手并具备进一步探索和学习的标准。对于更深入的特性、标准库和扩展开发,指南建议读者查阅Python参考文档、C API手册以及专业书籍。 Python 3.6.0入门指南是一份实用的教程,适合那些希望入门编程,尤其是对脚本编写和快速原型设计感兴趣的读者。通过跟随指南中的实例,学习者能快速掌握Python的核心概念,为进一步提升技能打下坚实基础。
2018-11-29 上传
如果你要用计算机做很多工作,最后你会发现有一些任务你更希望用自动化的方式进行处理。比如,你想要在大量的文本文件中 执行查找 / 替换,或者以复杂的方式对大量的图片进行重命名和整理。也许你想要编写一个小型的自定义数据库、一个特殊的 GUI 应用程序或一个简单的小游戏。 如果你是一名专业的软件开发者,可能你必须使用几种 C/C++/JAVA 类库,并且发现通常编写 / 编译 / 测试 / 重新编译的周期是如此漫 长。也许你正在为这些类库编写测试用例,但是发现这是一个让人烦躁的工作。又或者你已经完成了一个可以使用扩展语言的程 序,但你并不想为此重新设计并实现一套全新的语言。 那么 Python 正是你所需要的语言。 虽然你能够通过编写 Unix shell 脚本或 Windows 批处理文件来处理其中的某些任务,但 Shell 脚本更适合移动文件或修改文本数据, 并不适合编写 GUI 应用程序或游戏;虽然你能够使用 C/C++/JAVA 编写程序,但即使编写一个简单的 first-draft 程序也有可能耗费 大量的开发时间。相比之下, Python 更易于使用,无论在 Windows 、 Mac OS X 或 Unix 操作系统上它都会帮助你更快地完成任务。 虽然 Python 易于使用,但它却是一门完整的编程语言;与 Shell 脚本或批处理文件相比,它为编写大型程序提供了更多的结构和支 持。另一方面, Python 提供了比 C 更多的错误检查,并且作为一门 高级语言 ,它内置支持高级的数据结构类型,例如:灵活的数 组和字典。因其更多的通用数据类型, Python 比 Awk 甚至 Perl 都适用于更多问题领域,至少大多数事情在 Python 中与其他语言同 样简单。 Python 允许你将程序分割为不同的模块,以便在其他的 Python 程序中重用。 Python 内置提供了大量的标准模块,你可以将其用作 程序的基础,或者作为学习 Python 编程的示例。这些模块提供了诸如文件 I/O 、系统调用、 Socket 支持,甚至类似 Tk 的用户图形 界面( GUI )工具包接口。 Python 是一门解释型语言,因为无需编译和链接,你可以在程序开发中节省宝贵的时间。 Python 解释器可以交互的使用,这使得试 验语言的特性、编写临时程序或在自底向上的程序开发中测试方法非常容易。你甚至还可以把它当做一个桌面计算器。 Python 让程序编写的紧凑和可读。用 Python 编写的程序通常比同样的 C 、 C++ 或 Java 程序更短小,这是因为以下几个原因
2018-06-05 上传
Index The Python Tutorial 1 1. Whetting Your Appetite 4 2. Using the Python Interpreter 6 2.1. Invoking the Interpreter 7 2.1.1. Argument Passing 8 2.1.2. Interactive Mode 8 2.2. The Interpreter and Its Environment 9 2.2.1. Source Code Encoding 9 3. An Informal Introduction to Python 10 3.1. Using Python as a Calculator 11 3.1.1. Numbers 11 3.1.2. Strings 13 3.1.3. Lists 19 3.2. First Steps Towards Programming 21 4. More Control Flow Tools 24 4.1. if Statements 24 4.2. for Statements 24 4.3. The range() Function 25 4.4. break and continue Statements, and else Clauses on Loops 27 4.5. pass Statements 28 4.6. Defining Functions 29 4.7. More on Defining Functions 31 4.7.1. Default Argument Values 31 4.7.2. Keyword Arguments 33 4.7.3. Arbitrary Argument Lists 35 4.7.4. Unpacking Argument Lists 36 4.7.5. Lambda Expressions 37 4.7.6. Documentation Strings 38 4.7.7. Function Annotations 39 4.8. Intermezzo: Coding Style 39 5. Data Structures 40 5.1. More on Lists 41 5.1.1. Using Lists as Stacks 43 5.1.2. Using Lists as Queues 43 5.1.3. List Comprehensions 44 5.1.4. Nested List Comprehensions 46 5.2. The del statement 47 5.3. Tuples and Sequences 48 5.4. Sets 50 5.5. Dictionaries 51 5.6. Looping Techniques 53 5.7. More on Conditions 55 5.8. Comparing Sequences and Other Types 56 6. Modules 57 6.1. More on Modules 58 6.1.1. Executing modules as scripts 60 6.1.2. The Module Search Path 61 6.1.3. “Compiled” Python files 61 6.2. Standard Modules 62 6.3. The dir() Function 63 6.4. Packages 66 6.4.1. Importing * From a Package 68 6.4.2. Intra-package References 69 6.4.3. Packages in Multiple Directories 70 7. Input and Output 70 7.1. Fancier Output Formatting 70 7.1.1. Old string formatting 75 7.2. Reading and Writing Files 76 7.2.1. Methods of File Objects 77 7.2.2. Saving structured data with json 79 8. Errors and Exceptions 81 8.1. Syntax Errors 81 8.2. Exceptions 81 8.3. Handling Exceptions 82 8.4. Raising Exceptions 86 8.5. User-defined Exceptions 87 8.6. Defining Clean-up Actions 88 8.7. Predefined Clean-up Actions 89 9. Classes 90 9.1. A Word About Names and Objects 91 9.2. Python Scopes and Namespaces 91 9.2.1. Scopes and Namespaces Example 94 9.3. A First Look at Classes 95 9.3.1. Class Definition Syntax 95 9.3.2. Class Objects 96 9.3.3. Instance Objects 97 9.3.4. Method Objects 98 9.3.5. Class and Instance Variables 99 9.4. Random Remarks 100 9.5. Inheritance 102 9.5.1. Multiple Inheritance 104 9.6. Private Variables 105 9.7. Odds and Ends 106 9.8. Iterators 106 9.9. Generators 108 9.10. Generator Expressions 109 10. Brief Tour of the Standard Library 110 10.1. Operating System Interface 110 10.2. File Wildcards 111 10.3. Command Line Arguments 111 10.4. Error Output Redirection and Program Termination 112 10.5. String Pattern Matching 112 10.6. Mathematics 113 10.7. Internet Access 114 10.8. Dates and Times 114 10.9. Data Compression 115 10.10. Performance Measurement 115 10.11. Quality Control 116 10.12. Batteries Included 117 11. Brief Tour of the Standard Library — Part II 117 11.1. Output Formatting 118 11.2. Templating 119 11.3. Working with Binary Data Record Layouts 121 11.4. Multi-threading 121 11.5. Logging 122 11.6. Weak References 123 11.7. Tools for Working with Lists 124 11.8. Decimal Floating Point Arithmetic 126 12. Virtual Environments and Packages 127 12.1. Introduction 127 12.2. Creating Virtual Environments 128 12.3. Managing Packages with pip 129 13. What Now? 131 14. Interactive Input Editing and History Substitution 133 14.1. Tab Completion and History Editing 133 14.2. Alternatives to the Interactive Interpreter 133 15. Floating Point Arithmetic: Issues and Limitations 134 15.1. Representation Error 139 16. Appendix 141 16.1. Interactive Mode 141 16.1.1. Error Handling 141 16.1.2. Executable Python Scripts 142 16.1.3. The Interactive Startup File 142 16.1.4. The Customization Modules 143