博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python基础:语法基础(3)
阅读量:7112 次
发布时间:2019-06-28

本文共 2092 字,大约阅读时间需要 6 分钟。

本篇主要介绍Python中一些基础语法,其中包括:标识符、关键字、常量、变量、表达式、语句、注释、模块和包等内容。

1. 标识符和关键字

1.1 标识符

  标识符是变量、常量、函数、属性、类、模块和包等指定的名称,Python语言中标识符的命名规则如下:

  (1)区分大小写,例Name与name是两个不同的标识符;

  (2)标识符首字母可以是下划线“_”或字母,但不能是数字;

  (3)标识符除首字母外的其它字符,可以是下划线“_”、字母和数字;

  (4)关键字不作为标识符;

  (5)Python内建函数不能作为标识符。

1.2 关键字

  Python语言中有33个关键字,其中只有三个(即True、False和None)首字母大写,其它均为全部小写。

>>> help()Welcome to Python 3.7's help utility!If this is your first time using Python, you should definitely check outthe 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 writingPython programs and using Python modules.  To quit this help utility andreturn 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 comeswith a one-line summary of what it does; to list the modules whose nameor summary contain a given string such as "spam", type "modules spam".help> keywordsHere is a list of the Python keywords.  Enter any keyword to get more help.False               class               from                orNone                continue            global              passTrue                def                 if                  raiseand                 del                 import              returnas                  elif                in                  tryassert              else                is                  whileasync               except              lambda              withawait               finally             nonlocal            yieldbreak               for                 not

2. 变量和常量

2.1 变量

  在Python中声明变量时,不需要指定数据类型。Python是动态类型语言,不会检查数据类型,在声明变量时不需要指定数据类型。

  在使用变量前需要对其赋值。没有赋值的变量是没有意义的,编译器会编译不通过。

  同一个变量可以反复赋值,而且可以是不同类型的变量。

  当不能确定变量或数据的类型时,可以使用解释器内置的函数type进行确认。

>>> hello = 'Hello World!'>>> hello'Hello World!'>>> type(hello)
>>> hello = 100>>> hello100>>> type(hello)

2.2 常量

  Python不能从语法上定义常量,Python没有提供一个关键字使得变量不能被修改。

  Python中只能讲变量当成常量使用,只是不要修改它。

转载于:https://www.cnblogs.com/libingql/p/10161823.html

你可能感兴趣的文章
DHCP租约时间工作原理
查看>>
Scrum敏捷软件开发之技术实践——测试驱动开发TDD
查看>>
php提示Fatal error: Call to undefined function imagecreate()
查看>>
Qt移动应用开发(六):QML与C++互动
查看>>
svn代码统计工具的金额
查看>>
2015第32周三
查看>>
Zero Downtime Upgrade of Oracle 10g to Oracle 11g Using GoldenGate — 3
查看>>
OCP-1Z0-051-标题决心-文章2称号
查看>>
Codeforces 56D Changing a String 编辑距离 记忆dp
查看>>
Scala 深入浅出实战经典 第62讲:Scala中上下文界定内幕中的隐式参数实战详解...
查看>>
Android应用Design Support Library完全使用实例
查看>>
中通打印助手-实现快递面单快速打印(免费使用)
查看>>
付款页面DEMO
查看>>
Swift - 使用Core Data进行数据持久化存储
查看>>
android L新控件RecyclerView具体解释DeMo
查看>>
[转载]服务器和应用系统迁移方案
查看>>
ActionBarActivity: cannot be resolved to a type
查看>>
类的专有方法(__init__)
查看>>
open()系统调用的实现
查看>>
java历史集合类对比
查看>>