Python关键字是python编程语言的保留字。这些关键字不能用于其他目的。

Python中有35个关键字-下面列出了它们的用法。

Keyword Description
and logical AND operator. Return True if both statements are True.

= (5 3 and 5 10)

print(x)    # True

or logical OR operator. Returns True if either of two statements is true. If both statements are false, the returns False.

= (5 3 or 5 10)

print(x)    # True

as It is used to create an alias.

import calendar as c

print(c.month_name[1])  #January

assert It can be used for debugging the code. It tests a condition and returns True , if not, the program will raise an AssertionError.

= "hello"

assert == "goodbye""x should be 'hello'"  # AssertionError

async It is used to declare a function as a coroutine, much like what the @asyncio.coroutine decorator does.

async def ping_server(ip):

await It is used to call async coroutine.

async def ping_local():

    return await ping_server('192.168.1.1')

class It is used to create a class.

class User:

  name = "John"

  age = 36

def It is used to create or define a function.

def my_function():

  print("Hello world !!")

my_function()

del It is used to delete objects. In Python everything is an object, so the del keyword can also be used to delete variables, lists, or parts of a list, etc.

= "hello"

del x

if It is used to create conditional statements that allows us to execute a block of code only if a condition is True.

= 5

if x > 3:

  print("it is true")

elif It is used in conditional statements and is short for else if.

= 5

if i > 0:

    print("Positive")

elif == 0:

    print("ZERO")

else:

    print("Negative")

else It decides what to do if the condition is False in if..else statement.

= 5

if i > 0:

    print("Positive")

else:

    print("Negative")

It can also be use in try...except blocks.

= 5

try:

    x > 10

except:

    print("Something went wrong")

else:

    print("Normally execute the code")

try It defines a block of code ot test if it contains any errors.
except It defines a block of code to run if the try block raises an error.

try:

    x > 3

except:

    print("Something went wrong")

finally It defines a code block which will be executed no matter if the try block raises an error or not.

try:

    x > 3

except:

    print("Something went wrong")

finally:

     print("I will always get executed")

raise It is used to raise an exception, manually.

= "hello"

if not type(x) is int:

    raise TypeError("Only integers are allowed")

False It is a Boolean value and same as 0.
True It is a Boolean value and same as 1.
for It is used to create a for loop. A for loop can be used to iterate through a sequence, like a list, tuple, etc.

for in range(19):

    print(x)

while It is used to create a while loop. The loop continues until the conditional statement is false.

= 0

while x < 9:

    print(x)

    = + 1

break It is used to break out a for loop, or a while loop.

= 1

while i < 9:

    print(i)

    if == 3:

        break

    += 1

continue It is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.

for in range(9):

    if == 3:

        continue

    print(i)

import It is used to import modules.

import datetime

from It is used to import only a specified section from a module.

from datetime import time

global It is used to create global variables from a no-global scope, e.g. inside a function.

def myfunction():

    global x

    = "hello"

in 1. It is used to check if a value is present in a sequence (list, range, string etc.).
2. It is also used to iterate through a sequence in a for loop.

fruits = ["apple""banana""cherry"]

if "banana" in fruits:

    print("yes")

for in fruits:

    print(x)

is It is used to test if two variables refer to the same object.

= ["apple""banana""cherry"]

= ["apple""banana""cherry"]

= a

print(a is b)   # False

print(a is c)   # True

lambda It is used to create small anonymous functions. They can take any number of arguments, but can only have one expression.

= lambda a, b, c : a + + c

print(x(562))

None It is used to define a null value, or no value at all. None is not the same as 0, False, or an empty string.
None is a datatype of its own (NoneType) and only None can be None.

= None

if x:

  print("Do you think None is True")

else:

  print("None is not True...")      # Prints this statement

nonlocal It is used to declare that a variable is not local. It is used to work with variables inside nested functions, where the variable should not belong to the inner function.

def myfunc1():

    = "John"

    def myfunc2():

        nonlocal x

        = "hello"

    myfunc2()

    return x

print(myfunc1())

not It is a logical operator and reverses the value of True or False.

= False

print(not x)    # True

pass It is used as a placeholder for future code. When the pass statement is executed, nothing happens, but you avoid getting an error when an empty code is not allowed.

Empty code is not allowed in loops, function definitions, class definitions, or in if statements.

for in [012]:

            pass

return It is to exit a function and return a value.

def myfunction():

            return 3+3

with Used to simplify exception handling
yield To end a function, returns a generator

学习愉快!

