python学习001-内置函数

【注】该python课程笔记来源于B站鱼C-小甲鱼

ctrl+n 新建窗口
file - new window

tab键的使用:缩进,提示
ctrl+s 保存

F5 run - run model 运行

变量不用赋值
没有大括号,只有缩进:严格要求
if:自动缩进

print('--------------我爱鱼C工作室--------------')
temp = input("猜一下小甲鱼心里想的是哪个数字:")
#返回一个输入值给temp
#temp:字符串变量
guess = int(temp)
#转化为整型
if guess == 8:
    print("你是小甲鱼心里的蛔虫吗?!")
    print("哼")
else:
    print("猜错啦")
print("不玩啦")

BIF == Built-in function 内置函数

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

测试题

0. 什么是BIF?

BIF 就是 Built-in Functions,内置函数。为了方便程序员快速编写脚本程序(脚本就是要编程速度快快快!!!),Python 提供了非常丰富的内置函数,我们只需要直接调用即可,例如 print() 的功能是“打印到屏幕”,input() 的作用是接收用户输入(注:Python3 用 input() 取代了 Python2 的 raw_input(),用法如有不懂请看视频讲解)。


1. 用课堂上小甲鱼教的方法数一数 Python3 提供了多少个 BIF?

在 Python 或 IDLE 中,输入 dir(__builtins__) 可以看到 Python 提供的内置方法列表(注意,builtins 前后是两个下划线哦)其中小写的就是 BIF。如果想具体查看某个 BIF 的功能,比如 input(),可以在 shell 中输入 help(input),就会得到这个 BIF 的功能描述。哦,答案应该是 68 个,不信你自己数数看,你们肯定没有自己完成作业就来看答案!


2. 在 Python 看来:'FishC' 和 'fishc' 一样吗?

不一样,因为 Python 是一个“敏感的小女孩”,所以不要试图欺骗她,对 Python 来说,fishc 和 FishC 是完全不同的两个名字,所以编程的时候一定要当心。不过 Python 会帮助解决可能因此出现的问题,例如只有当标识符已经赋值后(还记得吗,小甲鱼在课堂中说过 Python 的变量是不用先声明的)才能在代码中使用,未赋值的标识符直接使用会导致运行时错误,所以你很快就可以根据经验发现此问题。


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

缩进!在小甲鱼看来,缩进是 Python 的灵魂,缩进的严格要求使得 Python 的代码显得非常精简并且有层次(小甲鱼阅读过很多大牛的代码,那个乱......C语言不是有国际乱码大赛嘛......)。

所以在 Python 里对待缩进代码要十分小心,如果没有正确地缩进,代码所做的事情可能和你的期望相去甚远(就像C语言里边括号打错了位置)。

如果在正确的位置输入冒号“:”,IDLE 会自动将下一行缩进!


4. 这节课的例子中出现了“=”和“==”,他们表示不同的含义,你在编程的过程中会不小心把“==”误写成“=”吗?有没有好的办法可以解决这个问题呢?


C语言的话,如果 if( c == 1 ) 写成 if( c = 1 ),程序就完全不按程序员原本的目的去执行,但在 Python 这里,不好意思,行不通,语法错误!Python 不允许 if 条件中赋值,所以 if c = 1: 会报错!

小甲鱼觉得这种做法可以非常有效的防止程序员因为打炮、熬夜等问题导致粗心的 BUG ,并且这类 BUG 杀伤力都巨大!


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

在一些编程语言,我们可以将两个字符串“相加”在一起,如:'I' + 'Love' + 'FishC' 会得到 'ILoveFishC',在 Python 里,这种做法叫做拼接字符串。

动动手:

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

temp = input("请输入您的姓名:")
print("你好,"+temp)

1.编写程序:calc.py 要求用户输入1到100之间数字并判断,输入符合要求打印“你好漂亮”,不符合要求则打印“你好丑”

temp = input("请输入1到100之间的数字:")
guess = int(temp)
if guess >= 0 & guess <= 100:
    print("你好漂亮")
else:
    print("你好丑")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值