JM233333's Blog
  • Programming Languages

    • C
    • Python
  • Algorithms and Data Structures

    • Data Structure
    • Fundamental Algorithms
    • Graph Theory
  • GNU Toolchain

    • Bash
    • gdb
  • Development Environment

    • Ubuntu
    • QEMU
  • Development Tools

    • Git
    • VSCode
  • Operating Systems

    • Principles of Operating Systems
    • Xv6
    • Linux Kernel
  • Software Testing and Analysis

    • Software Testing
    • Software Analysis
    • Program Verification
  • LeetCode
  • XJTUOJ
  • System

    • System Performance
  • Programming

    • ...
  • Others

    • ...
  • Paper Reading

    • Model Checking
    • Fuzzing
    • Symbolic Execution
  • 3D Game Programming

    • 3D Mathematics

JM233333

弱小可怜又无助的学术废物
  • Programming Languages

    • C
    • Python
  • Algorithms and Data Structures

    • Data Structure
    • Fundamental Algorithms
    • Graph Theory
  • GNU Toolchain

    • Bash
    • gdb
  • Development Environment

    • Ubuntu
    • QEMU
  • Development Tools

    • Git
    • VSCode
  • Operating Systems

    • Principles of Operating Systems
    • Xv6
    • Linux Kernel
  • Software Testing and Analysis

    • Software Testing
    • Software Analysis
    • Program Verification
  • LeetCode
  • XJTUOJ
  • System

    • System Performance
  • Programming

    • ...
  • Others

    • ...
  • Paper Reading

    • Model Checking
    • Fuzzing
    • Symbolic Execution
  • 3D Game Programming

    • 3D Mathematics
  • c

  • cpp

  • java

  • python

    • Python 00 - Introduction
    • Python 01 - Summary of Basic Syntax (unfinished)
      • 前置知识
      • 前言
      • 控制语句
        • match
      • 函数
        • 函数参数
        • 参数约束规则
        • bababa
        • 文档字符串
        • 函数注解
      • 作用域
    • Python 02 - Data Structures (unfinished)
    • Python 88 - String
    • Python 88 - String Formatting
    • Python 99 - Summary of Piecemeal Knowledge (unfinished)
  • programming-languages
  • python
JM233333
2022-06-01
745
12

Python 01 - Summary of Basic Syntax (unfinished)

Creative Commons

# 前置知识

在阅读本文档前,请先阅读 Python 语言的整体介绍文档,详见 Python 语言的 00 - Introduction 文档。

  • Python 00 - Introduction

# 前言

本文是对 Python 3.x 基础语法知识的汇总,但仅仅是汇总,而不是教程。换句话说,本文是供那些对基础语法还不熟悉的初学者集中查阅用的,但并非面向初学者的教程。因为我们假设读者可以通过其它途径学习 Python 3.x ,这种太基础的教程实在是太多了,真的没有必要再写一遍了。


# 控制语句

# match

Python 的 match 语句类似于 C/C++ 等语言中的 switch 语句:接受一个表达式,并将其值与每个 case 语句给出的表达式进行比较,若匹配则执行对应 case 块中的代码。

有趣的是,这一传统语法直到 Python 3.10 才被引入,不过相比于 C/C++ 的 switch / case 而言,Python 的 match / case 具有更强大的功能:例如它的每个 case 可以匹配多个模式,并且支持将组件(序列元素或对象属性)从值中提取到变量中的功能,等等,详见下文。

https://docs.python.org/3/tutorial/controlflow.html#match-statements

---https://docs.python.org/3/howto/sorting.html#sortinghowto

# 函数

# 函数参数

# 关键字参数

关键字参数 (keyword argument, kwarg)

# 参数约束规则

def f(pos-only args, /, pos-or-kwd args, *, kwd-only args):

https://docs.python.org/3/tutorial/controlflow.html#special-parameters

# bababa

# 默认参数值

def f(a, L=[]):
    L.append(a)
    return L

print(f(1))
print(f(2))
print(f(3))
[1]
[1, 2]
[1, 2, 3]
def f(a, L=None):
    if L is None:
        L = []
    L.append(a)
    return L

# 文档字符串

文档字符串 (documentation strings, docstrings)

# 函数注解

函数注解 (function annotations)

一般用于类型信息

def f(a: str, b: int) -> str:

# 作用域

global, nonlocal


#基础教程#编程语言#Python

← Python 00 - Introduction Python 02 - Data Structures (unfinished)→

最近更新
01
Linux Kernel 00 - Introduction
08-01
02
Linux Kernel 01 - Build and Run a Tiny Linux Kernel on QEMU
08-01
03
Linux Kernel 01 - Debug the Linux Kernel
08-01
更多文章>
Theme by Vdoing | Copyright © 2019-2022 JM233333 | CC BY-NC-SA 4.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式