Python系列学习笔记(二)——基础语法规则

来源:互联网 发布:asp网站sql注入 编辑:程序博客网 时间:2024/06/05 20:20

前言

本教程会依赖Python3作为依赖讲解, 这节我们介绍基础的语法应用。

标识符的定义

  • 第一个字符必须是字母表中字母或下划线’_’。
  • 标识符的其他的部分有字母、数字和下划线组成。
  • 标识符对大小写敏感,也就是区分大小写

预留关键字,在声明变量的时候无法使用, 在python中提供了如下类库,可以查看当前的python所有保留关键字。

import keywordprint(keyword.kwlist)

结果:

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

行与缩进

在Python中最重要的特色就是行缩进, 如下:

if False:    print("True")else:    print("False")

如果沒有缩进,会报错误:

  File "/Users/tyler/PycharmProjects/learn-case01/hello.py", line 8    print("True")        ^IndentationError: expected an indented block

多行语句

Python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠()来实现多行语句

total = item_one + \        item_two + \        item_three

在 [], {}, 或 () 中的多行语句,不需要使用反斜杠()

total = ['item_one', 'item_two', 'item_three',        'item_four', 'item_five']

Python可以在同一行中使用多条语句,语句之间使用分号(;)分割.