A Byte of Python 简明Python教程 读书笔记

本文介绍了Python的基础知识,包括环境搭建、基本语法、数据类型、流程控制、函数与类等内容,并提供了实用的操作指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工作需要用到Python,用java实现后台的数据处理分发

把《A Byte of Python》   简明Python教程里面的示范在Python里面运行一遍,做点笔记:

Python环境可以从 https://www.python.org/downloads/  下载

1. 在命令行输入python3运行,会出现解释器提示符(Python Interpreter Prompt): >>> 

2.退出解释器提示符:  ctl+d   或者 输入 exit() 

3.如果你需要获得 Python 中有关任何函数或语句的快速信息,你可以使用其内置的  help  功
能。例如,运行  help('len')  命令——这将显示出有关len  函数的帮助

4.双引号包括的字符串和被单引号括起的字符串其工作机制完全相同:   'abcd'  "abcd",效果是一样的

5.你可以通过使用三个引号—— """  或  '''  来指定多行字符串。你可以在三引号之间自由地
使用单引号与双引号


6. DocStrings

可以通过使用函数的  __doc__  (注意其中的双下划綫)属性(属于函数的名称)来获取
函数  的文档字符串属性

print(函数名.__doc__),用来获取函数里面  '''  ''' 或者 """  """之间的内容

7.按字节码编译的(Byte-Compiled)文件,这一文件以  .pyc  为其扩展

8.模块的  __name__, 可以找到它们所处的模块的名称

9.内置的  dir()  函数能够返回由对象所定义的名称列表

10.包是指一个包含模块与一个特殊的  __init__.py  文件的文件夹

11.Python 中有四种内置的数据结构——列表(List)、元组(Tuple)、字典(Dictionary)和集
合(Set)

12. for...in  循环来遍历列表中的每一个项目

for item in shoplist:

13. 元组(Tuple)用于将多个对象保存到一起,类似于字符串,它们是不可变的

myempty = ()  空元祖

singleton = (2, ) 包含一个项目的元祖

14.字典

只能使用不可变的对象(如字符串)作为字典的键值,但是你可以使用可变或不可变的对象作为字典中的值

键值与值之间使用冒号分隔,而每一对键值与值则使用逗号进行区分,它们全都由一对花括号括起

d = {key : value1 , key2 : value2}

15.序列

序列的主要功能是资格测试(Membership Test)(也就是  in  与not in  表达式)和索引操作(Indexing Operations),它们能够允许我们直接获取序列中的特定项目,列表、元组和字符串可以看作序列(Sequence)的某种表现形式

下标操作: 列表,元组,字符串名【0---len】

切片操作:  切片起始位置:切片结束位置:切片的步长

创建一份诸如序列等复杂对象的副本(而非整数这种简单的对象(Object)),你必须使用切片操作来制作副本!!!

16.集合(Set)是简单对象的无序集合(Collection)

17.os.sep  变量的使用方式——它将根据你的操作系统给出相应的分隔符,在GNU/Linux 与 Unix 中它会是  '/'  ,在 Windows中它会是  '\\'  ,在 Mac OS 中它会是':' 

 18. 通过  class  关键字可以创建一个类,类的名称后跟一对括号的方法,给这个类创建一个对象

请注意,即使是整数也会被视为对象( int  类的对象)

Python 中的  self  相当于this

如果你有一个没有参数的方法,你依旧必须拥有一个参数—— self 

class 类名:
def 函数名(self):

19.__init__  在init的前后加的双下划线

20. 通过      类名.__doc__       访问类的 文档字符串

21.如果继承元组(Inheritance Tuple)中有超过一个类,  这种情况就会被称作多重继承(Multiple Inheritance)

22.Pickle  标准模块,通过它你可以将任何纯 Python 对象存储到一个文件中,并在稍后将其取回。

将一个对象存储到一个文件中,我们首先需要通过  open  以写入(write)二进制(binary)模式打开文件,然后调用  pickle  模块的  dump  函数。这一过程被称作封装(Pickling);接着,我们通过  pickle  模块的  load  函数接收返回的对象。这个过程被称作拆封(Unpickling);

23. try

try:

except:

except:

else:

finally:

24.

__init__(self, ...)
这一方法在新创建的对象被返回准备使用时被调用

__del__(self)
这一方法在对象被删除之前调用(它的使用时机不可预测,所以避免使用它)

__str__(self)
当我们使用  print  函数时,或  str()  被使用时就会被调用

__lt__(self, other)
当小于运算符(<)被使用时被调用。类似地,使用其它所有运算符(+、> 等等)时都会有特殊方法被调用

__getitem__(self, key)
使用  x[key]  索引操作时会被调用

__len__(self)
当针对序列对象使用内置  len()  函数时会被调用

26.  lambda  语句可以创建一个新的函数对象

lambda语句中,冒号前是参数,可以有多个,用逗号隔开,冒号右边的返回值

27.一个函数和它的环境变量合在一起,就构成了一个闭包(closure)环境变量取值被保存在函数对象的__closure__属性中


this is a book about python. it was written by Swaroop C H.its name is "a byte of python". Table of Contents Preface Who This Book Is For History Lesson Status of the book Official Website License Terms Using the interpreter prompt Choosing an Editor Using a Source File Output How It Works Executable Python programs Getting Help Summary 4. The Basics Literal Constants Numbers Strings Variables Identifier Naming Data Types Objects Output How It Works Logical and Physical Lines Indentation Summary 5. Operators and Expressions Introduction Operators Operator Precedence Order of Evaluation Associativity Expressions Using Expressions Summary 6. Control Flow Introduction The if statement ivUsing the if statement How It Works The while statement Using the while statement The for loop Using the for statement Using the break statement The continue statement Using the continue statement Summary 7. Functions Introduction Defining a Function Function Parameters Using Function Parameters Local Variables Using Local Variables Using the global statement Default Argument Values Using Default Argument Values Keyword Arguments Using Keyword Arguments The return statement Using the literal statement DocStrings Using DocStrings Summary 8. Modules Introduction Using the sys module Byte-compiled .pyc files The from..import statement A module's __name__ Using a module's __name__ Making your own Modules Creating your own Modules from..import The dir() function Using the dir function Summary 9. Data Structures Introduction List Quick introduction to Objects and Classes Using Lists Tuple Using Tuples Tuples and the print statement Dictionary Using Dictionaries Sequences Using Sequences References Objects and References More about Strings String Methods Summary 10. Problem Solving - Writing a Python Script The Problem The Solution First Version Second Version Third Version Fourth Version More Refinements The Software Development Process Summary 11. Object-Oriented Programming Introduction The self Classes Creating a Class object Methods Using Object Methds The __init__ method Using the __init__ method Class and Object Variables Using Class and Object Variables Inheritance Using Inheritance Summary 12. Input/Output Files Using file Pickle Pickling and Unpickling Summary 13. Exceptions Errors Try..Except Handling Exceptions Raising Exceptions How To Raise Exceptions Try..Finally Using Finally Summary 14. The Python Standard Library Introduction The sys module Command Line Arguments More sys The os module Summary 15. More Python Special Methods Single Statement Blocks List Comprehension Using List Comprehensions Receiving Tuples and Lists in Functions Lambda Forms Using Lambda Forms The exec and eval statements The assert statement The repr function Summary 16. What Next? Graphical Software Summary of GUI Tools Explore More Summary A. Free/Libré and Open Source Software (FLOSS) B. About Colophon About the Author C. Revision History Timestamp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值