小甲鱼Python学习知识点记录(002讲)

一、建立一个Python文件

第一步:打开Idle

第二步:左上角File--->New File(或直接快捷键Ctrl+N)

第三步:在新打开的文本框中敲入代码

第四步:左上角File--->Save(或直接快捷键Ctrl+S)

第五步:上方Run--->Run Module(或直接快捷键F5)

二、主要知识点

1.本节实验代码:

print('——————————我爱鱼C工作室————————')
temp=input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
guess=int(temp)
if guess==8:
    print("妈耶, 你是小甲鱼心里的蛔虫吗?!")
    print("哼,猜中了也没奖励!")
else:
    print("猜错啦,小甲鱼现在心里想的是8!")
print("游戏结束,不玩啦")

2.代码解析

■此处的变量temp并没有变量声明。

■没有像C语言的大括号什么的,在Python中,缩进就相当于C中的大括号,缩进弄错就相当于C语言中的大括号用错,比如若把上面的代码改成:

print('——————————我爱鱼C工作室————————')
temp=input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
guess=int(temp)
if guess==8:
    print("妈耶, 你是小甲鱼心里的蛔虫吗?!")
print("哼,猜中了也没奖励!")      #此句缩进错误
else:
    print("猜错啦,小甲鱼现在心里想的是8!")
print("游戏结束,不玩啦")

显示:invalid syntax

■可以发现代码中的if和else后面都有冒号,一方面这是Python的语法要求(我刚刚试了下,去掉冒号的话就是错的语法);另一方面在if和else后面加上冒号之后,再下面一行直接缩进一个tab(四个空格)。

■代码第二行的=是赋值,if后面的==是判断是否相等。

■第2、3行就是两条赋值语句,第三行涉及的字符型转整型和内置函数input的用法有关,我还没弄懂,就不瞎写了。

■画出程序的流程图(以后写代码都可以画出程序简单的流程图,帮助很大)

■BIF=built-in functions,内置函数,python里面有许多内置函数,我们可以通过在IDLE中键入dir(__builtins__)来查看到底到多少内置函数:

>>>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']
#并非全部都是BIF,纯小写的这些是BIF,如input,help等。

■我们可以借助内置函数help了解内置函数的功能

>>>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.

>>>help(int)

Squeezed text(241 lines)--->右击--->view

#本例中,>>>help(int())可得到同样的结果,但在上例中则不行,我暂时还没弄懂为什么。

三、课后题知识记录

0.什么是BIF?

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

注:Python3用input()替代了Python2的raw_input()。

1.数一数Python3提供了多少个内置函数?

答:在Python或IDLE中输入dir(__builtins__)就可得到Python提供的内置函数列表,其中纯小写的为内置函数,共68个,可利用help()查看其功能。

2.在Python看来,"fishc"和"Fishc"一样吗?

答:不一样,Python是十分敏感的,"Fishc"和"fishc"对Python来说是完全不同的两个名字,所以编程的时候一定要当心。不过Python会帮助解决可能因此出现的问题,如只有当标识符已经赋值后(Python的变量是不用先声明的)才可在代码中运行,未赋值的标识符直接使用会导致运行错误,很快我们就可根据经验发现问题。

3.在小甲鱼看来,Python中什么是最重要的?你赞同吗?

答:缩进!在小甲鱼看来,缩进是Python的灵魂,缩进的严格要求使Python的代码十分精简而有层次。

所以对待缩进要十分小心,像C语言中对待括号的位置一样小心。

在正确的位置输入冒号,IDLE会自动在下一行缩进。

4.在本节的例子中出现了=和==,它们的含义不同,你在编程中会把它们俩写错吗?有没有什么好方法可以避免这样的错误呢?

答:在C语言中,如果if(c==1)写成了if(c=1),程序就不会按照程序员的目标去执行,但是在Python中不允许if条件中赋值,即if c=1会报错。

5.你听说过“拼接”这个词吗?

答:在一些编程语言中,我们可以把一些字符串“加”起来,如'I'+'love'+'you'得到'Iloveyou',在Python中这叫做拼接字符串。

6.编写程序:hello.py,请用户输入姓名并打印“你好,姓名!”

name=input('请输入您的姓名:')
print('你好'+name+'!')

7.编写程序:calcu.py,要求用户输入1至100的数字,符合要求则打印“你妹好漂亮”,否则打印“你大爷好丑”。

temp=input("请输入1至100的数字:")
num=int(temp)
if 1<=num<=100:
    print("你妹好漂亮(〃'▽'〃)")
else:
    print("你大爷好丑(▼ヘ▼#)")

猜你喜欢

转载自blog.csdn.net/weixin_41587118/article/details/86685527