本篇主要介绍Python中一些基础语法,其中包括:标识符、关键字、常量、变量、表达式、语句、注释、模块和包等内容。python
标识符是变量、常量、函数、属性、类、模块和包等指定的名称,Python语言中标识符的命名规则以下:async
(1)区分大小写,例Name与name是两个不一样的标识符;函数
(2)标识符首字母能够是下划线“_”或字母,但不能是数字;ui
(3)标识符除首字母外的其它字符,能够是下划线“_”、字母和数字;this
(4)关键字不做为标识符;spa
(5)Python内建函数不能做为标识符。code
Python语言中有33个关键字,其中只有三个(即True、False和None)首字母大写,其它均为所有小写。blog
>>> help() Welcome to Python 3.7's help utility! If this is your first time using Python, you should definitely check out the tutorial on the Internet at https://docs.python.org/3.7/tutorial/. Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type "quit". To get a list of available modules, keywords, symbols, or topics, type "modules", "keywords", "symbols", or "topics". Each module also comes with a one-line summary of what it does; to list the modules whose name or summary contain a given string such as "spam", type "modules spam". help> keywords Here is a list of the Python keywords. Enter any keyword to get more help. False class from or None continue global pass True def if raise and del import return as elif in try assert else is while async except lambda with await finally nonlocal yield break for not
在Python中声明变量时,不须要指定数据类型。Python是动态类型语言,不会检查数据类型,在声明变量时不须要指定数据类型。get
在使用变量前须要对其赋值。没有赋值的变量是没有意义的,编译器会编译不经过。编译器
同一个变量能够反复赋值,并且能够是不一样类型的变量。
当不能肯定变量或数据的类型时,能够使用解释器内置的函数type进行确认。
>>> hello = 'Hello World!' >>> hello 'Hello World!' >>> type(hello) <class 'str'> >>> hello = 100 >>> hello 100 >>> type(hello) <class 'int'>
Python不能从语法上定义常量,Python没有提供一个关键字使得变量不能被修改。
Python中只能讲变量当成常量使用,只是不要修改它。