• 1.0 获取帮助
  • 1.1 查看报错信息
  • 1.2 两种基本结构


如前一节说到的,这里作为学习记录,一定会有不少弯路,比如我什么都不会的情况下查个帮助还能出错……

1.0 获取帮助

  刚刚装好Python非常不适应,基本什么都不懂,鉴于从minecraft指令到MATLAB命令行都是一个help打天下,所以打算借助刚刚下载的帮助文件(见上节)试着在Python命令行里敲下help看一下结果(这也是我每次打开那个python.exe第一眼就看到的提示: Type “help”, “copyright”, “credits” or “license” for more information.).继续依照提示输入help()获得交互式帮助信息(Python的交互做的真的有点差?)如下

>>>help()
>Welcome to Python 3.7's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help>

  这里继续按照提示输入symbols:

help>symbols

Here is a list of the punctuation symbols which Python assigns special meaning
to. Enter any symbol to get more help.
!=                  +                   <=                  __
"                   +=                  <>                  `
"""                 ,                   ==                  b"
%                   -                   >                   b'
%=                  -=                  >=                  f"
&                   .                   >>                  f'
&=                  ...                 >>=                 j
'                   /                   @                   r"
'''                 //                  J                   r'
(                   //=                 [                   u"
)                   /=                  \                   u'
*                   :                   ]                   |
**                  <                   ^                   |=
**=                 <<                  ^=                  ~
*=                  <<=                 _

  可以看到Python在此罗列了其基本运算符,参照其它语言,大部分的符号都可以被理解,不理解也无所谓,这里主要目的是了解一下Python的帮助系统,可以看到,它的帮助系统给人的第一印象并不友好.

1.1 查看报错信息

  上一节在Python官网上看到的函数定义中已经涉及到一个基本的结构了(while为关键字的循环),这里为了进一步了解Python的帮助系统,我们先尝试输入help(for)(因为大多数语言也有for循环),结果出现如下报错:

>>> help(for)
  File "<stdin>", line 1
    help(for)
           ^
SyntaxError: invalid syntax

可以顺便看到Python的报错信息:(输入行不算)

  • 第一行显示了出错的文件位置(?)
  • 第二三行标注了具体出错的位置
  • 最后一行标注了错误类型

这其实是一个新手很常犯的错误: 大多数语言中没有任何引号包含的单词要么是变量或者函数名,要么是关键字.那么,help作为一个Python函数,首先不能接受关键字作为其输入,而这里如果把for这三个字母看成是一个变量的名字那么这个变量又没有被事先声明.

  推测Python中的help函数应该接受一个字符串作为输入—而for这三个字母虽然是字符串的内容却不是字符串的名字(字符串的变量名和内容之间的关系就好比,字符串的变量名可以是’我的csdn博客名’,字符串的内容对应的是’weisuowangshuai’),多数语言中的字符串应该用引号包括起来,这里我们重新尝试,这次输入help('for'),这一次得到正常的输出如下:

>>> help('for')
The "for" statement
*******************

The "for" statement is used to iterate over the elements of a sequence
(such as a string, tuple or list) or other iterable object:

   for_stmt ::= "for" target_list "in" expression_list ":" suite
                ["else" ":" suite]

The expression list is evaluated once; it should yield an iterable
object.  An iterator is created for the result of the
"expression_list".  The suite is then executed once for each item
provided by the iterator, in the order returned by the iterator.  Each
item in turn is assigned to the target list using the standard rules
for assignments (see Assignment statements), and then the suite is
executed.  When the items are exhausted (which is immediately when the
sequence is empty or an iterator raises a "StopIteration" exception),
the suite in the "else" clause, if present, is executed, and the loop
terminates.

A "break" statement executed in the first suite terminates the loop
without executing the "else" clause’s suite.  A "continue" statement
executed in the first suite skips the rest of the suite and continues
with the next item, or with the "else" clause if there is no next
item.

The for-loop makes assignments to the variables(s) in the target list.
This overwrites all previous assignments to those variables including
-- More  --

之后不断敲回车整个帮助文档内容就可以被展示出来了,这里不再演示了.

1.2 两种基本结构

  依次尝试help各种其它语言的关键字,可以得到Python中可用的结构包括:

  • 分支结构 (以if,else等为关键字),基本语法为:
# 注意奇葩的缩进规则和该死的冒号
if 判据:
    判据成立时被选择执行的语句
else:
    判据不成立时被选择执行的语句
#end of if

   有趣的是Python似乎没有提供switch选择分支,不过考虑到所有的分支其实都可以转换成二分支(嵌套),这个问题似乎也无伤大雅(?)

  • 循环结构(以for或者while等为关键字),基本语法为:
while 判据:
    判据表达为真时反复执行的语句
#end of while

for 循环变量 in 变量取值范围:
    循环变量在变量取值范围内时执行的语句
#end of for

   while循环早在前一节就已经接触过,这里比较好奇的是for循环结构中,那个变量取值范围是怎样确定的,难道需要把每个取值都写出来吗?事实是如下语句确实是可行的:

#这居然真的可以...
for a in 1,2,3,4,5,6,7:
    print(a)

   问题在于如何用一种省力的方式产生诸如1到7这种自然数列,这里就要用到range函数了(查到的,其实我试过MATLAB里边的写法1:1:7,结果当然报错,估计是一堆冒号给Python弄蒙了?…),该函数能够产生一个等差数列,其最长调用方式为:

range(数列首项,数列末项限制(不含边界),公差)

例如前文所述的循环可以写成如下形式:

for a in range(1,8,1):
    print(a)