Fork me on GitHub

Python简介及第一个程序

一、应用场景

  • Web开发(基础语法学完后,可以学习django、flask、tornado、fastapi等web框架)
  • 数据分析
  • 爬虫(requests、scrapy等手段)
  • AI(机器学习、深度学习等知识)
  • ...

二、语言特点

面向对象、解释性语言

  • 学习起来比较容易上手(语法简洁、没有特别复杂的概念)
  • 标准库和第三方库特别多,做项目可以节约更多时间(有现成的轮子用)

三、代码运行方式

  • 终端解释器中写入代码运行
  • 执行py文件(启动Python解释器,一次性将源代码给解释器执行,无法进行交互式运行)

四、编译型和解释型区别

举个例子:

package main

import "fmt"

func main() {

	fmt.Println("hello world!")

}

可以使用go run main.go执行,可以看出输出hello world!,但是假如我们先执行go build main.go的话会生成一个二进制文件main.exe,然后直接执行main.exe,可以看到编译型语言执行的速度更快。因为它执行的就是二进制文件。

Python语言的解释型可以理解相当于go run main.go,显然会慢一些。

那么为什么还要学习Python呢?

每一门语言都有其优劣之处,Python语言适合数据分、人工智能,丰富的第三方包,我们完全可以发挥出其优势。所以需要好好学习。

四、输入输出内置函数

  • input
>>> 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.
  • print
>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

通过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']
>>>

五、第一个Python程序

print("Hello World!")
posted @ 2023-01-13 12:37  iveBoy  阅读(12)  评论(0)    收藏  举报
TOP