Python中的关键字|设置 2
Python关键字 - 简介
Python中的关键字|设置 1
更多关键词:
16. try : 这个关键字用于异常处理,用于捕捉代码中使用关键字except的错误。检查“try”块中的代码,如果有任何类型的错误,除了块被执行。
17. except :如上所述,这与“try”一起使用以捕获异常。
18. raise :也用于异常处理以显式地引发异常。
19. finally :无论“try”块的结果是什么,被称为“finally”的块总是被执行。详细文章Python中的异常处理
20. for :这个关键字用于控制流和for循环。
21. while :有类似“for”的作用,用于控制流和for循环。
22. pass :是Python中的null语句。遇到这种情况时没有任何反应。这用于防止缩进错误并用作占位符
详细文章 – for, while, pass
23. import :该语句用于将特定模块包含到当前程序中。
24. from :一般与import一起使用,from用于从导入的模块中导入特定的功能。
25. as :该关键字用于为导入的模块创建别名。即给导入的模块一个新名称。例如,将数学导入为 mymath。
详细文章 - 进口,来自和作为
26. lambda : 这个关键字用于制作内联返回函数,内部不允许使用任何语句。详细文章——map、filter、lambda
27.return :这个关键字用来从函数中返回。详细文章 – Python中的返回值。
28.yield :这个关键字和return语句一样使用,但是用来返回一个生成器。详细文章——yield关键字
29. with :这个关键字用于将代码块的执行包装在上下文管理器定义的方法中。这个关键字在日常编程中使用得不多。
30. in :该关键字用于检查容器是否包含值。该关键字也用于循环容器。
31. is : 该关键字用于测试对象身份,即检查两个对象是否占用相同的内存位置。
Python
# Python code to demonstrate working of
# in and is
# using "in" to check
if 's' in 'geeksforgeeks':
print ("s is part of geeksforgeeks")
else : print ("s is not part of geeksforgeeks")
# using "in" to loop through
for i in 'geeksforgeeks':
print (i,end=" ")
print ("\r")
# using is to check object identity
# string is immutable( cannot be changed once allocated)
# hence occupy same memory location
print (' ' is ' ')
# using is to check object identity
# dictionary is mutable( can be changed once allocated)
# hence occupy different memory location
print ({} is {})
Python
# Python code to demonstrate working of
# global and non local
#initializing variable globally
a = 10
# used to read the variable
def read():
print (a)
# changing the value of globally defined variable
def mod1():
global a
a = 5
# changing value of only local variable
def mod2():
a = 15
# reading initial value of a
# prints 10
read()
# calling mod 1 function to modify value
# modifies value of global a to 5
mod1()
# reading modified value
# prints 5
read()
# calling mod 2 function to modify value
# modifies value of local a to 15, doesn't effect global value
mod2()
# reading modified value
# again prints 5
read()
# demonstrating non local
# inner loop changing the value of outer a
# prints 10
print ("Value of a using nonlocal is : ",end="")
def outer():
a = 5
def inner():
nonlocal a
a = 10
inner()
print (a)
outer()
# demonstrating without non local
# inner loop not changing the value of outer a
# prints 5
print ("Value of a without using nonlocal is : ",end="")
def outer():
a = 5
def inner():
a = 10
inner()
print (a)
outer()
输出:
s is part of geeksforgeeks
g e e k s f o r g e e k s
True
False
32. global :该关键字用于定义函数内部的变量为全局范围。
33. non-local : 这个关键字的作用类似于 global,但不是 global,这个关键字声明一个变量来指向外部封闭函数的变量,在嵌套函数的情况下。
Python
# Python code to demonstrate working of
# global and non local
#initializing variable globally
a = 10
# used to read the variable
def read():
print (a)
# changing the value of globally defined variable
def mod1():
global a
a = 5
# changing value of only local variable
def mod2():
a = 15
# reading initial value of a
# prints 10
read()
# calling mod 1 function to modify value
# modifies value of global a to 5
mod1()
# reading modified value
# prints 5
read()
# calling mod 2 function to modify value
# modifies value of local a to 15, doesn't effect global value
mod2()
# reading modified value
# again prints 5
read()
# demonstrating non local
# inner loop changing the value of outer a
# prints 10
print ("Value of a using nonlocal is : ",end="")
def outer():
a = 5
def inner():
nonlocal a
a = 10
inner()
print (a)
outer()
# demonstrating without non local
# inner loop not changing the value of outer a
# prints 5
print ("Value of a without using nonlocal is : ",end="")
def outer():
a = 5
def inner():
a = 10
inner()
print (a)
outer()
输出:
10
5
5
Value of a using nonlocal is : 10
Value of a without using nonlocal is : 5