Today we will learn about Python keywords and identifiers. Earlier we learned how to install python and get started with it in python tutorial for beginners.

今天,我们将学习Python关键字和标识符。 之前,我们在python初学者教程中学习了如何安装python并开始使用它。

Python关键字 (Python Keywords)

Well simply, python keywords are the words that are reserved. That means you can’t use them as name of any entities like variables, classes and functions.

简单来说,python关键字是保留的单词。 这意味着您不能将它们用作变量,类和函数等任何实体的名称。

So you might be thinking what are these keywords for. They are for defining the syntax and structures of Python language.

因此,您可能正在考虑这些关键字的用途。 它们用于定义Python语言的语法和结构。

You should know there are 33 keywords in Python programming language as of writing this tutorial. Although the number can vary in course of time. Also keywords in Python is case sensitive. So they are to be written as it is. Here is a list of all keywords in python programming.

在撰写本教程时,您应该知道Python编程语言中有33个关键字。 尽管数量会随着时间而变化。 另外,Python中的关键字区分大小写。 因此,应按原样编写它们。 这是python编程中所有关键字的列表。

If you look at all the keywords and try to figure out all at once, you will be overwhelmed. So for now just know these are the keywords. We will learn their uses respectively. You can get the list of python keywords through python shell help.

如果您查看所有关键字并尝试一次找出所有关键字,您将不知所措。 所以现在就知道这些是关键词。 我们将分别学习它们的用法。 您可以通过python shell帮助获取python关键字列表。

>>> help()Welcome to Python 3.6's help utility!help> keywordsHere is a list of the Python keywords.  Enter any keyword to get more help.False               def                 if                  raise
None                del                 import              return
True                elif                in                  try
and                 else                is                  while
as                  except              lambda              with
assert              finally             nonlocal            yield
break               for                 not
class               from                or
continue            global              pass                help>

Below is a simple example showing usage of if-else in python program.

下面是一个简单的示例,显示python程序中if-else的用法。

var = 1;if(var==1):print("odd")
else:print("even")

When we run above program, python understands the if-else block because of fixed keywords and syntax and then do the further processing.

当我们在上面的程序中运行时,由于固定的关键字和语法,python会理解if-else块,然后进行进一步处理。

Python标识符 (Python Identifiers)

Python Identifier is the name we give to identify a variable, function, class, module or other object. That means whenever we want to give an entity a name, that’s called identifier.

Python标识符是我们用来标识变量,函数,类,模块或其他对象的名称。 这意味着每当我们要给实体命名时,即称为标识符。

Sometimes variable and identifier are often misunderstood as same but they are not. Well for clarity, let’s see what is a variable?

有时,变量和标识符常常被误解为相同,但事实并非如此。 为了清楚起见,让我们看看什么是变量?

Python中的变量 (Variable in Python)

A variable, as the name indicates is something whose value is changeable over time. In fact a variable is a memory location where a value can be stored. Later we can retrieve the value to use. But for doing it we need to give a nickname to that memory location so that we can refer to it. That’s identifier, the nickname.

顾名思义,变量是随时间变化的值。 实际上,变量是可以存储值的存储位置。 稍后我们可以检索要使用的值。 但是为此,我们需要给该内存位置起一个昵称,以便我们可以对其进行引用。 这就是标识符,即昵称。

编写标识符的规则 (Rules for writing Identifiers)

There are some rules for writing Identifiers. But first you must know Python is case sensitive. That means Name and name are two different identifiers in Python. Here are some rules for writing Identifiers in python.

有一些编写标识符的规则。 但是首先您必须知道Python是区分大小写的。 这意味着Namename是Python中的两个不同的标识符。 以下是在python中编写标识符的一些规则。

  1. Identifiers can be combination of uppercase and lowercase letters, digits or an underscore(_). So myVariable, variable_1, variable_for_print all are valid python identifiers.标识符可以是大写和小写字母,数字或下划线(_)的组合。 所以myVariablevariable_1variable_for_print都是有效的python标识符。
  2. An Identifier can not start with digit. So while variable1 is valid, 1variable is not valid.标识符不能以数字开头。 因此,虽然variable1有效,但1variable无效。
  3. We can’t use special symbols like !,#,@,%,$ etc in our Identifier.我们在标识符中不能使用!,#,@,%,$等特殊符号。
  4. Identifier can be of any length.标识符可以是任何长度。

Though these are hard rules for writing identifiers, also there are some naming conventions which are not mandatory but rather good practices to follow.

尽管这些是编写标识符的硬性规则,但也有一些命名约定不是强制性的,而是遵循的良好实践。

  1. Class names start with an uppercase letter. All other identifiers start with a lowercase letter.类名以大写字母开头。 所有其他标识符以小写字母开头。
  2. Starting an identifier with a single leading underscore indicates the identifier is private.以单个下划线开头的标识符表示标识符是私有的。
  3. If the identifier starts and ends with two underscores, than means the identifier is language-defined special name.如果标识符以两个下划线开头和结尾,则表示标识符是语言定义的特殊名称。
  4. While c = 10 is valid, writing count = 10 would make more sense and it would be easier to figure out what it does even when you look at your code after a long time.尽管c = 10是有效的,但是将count = 10写入将更有意义,并且即使您长时间查看代码也更容易弄清楚它的作用。
  5. Multiple words can be separated using an underscore, for example this_is_a_variable.可以使用下划线分隔多个单词,例如this_is_a_variable

Here’s a sample program for python variables.

这是python变量的示例程序。

myVariable="hello world"
print(myVariable)var1=1
print(var1)var2=2
print(var2)

If you run the program, the output will be like below image.

如果运行该程序,输出将如下图所示。

So, that’s it for today. In the next tutorial we will learn about Python Statements and Comments. Till then #happy_coding

Python关键字和标识符相关推荐

  1. python关键字与标识符

    编程语言众多,但每种语言都有相应的关键字,Python 也不例外,它自带了一个 keyword 模块,用于检测关键字. 关键字列表 进入 Python 交互模式,获取关键字列表: >>&g ...

  2. [转载] [Python基础语法]关键字、标识符和变量

    参考链接: Python关键字和标识符 这篇教程将向您展示关于python关键字.标识符和变量的知识. Python关键字 Python关键词是Python保留的具有特定含义的特殊词语,用于执行某些操 ...

  3. python语言关键字是_Python 关键字和标识符

    Python 关键字和标识符 在本教程中,您将了解关键字(Python中的保留字)和标识符(变量.函数等的名称). Python关键字 关键字是Python中的保留字. 我们不能将关键字用作 变量名, ...

  4. [转载] python关键字和保留字_Python关键字

    参考链接: Python关键字和标识符 python关键字和保留字 关键词 (Keywords) Keywords are the reserved words in Python programmi ...

  5. python标识符和关键字_Python标识符和关键字资料解析

    标识符和关键字都是之中具有某种意义的标记和称谓,就像人的外号一样.所谓的标识符就是代码的分号.单引号.双引号等等就是标识符,而代码中的if.for等等就是关键字. python语言的标识符使用规则和C ...

  6. python中的标识符能不能使用关键字_Python中的标识符不能使用关键字

    Python中的标识符不能使用关键字 答:√ 智慧职教: 检查客室座椅外观良好,确认?无破损 答:坐垫 靠背 关于投标报价时综合单价的确定,下列做法中正确的是() 答:以项目特征描述为依据确定综合单价 ...

  7. 计算机简介Python简介关键字和标识符输入输出

    一.计算机简介[了解] 计算机之父 - 冯.诺依曼提出了计算机的五大部分:输入设备,输出设备,存储器,运算器和控制器 现在我们电脑的配置有:键盘鼠标 显示器 机箱 音响等 ​ 键盘鼠标:告诉电脑进行什 ...

  8. Python中的标识符有哪些基础原则?

    很多同学学习Python技术的过程中,会接触一些标识符的知识,这部分也是Python的基础知识,那么Python中的标识符有哪些基础原则?接下来我们一起来看看详细的内容介绍吧,希望对你们有Python ...

  9. python 关键字大全_一日一技:用实例列举python中所有的关键字(01)

    Python关键字列表 本教程提供有关Python中使用的所有关键字的简要信息. 关键字是Python中的保留字.我们不能将关键字用作变量名,函数名或任何其他标识符. 以下是Python中所有关键字的 ...

最新文章

  1. java 虚拟打印机_Java 通过物理、虚拟打印机打印Word文档
  2. ​Xamarin iOS教程之自定义视图
  3. redis数据持久化到mysql_Redis【数据持久化篇】
  4. linux结束进程_生人勿近之Linux里养僵尸
  5. 字符设备驱动高级篇4——设备类(自动创建和删除设备文件)相关代码分析
  6. Pytorch采坑记录:DDP加载之前的checkpoint后loss上升(metric下降)
  7. Thinkphp开发时关闭缓存的方法
  8. 多伦多大学计算机科学升大二,加拿大多伦多大学优势专业排名_加拿大多伦多大学优势专业盘点...
  9. BMW M550i xDrive
  10. python汉化之后好用吗_关于Python写的程序汉化心得和所走过的各种坑儿
  11. alook浏览器_Alook浏览器安卓版本-Alook浏览器(2倍速)安卓版本下载v3.4.1-121安卓网...
  12. 设置自动启动ssh服务
  13. 2021年最新AZ自动发卡网源码-全网首发
  14. 智能驾驶是什么意思_智能驾驶L1L2L3L4到底是什么意思,再不知道就OUT啦
  15. 计算机组成原理之概述篇(一)
  16. html 设置图片显示比例,css巧妙设置等比例图片显示
  17. html2canvas的使用以及跨域问题
  18. Unity3d自学之路(一)
  19. Redis 详细入门教程
  20. C语言统计文章单词出现的次数,统计英文文章中单词出现频率

热门文章

  1. [转载收藏]三层式开发中的层次划分
  2. [转载] Python内置函数-min函数和max函数-详解
  3. [转载] python3 opencv 图像二值化笔记(cv2.adaptiveThreshold)
  4. 数据结构与算法(Python)第四天
  5. Java匹马行天下之C国程序员的秃头原因
  6. (课堂画图)毕业论文管理系统
  7. 脚本化CSS类-HTML5 classList属性
  8. 自动驾驶算法-滤波器系列(六)——10+种经典滤波算法
  9. urdf与xacro的使用方法 机械臂模型仿真示例
  10. jupyter notebook matplotlib绘制动态图并显示在notebook中