1. Python基础教程
  2. 在SublimeEditor中配置Python环境
  3. Python代码中添加注释
  4. Python中的变量的使用
  5. Python中的数据类型
  6. Python中的关键字
  7. Python字符串操作
  8. Python中的list操作
  9. Python中的Tuple操作
  10. Pythonmax()和min()–在列表或数组中查找最大值和最小值
  11. Python找到最大的N个(前N个)或最小的N个项目
  12. Python读写CSV文件
  13. Python中使用httplib2–HTTPGET和POST示例
  14. Python将tuple开箱为变量或参数
  15. Python开箱Tuple–太多值无法解压
  16. Pythonmultidict示例–将单个键映射到字典中的多个值
  17. PythonOrderedDict–有序字典
  18. Python字典交集–比较两个字典
  19. Python优先级队列示例

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

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

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

= (5 3 and 5 10)
print(x)    # True
or A 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

学习愉快!

参考:W3 Schools

(Python基础教程之六)Python中的关键字的更多相关文章

  1. (Python基础教程之十三)Python中使用httplib2 – HTTP GET和POST示例

    Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...

  2. (Python基础教程之八)Python中的list操作

    Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...

  3. (Python基础教程之十二)Python读写CSV文件

    Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...

  4. (Python基础教程之二十二)爬虫下载网页视频(video blob)

    Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...

  5. 改写《python基础教程》中的一个例子

    一.前言 初学python,看<python基础教程>,第20章实现了将文本转化成html的功能.由于本人之前有DIY一个markdown转html的算法,所以对这个例子有兴趣.可仔细一看 ...

  6. python基础教程笔记—即时标记(详解)

    最近一直在学习python,语法部分差不多看完了,想写一写python基础教程后面的第一个项目.因为我在网上看到的别人的博客讲解都并不是特别详细,仅仅是贴一下代码,书上内容照搬一下,对于当时刚学习py ...

  7. 《python基础教程(第二版)》学习笔记 函数(第6章)

    <python基础教程(第二版)>学习笔记 函数(第6章) 创建函数:def function_name(params):  block  return values 记录函数:def f ...

  8. python基础教程(2)

    Python 基础教程 Python 是一种解释型.面向对象.动态数据类型的高级程序设计语言. 执行Python程序 对于大多数程序语言,第一个入门编程代码便是 "Hello World!& ...

  9. Python基础教程 (第2+3 版)打包pdf|内附网盘链接提取码

                <Python基础教程 第3版>包括Python程序设计的方方面面:首先,从Python的安装开始,随后介绍了Python的基础知识和基本概念,包括列表.元组.字符 ...

  10. 开始学python不知该怎么学?Python基础教程(第2版) 免费下载

    Python基础教程(第2版)pdf高清版免费下载  解压码:n0nl   内容简介  · · · · · · 本书是经典教程的全新改版,作者根据Python 3.0版本的种种变化,全面改写了书中内容 ...

随机推荐

  1. Blender 2D动画

    前情提要: 本来之前会的,很久没有弄,居然忘了,忘得透透的,没得办法,先简单记录一下 前提: 安装有Blender软件 步骤: 1. 打开Blender 2.点击文件,新建,2D Animation ...

  2. how to create rpm

    RPM Spec 中各个字段的 pre, post, preun, postun 的用法 https://www.golinuxhub.com/2018/05/how-to-execute-scrip ...

  3. Hydra(海德拉)工具使用从0到1,爆破服务器密码,2024最新版

    Hydra(海德拉)工具使用从0到1,爆破服务器密码,2024最新版 Hydra简介 Hydra又叫九头蛇,是一款由著名的黑客组织THC开发的开源暴力破解工具,支持大部分协议的在线密码破解,是网络安全 ...

  4. 使用Navicat Premium 将数据库导入、导出方法

    数据库导出 1.双击要导出的数据库,右键选转储SQL文件-,选择要保存的文件夹. 2.点击开始后,开始导出. 数据库导入 1.新建数据库,数据库的名字必须和导入的数据库文件一致. 2.在新建的数据库右 ...

  5. [TK] 寻宝游戏

    在树上标记若干个点,求出从某个点走过全部点并回到该点的最小路径. 有多次询问,每次询问只改变一个点. 首先是一个暴力的思路. 会发现,从标记点中的其中一个开始走,结果一定更优,并且无论从哪个点开始走, ...

  6. .Net Web项目中,实现轻量级本地事件总线 框架

    一.事件总线设计方案 1.1.事件总线的概念 事件总线是一个事件管理器,负责统一处理系统中所有事件的发布和订阅. 事件总线模式通过提供一种松耦合的方式来促进系统内部的业务模块之间的通信,从而增强系统的 ...

  7. 阿里云Tomcat7配置域名详解

    一. 进入阿里云服务控制台,点击SSL证书 看到下载了么,对应着你的域名点击下载服务器类型选择Tomcat,点击下载,压缩包中包含 xxxxx__test.com.pfx, pfx-password. ...

  8. 《Vue.js 设计与实现》读书笔记 - 第10章、双端 Diff 算法

    第10章.双端 Diff 算法 10.1 双端比较的原理 上一章的移动算法并不是最优的,比如我们把 ABC 移动为 CAB,如下 A C B --> A C B 按照上一章的算法,我们遍历新的数 ...

  9. C#查漏补缺----Exception处理实现,无脑抛异常不可取

    前言 环境:.NET 8.0 系统:Windows11 参考资料:CLR via C#, .Net Core底层入门 https://andreabergia.com/blog/2023/05/err ...

  10. DirectoryOpus插件:“照得标管理器”-海量照片分类管理好帮手!

      照得标管理器 前言   名词解释:"照得标管理器",即:照片得到标签管理器,后文统一简称"照得标管理器"或"照得标".  注:请不要和抖 ...