python意外缩进引发逻辑错误_《A Byte of Python》阅读记录—变量赋值、物理行、逻辑行和缩进...

本文介绍了Python编程的基础知识,包括变量与常量的使用、代码编写规范、逻辑行与物理行的区别及缩进的重要性。

通过前面关于Python部分基础知识的了解,我们尝试着开始Python程序的编写。在编写的过程中还会有更多的概念或者规则需要学习,那么我们更多的会在一个个小的Python程序里面去使用并熟悉这些相关的功能。

从今以后,我们编写、保存和运行Python程序的标准步骤如下:

对于pycharm用户

打开pycharm。首先我们先建立一个名为A Byte of Python的项目(project),以后学习的案例程序都在这个项目里面去创建新的文件。以给定的文件名创建新文件。输入案例中给出的代码或者编写自己的代码。运行当前文件。接下来,先以一个案例来熟悉如何使用变量与常量。我们创建一个var.py的Python程序,输入一下代码:

i = 5print(i)i = i + 1print(i)s = '''This is a multi-line string.This is the second line.'''print(s)

最后运行得到如下结果:

我们来分析一下这个案例是如何工作的呢?

首先,我们使用赋值运算符(=)将字面常量数值5赋值给变量i。这一行被称之为声明语句(statement),因为其作用正是声明一些在这种情况下应该完成的事情:我们将变量名i与值5相连接。然后,我们通过print语句来打印变量i所声明的内容,就可以看到运行结果5。

接下来,我们将数值1加到变量i所存储的值5中,并将得到的结果再次赋值给变量i。这个在很多程序设计语言里面都会学到的,我们可以简单的把这个操作称之为自加1.然后,又一次通过print语句打印变量i的内容,这次得到的结果为6。

最后,我们把一串多行的字符串赋值给变量s,这里我们使用了三引号。然后利用print语句输出了变量s。

通过这个简单的程序,我们可以看出来,Python的变量只需被赋予某一值,而并不需要声明或者定义数据类型。

这里提出一个关于逻辑行与物理行的概念。所谓物理行(physical line )是我们在编写程序的时候我们自己看到的内容。而逻辑行(logical line)则是Python所看到的单个语句。一般来说Python会假定每一物理行会对应一个逻辑行。如果你希望在一行物理行中指定多行逻辑行,那么我们必须通过使用(;)来明确表明逻辑行或语句的结束。例如:

I = 5

Print(i)

实际上等同于

i = 5;print(i);

然而,我们从来不会这样去写。因为每一行物理行对应一行逻辑行是最方便我们去理解程序本义的。除非你有一行非常长的代码,一行写不完或者写出来很不好看,那么我们可以使用反斜杠(\)将其拆分为多个物理行。这被称作显式行连接(explicit line joining):

s = ‘This is a string. \

This continues the string. ’

Print (s)

那么运行后将输出:This is a string. This continues the string.

但是还有一种情况,就是逻辑行以括号开始,它可以是方括号或花括号,但不能是结束括号。在这种情况下,我们可以不使用反斜杠。这被称作隐式行连接。

另外提一个非常重要的概念:缩进。实际上,空白区在Python程序中各行的开头非常重要。我们把它称作缩进(indentation)。在逻辑行的开头留下空白区(使用空格或制表符)用以确定各逻辑行的缩进级别,并且可用于确定语句的分组。

这就意味着在同一级别的语句必须拥有相同的缩进。每一组重要的语句被称为块(block)。在后续章节中会特别的让我们了解到缩进是多么的重要。

对应的,我们需要记住:错误的缩进可能会导致错误。虽然pycharm会给我们提供自动缩进,但自动缩进以后,当我们需要跳出那一级别的语句的时候,我们就得把缩进改回去,尤其是多级缩进的时候要特别的注意一下。

例如这个案例:

i = 4print('value is ',i)print(' I repeat,the value is',i)

通过运行我们发现,程序报错,错误理由为:意外缩进(IndentationError: unexpected indent)。这里我们看到了第二行前面有一个空格。Python就告诉我们这个程序的语法是无效的。同时Python语言官方建议我们的缩进是四个空格来代表依次缩进。好的编辑器会自动为我们完成这个工作。但是假如我们临时在工作中需要编写一个简单的Python程序又没有一个编辑器的时候,我们可以使用诸如记事本之类的去编写一个Python程序。在这种情况下,我们就必须特别注意缩进格式。并确保在缩进中使用了数量一致的空格,比如一个语句块中前一个是四个空格,后一个是三个空格,我们的程序将不会被执行或者产生一些不是我们希望的结果。

今天的学习内容对我来说很有用,知其然而知其所以然,正是我希望学到的东西。在此希望每一天都能够与大家共同进步一点点。加油!!!

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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值