Python中的关键字相关推荐

  1. 以下哪个不是python中的关键字-以下不是python中的关键字

    [单选题]直径数字前应加符号( ) [单选题]Python 中对变量描述错误的选项是: [填空题]近头者为 [单选题]以下对 Python 程序缩进格式描述错误的选项是 [单选题]关于Python语言 ...

  2. python的上下文管理用哪个关键字_正确理解python中的关键字“with”与上下文管理器...

    正确理解python中的关键字"with"与上下文管理器 来源:中文源码网    浏览: 次    日期:2018年9月2日 [下载文档:  正确理解python中的关键字&quo ...

  3. 下列选项中不属于python 3中的关键字是_以下不是python中的关键字

    [多选题]登记银行存款日记账的依据是( ). [判断题]总分类账提供的总括信息是对明细分类账详细信息的综合,它对明细分类账具有统驭作用;而明细分类账提供的详细信息是对总分类账的补充.解释和说明.( ) ...

  4. 在python中查看关键字、需要执行_python关键字以及含义,用法

    Python常用的关键字 1.and , or and , or 为逻辑关系用语,Python具有短路逻辑,False and 返回 False 不执行后面的语句, True or 直接返回True, ...

  5. Python中else关键字的常见用法

    Python中的else常见用法有三:选择结构.循环结构和异常处理结构. (1)选择结构 这应该是最常见的用法,与关键字if和elif组合来使用,用来说明条件不符合时应执行的代码块. (2)循环结构 ...

  6. Python中的关键字和内置函数

    Python中所有的关键字(共有33个关键字): Python中所有的内置函数: 注意 在Python 2.7中, print是关键字而不是函数.另外, Python 3没有内置函数unicode() ...

  7. Python中的关键字的用法

    Python有哪些关键字 -Python常用的关键字 and, del, from, not, while, as, elif, global, or, with, assert, else, if, ...

  8. python中查看关键字需要在python解释器中执行_Day09-python基础之Cpython解释器支持的进程与线程...

    一.进程与线程理论基础 1.背景知识 进程的概念起源于操作系统,是操作系统最核心的概念. 进程是对正在运行程序的一个抽象,操作系统的其他所有内容都是围绕进程的概念展开的.所以想要真正了解进程,必须事先 ...

  9. 以下哪个不是python中的关键字-以下哪个选项不是Python语言的保留字

    [单选题]以下赋值语句中合法的是 [单选题]关于颞下颌关节的运动,说法错误的是 ( ) [单选题]颞下颌关节的负重区为 ( ) [填空题]在Python中__________表示空类型. [填空题]l ...

  10. 在python中查看关键字需要在python解释器中执行_现有代码 d={},在Python3解释器中执行 d[([1,2])] = 'b'得到的结果为( )。...

    [单选题]下列字符中对应ASCII码数值最小的是哪个选项?( ) [单选题]Python解释器执行'{0},{2},{1}'.format('a','b','c')的结果为( ). [单选题]Pyth ...

最新文章

  1. cocos2d-x android 移植 问题
  2. Android防止按钮连续点击
  3. Android创建自己的gradle依赖包
  4. 架构师之路 — API 经济 — API 实现方式
  5. LeetCode-剑指 Offer 04. 二维数组中的查找
  6. 每天一道LeetCode-----有序数组循环右移n位后,寻找最小值,数组中可能包含重复元素
  7. fftw-3.3.8库在linux下的的编译和配置
  8. 多个摄像机之间的切换
  9. 南下事业篇——深圳 深圳(回顾)
  10. mysql定时增量备份_Mysql日常自动备份和增量备份脚本
  11. spring面向接口编程
  12. 华为培训视频-AAA培训
  13. linux 下部署tomcat问题
  14. 用python写一个倒计时器
  15. Java写的小游戏贪吃蛇代码
  16. 自编Photoshop简单教程
  17. 揭明星工作室待遇:助理3000经纪人30万
  18. 西游记中13大高手排名
  19. AcWing 0x00. 语法基础课【Python3】版题解-顺序/判断/循环语句
  20. 令牌桶过滤器(TBF)

热门文章

  1. 黑马程序员-骑士飞行棋
  2. 大数据采集工具Flume
  3. 美通社日历 | 媒体关注、会展信息、企业财报发布,节假日备忘(1月4日—1月10日)...
  4. java通过freemarker导出word文档带图片并且循环
  5. 千分位分隔符保留两位小数
  6. 方舟起源服务器配置文件,方舟生存进化单机版配置ini文件 设置指引
  7. 异步通信先,有效数据速率计算
  8. 为电竞而声:1MORE万魔耳机新品亮相ChinaJoy
  9. 自媒体神器 Previs Shot 使用指南
  10. 睡前写几句,缓解一下刷题的心情。。。