python 之青铜 :输入、输出、内建函数、数据类型

阅读 36

07-28 12:00

我们先从概念上简单的了解一下这几个词的意思,后面也还会讲到

什么是输入

  • 输入是相对而言的,我们把一支笔放进文具盒内,对文具盒而言就是把一支笔输入到文具盒,而程序而言,你把外部设备上的数据导入到程序内,这就是输入,输入不一定是键盘和鼠标,键盘和鼠标只是我们常用的一些输入设备。

什么是输出

  • 输出也是相对而言的,我们从文具盒拿出一支笔,这支笔可以理解为是从文具盒输出的。而程序而言只有输入没有输出是没有任何意义的,屏幕只是显示输出的一种设备,它只是把输出的信息显示到屏幕上。

什么是内建函数

  • 内建函数可以理解为:一些数学的已经被公认的公式,别人已经写好了,你只需要拿过来把值带进去运行就好

什么是数据类型

  • 数据类型用通俗一点的话就是种类,如这个时间上有:人、植物、动物 。。。

第一个最著名的输出程序,没错,就是它,第一次机器是这个世界产生了交互,你已经踏上了走上大牛的第一步,大牛也是一步一步蜕变的:

print(" hello world")

第一个输入程序

i = input("请输入:")
    print(type(i))
    # 或者
    print(type(input("请输入:")))
    
    # 请输入:1
    # <class 'str'>
    # 在终端输入的数据程序接收到的是字符串,如果我们要使用数字,就是使用转换的内建函数

查看内建函数

print(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', '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']

常用内建函数

# ord() 可以把字符转换成数字表示
#def ord(__c: str | bytes) -> int:
print(ord('A')) # 65 
print(ord('中')) # 20013

chr()把数字转换成字符
#def chr(__i: int) -> str:
print(chr(66)) # B
print(chr(20013)) # 中

len("ABC") # 3
len(b'ABC') # 3
len('中文') # 2
len('中文'.encode('utf-8')) #6

  • 1个中文字符经过UTF-8编码后通常会占用3个字节,而1个英文字符只占用1个字节。

在操作字符串时,我们经常遇到strbytes的互相转换。为了避免乱码问题,应当始终坚持使用UTF-8编码对strbytes进行转换

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

第一行注释是为了告诉Linux/OS X系统,这是一个Python可执行程序,Windows系统会忽略这个注释;

第二行注释是为了告诉Python解释器,按照UTF-8编码读取源代码,否则,你在源代码中写的中文输出可能会有乱码。

申明了UTF-8编码并不意味着你的.py文件就是UTF-8编码的,必须并且要确保文本编辑器正在使用UTF-8

精彩评论(0)

0 0 举报