初学者最困惑的问题

课间思考 2020-9-8

1. Python 是什么类型的语言?

Python是脚本语言

脚本语言(Scripting language)是电脑编程语言,因此也能让开发者藉以编写出让电脑听命行事的程序。以简单的方式快速完成某些复杂的事情,通常是创造脚本语言的重要原则,基于这项原则,使得脚本语言通常比 C语言、C++语言 或 Java 之类的系统编程语言要简单容易。

也让脚本语言另有一些属于脚本语言的特性:

(1)语法和结构通常比较简单

(2)学习和使用通常比较简单

(3)通常以容易修改程序的“解释”作为运行方式,而不需要“编译”

(4)程序的开发产能优于运行性能

一个脚本可以使得本来要用键盘进行的相互式操作自动化。一个Shell脚本主要由原本需要在命令行输入的命令组成,或在一个文本编辑器中,用户可以使用脚本来把一些常用的操作组合成一组串行。主要用来书写这种脚本的语言叫做脚本语言。很多脚本语言实际上已经超过简单的用户命令串行的指令,还可以编写更复杂的程序。

2. IDLE 是什么?

IDLE是一个Python Shell,shell的意思就是“外壳”,基本上来说,就是一个通过键入文本与程序交互的途径!像我们Windows那个cmd窗口,像Linux那个黑乎乎的命令窗口,他们都是shell,利用他们,我们就可以给操作系统下达命令。同样的,我们可以利用IDLE这个shell与Python进行互动。

3. print() 的作用是什么?

print() 会在输出窗口中显示一些文本(在这一讲中,输出窗口就是IDLE shell窗口)。

4. Python 中表示乘法的符号是什么?

Python中的乘号是*(星号)。

5. 为什么 >>>print('I love fishc.com ' * 5) 可以正常执行,但 >>>print('I love fishc.com ' + 5) 却报错?

在 Python 中不能把两个完全不同的东西加在一起,比如说数字和文本,正是这个原因,>>>print('I love fishc.com ' + 5) 才会报错。这就像是在说“五只小甲鱼加上苍井空会是多少?”一样没有多大意义,结果可能是五,可能是六,也可能是八!不过乘以一个整数来翻倍就具有一定的意义了,前边的例子就是将 "I love fishc.com" 这个字符串打印五次。

6. 如果我需要在一个字符串中嵌入一个双引号,正确的做法是?

你有两个选择:可以利用反斜杠(\)对双引号转义:\"。例如print('I\'m Python'),会打印I'm Python。或者用单引号引起这个字符串。例如:print('I love “China”'),会打印 I love “China”。如果字符串中有单引号,就用双引号把字符串引起来,例如print("I'm Python"),会打印I'm Python。

7. 为什么我们要使用 Python3?Python2到底有什么问题?看起来很多程序员依然都在使用Python2?

确实还有相当多的程序员在使用 Python2,不过 Python3 才是 Python 发展的未来,就像 XP 和 WIN7 一样。如果你了解了 Python3,Python2 的代码阅读对于你来说根本不成问题!

8. 动手试试直接输入>>>5+8 与输入>>>print(5+8) 有何不同?

不妨试试直接直接输入 >>>'I love fishc.com!' 与输入 >>>print('I love fishc.com!') 有何不同? 没错,直接输入是将结果及类型打印到屏幕上,而print是将结果打印到屏幕上,自己试试并观察结果!

>>> 'I love fishc.com!'

'I love fishc.com!'

>>> print('I love fishc.com')

I love fishc.com

9. 在交互模式中,使用 Python 计算一年有多少秒?

print( 365 * 24 * 60 * 60 )
# 31536000

10. 设置你的操作系统的环境变量,以便可以轻松进入 Python 环境:

以Window7为例:

 

11. 什么是BIF?如何显示BIF和获取帮助?

BIF 就是 Built-in Functions,内置函数。为了方便程序员快速编写脚本程序,Python 提供了非常丰富的内置函数,我们只需要直接调用即可,例如 print() 的功能是“打印到屏幕”,input() 的作用是接收用户输入。

在 Python 或 IDLE 中,输入 dir(__builtins__) 可以看到 Python 提供的内置方法列表(注意,builtins 前后是两个下划线哦)。其中小写的就是 BIF。如果想具体查看某个 BIF 的功能,比如 input(),可以在 shell 中输入 help(input),就会得到这个 BIF 的功能描述。

print( dir(__builtins__) )
'''
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
'''
print(help(input))
'''
Help on built-in function input in module builtins:

input(prompt=None, /)
    Read a string from standard input.  The trailing newline is stripped.

    The prompt string, if given, is printed to standard output without a
    trailing newline before reading input.

    If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
    On *nix systems, readline is used if available.

'''