Python中的关键字用法解析和代码实例

2021年3月31日12:23:57 发表评论 751 次浏览

Python关键字–介绍

本文旨在提供对这些关键字的详细了解。

1.True:此关键字用于表示布尔值true。如果语句为真, 则打印" True"。

2.False:此关键字用于表示布尔值false。如果语句为假, 则打印" False"。

python中的True和False与1和0相同。示例:

print False = = 0
print True = = 1
  
print True + True + True
print True + False + False

3.None:这是一个特殊的常量,用来表示空值或空值。重要的是要记住,0,任何空容器(e。g空列表)不计算到None。

它是其数据类型– NoneType的对象。无法创建多个None对象并将其分配给变量。

4. and:这是python中的逻辑运算符。"and"返回第一个假值,如果没有找到则返回最后一个。"“and”的真值表如下所示。

and

3和0 返回0

3和10 返回10

10或20或30或10或70返回10

对于像这样的语言的程序员来说, 上面的陈述可能会有些混乱C其中逻辑运算符始终返回布尔值(0或1)。以下几行直接来自python文档解释这个:

表达式x和y首先计算x;如果x为假, 则返回其值;否则, 将评估y并返回结果值。
表达式x或y首先计算x;如果x为true, 则返回其值;否则, 将评估y并返回结果值。

注意既不是and也不限制值和类型, 它们将返回False和True, 而是返回最后一个求值的参数。有时这很有用, 例如, 如果s是一个字符串, 如果为空则应替换为默认值, 则表达式s或" foo"会产生所需的值。由于不必创建新值, 因此无论其参数类型如何, 它都会返回一个布尔值(例如, 不是'foo'生成False而不是"。)

5.或:这是python中的逻辑运算符。 "要么"返回第一个True值。如果找不到, 则返回最后一个。"或"的真值表如下所示。

or

3或0返回3

3或10返回3

0或0或3或10或0返回3

6.not:此逻辑运算符反转真值。 " not"的真值表如下所示。

not
# Python code to demonstrate 
# True, False, None, and, or , not
  
# showing that None is not equal to 0
# prints False as its false.
print ( None = = 0 )
  
# showing objective of None
# two None value equated to None
# here x and y both are null
# hence true
x = None
y = None
print (x = = y)
  
# showing logical operation 
# or (returns True)
print ( True or False )
  
# showing logical operation 
# and (returns False)
print ( False and True )
  
# showing logical operation 
# not (returns False)
print ( not True )

输出如下:

False
True
True
False
False

7.assert断言:此功能用于调试目的。通常用于检查代码的正确性。如果某条语句的值为真, 则什么也不会发生, 但如果为假, "断言错误提出了。一个也可以打印带有错误的消息, 以逗号分隔.

8.break:" break"用于控制循环的流程。该语句用于跳出循环并将控制权传递给紧随循环之后的语句。

9.continue:" continue"也用于控制代码流。关键字跳过 当前循环的迭代, 但是没有结束循环.

在下面的文章中可以看到break和continue关键字的插图。

Python中的循环和控制语句(continue、break和pass)

0. class:该关键字用于声明用户定义的类。

11. def:此关键字用于声明用户定义的函数。有关更多信息。点击这里.

12.if:这是决策的控制声明。真相表达迫使控制进入" if"语句块。

13.else:这是决策的控制声明。错误的表达式会强制控件进入" else"语句块。

14.elif:这是决策的控制声明。它是"否则"

15.del:del用于删除对对象的引用。可以使用del删除任何变量或列表值。

# Python code to demonstrate 
# del and assert
  
# initialising list 
a = [ 1 , 2 , 3 ]
  
# printing list before deleting any value
print ( "The list before deleting any value" )
print (a)
  
# using del to delete 2nd element of list
del a[ 1 ]
  
# printing list after deleting 2nd element
print ( "The list after deleting 2nd element" )
print (a)
  
# demonstrating use of assert
# prints AssertionError
assert 5 < 3 , "5 is not smaller than 3"

输出如下:

The list before deleting any value
[1, 2, 3]
The list after deleting 2nd element
[1, 3]

运行时错误:

Traceback (most recent call last):
  File "9e957ae60b718765ec2376b8ab4225ab.py", line 19, in 
    assert 5<3, "5 is not smaller than 3"
AssertionError: 5 is not smaller than 3

下一篇–Python中的关键字|S2

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: