1、关键词Global和nonlocal是不同的?
global 表示一个变量的范围是在模块级定义的
哪个关键字可以防止改变嵌套函数中的变量?
关键字nonlocal表示一个嵌套函数中的变量不属于该内部函数的范围。
函数中的变量不属于该内部函数的范围
而是属于封装它的函数。
# global
global x
x = "Hello"
def myfunc1():
def myfunc2():
x = "John"
myfunc2()
return x
print(myfunc1())
Hello
与global的作用域不同,
# nonlocal
def myfunc1():
x = "John"
def myfunc2():
x = "hello"
myfunc2()
return x
print(myfunc1())
John
The keyword global is different.
It indicates that the scope of a variable is defined at the module level
一个变量可以同时是全局和非局部的: True or False ?
will output: False
一个变量不能同时是全局和非局部的。如果一个变量是非局部的,那就意味着它也是全局的。
def my_main_function():
def my_nested_function():
global my_variable
nonlocal my_variable
抛出错误 SyntaxError exception.
2、字符串是可变的吗?
大喵提示:对比数组和列表等元素通过对元素重新赋值而修改
Strings are mutable.
True
False
will output: True or False ?
3、What is the output of the code snippet below?
a = {}
b = {}
print(a is b)
will output: True or False ?
4、单引号和双引号
In Python, 'Hello', is the same as "Hello".
False
True
will output: True or False ?
fstring 案例请参考链接>>>
深入了解
Python的字符串函数是非常流行的。在Python中,有两种方法来表示字符串。字符串可以用单引号或双引号括起来。这两种方式(单引号或双引号)都是正确的,取决于需求。有时我们必须在同一个字符串中同时使用引号(单引号或双引号),在这种情况下,我们交替使用单引号和双引号,这样就可以将它们区分开来。
例子 #1:看看下面的例子,分析一下错误 -
Gives Error
print('It's python')
解释 -它给出了一个无效的语法错误。因为 "it "后面的单引号被认为是字符串的结束,其余部分不是字符串的一部分。
它可以被改正为
print("It's Python !")
输出。
It's Python!
例子 #2:
如果你想在Python中打印 "WithQuotes",这不能只用单引号(或双引号)来完成,它需要同时使用这两个引号。
# 这段代码在引号内打印输出。
# 在单引号内打印 WithQuotes
print("'WithQuotes'")
print("Hello 'Python'")
'WithQuotes'
Hello 'Python'
在单引号内打印WithQuotes
print('"WithQuotes"')
print('Hello "Python"' )
"WithQuotes"
Hello "Python"
结论 --
两种类型(单引号和双引号)的选择取决于程序员的选择。一般来说,双引号用于表示字符串,单引号用于正则表达式、Dict键或SQL。因此,单引号和双引号在python中都描述了字符串,但有时我们需要使用一种类型而不是另一种。
5、any()输出结果是?
What is the output of the code snippet below?
print(any([[], [], []]))
False
True
will output: True or False ?
6、any()输出结果是?
What is the output of the code snippet below?
print(any([[], [], []]))
False
True
will output: True or False ?
6、Python之禅有👇哪句话?
Which sentence is part of The Zen of Python?
- Nested is better than flat
- Flat is better than nested
Aboved stated which is Correct?👍
Python之禅全文
In Python console, typing import this will display the Zen of Python poem: Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
will output: True or False ?
7 Pandas是什么的简称?
Pandas is short for?
Panamerican Data
Panel data
Panoramic Data
Partial Negated Data
Partially Negated Data
will output: Panel data
8 一个装饰者可以用自己来装饰。
错了!👎
一个装饰器不能用自己来装饰。类似这样的情况。
A decorator can be decorated with itself.
Wrong!👎
A decorator cannot be decorated with itself. Something like:
@my_decorator
def my_decorator(a_function):
print("This is decorated")
return a_function
will throw a NameError exception.
将抛出一个NameError异常。
9 位操作符合?
如果其中一个位是1,另一个位是0,哪个位运算符给出1,否则返回假?
Which bitwise operator gives 1 if one of the bit is 1 and other is 0 else returns False?
AND
XOR
NOT
OR
None of the above
will output: XOR
10 位操作符合?
如果其中一个位是1,另一个位是0,哪个位运算符给出1,否则返回假?
Which bitwise operator gives 1 if one of the bit is 1 and other is 0 else returns False?
AND
XOR
NOT
OR
None of the above
will output: XOR
11 哪个伪模块可以用来启用与当前解释器不兼容的新语言功能?
_combat _
_ga _
_future _
_roadmap _
_compatiblity_
future是一个伪模块,程序员可以用它来启用与当前解释器不兼容的新语言特性。
will output: __future__
12
abc模块的目的是什么?
正确!👍
abc 模块提供了在 Python 中定义抽象基类 (ABC) 的基础结构,如 PEP 3119 所述;请参见 PEP,了解为什么要在 Python 中加入这个模块。(也请参见 PEP 3141 和 numbers 模块中关于基于 ABC 的数字类型层次的内容。 哪个伪模块可以用来启用与当前解释器不兼容的新语言功能?
_combat _
_ga _
_future _
_roadmap _
_compatiblity_
future是一个伪模块,程序员可以用它来启用与当前解释器不兼容的新语言特性。
will output: __future__
13 Unicode 13.0.0包含多少个字符?
8,143,859个字符
143,859个字符
4,859个字符
14,859个字符
will output: 143,859个字符
14 字典可以无限地嵌套吗?
正确!👍
字典可以无限地嵌套。唯一的限制是可用的内存。
a = {'a1':[{'a2':{'a3':[4,5,6]}}]}
eval(70*'['+70*']')
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
will output: 143,859个字符
15 理论上Unicode可以编码多少个字符?
1,111,998
11,111,998
111,111,998
111,198
will output: 11,111,998
16
下面的代码片断的输出是什么?
print(reversed("hello") == "olleh")
真
假的
# for string
seq_string = 'Python'
print(list(reversed(seq_string)))
# for tuple
seq_tuple = ('P', 'y', 't', 'h', 'o', 'n')
print(list(reversed(seq_tuple)))
# for range
seq_range = range(5, 9)
print(list(reversed(seq_range)))
# for list
seq_list = [1, 2, 4, 3, 5]
print(list(reversed(seq_list)))
class Vowels:
vowels = ['a', 'e', 'i', 'o', 'u']
def __reversed__(self):
return reversed(self.vowels)
v = Vowels()
print(list(reversed(v)))
will output: False
17 哪个运算符函数与下面的语法相对应?
a @ b
matmul(a, b)
matprod(a, b)
matprodarray(a, b)
mul(a, b)
will output: matmul(a, b)
operator — Standard operators as functions — Python 3.10.4 documentation
18下面的代码片断的输出是什么?
"python"[-4:0:-2]
o
tho
oht
th
t
will output: t
operator — Standard operators as functions — Python 3.10.4 documentation
18 首先调用的装饰器是哪个?
def make_bold(fn):
return lambda: "<b>" + fn() + "</b>"
def make_italic(fn):
return lambda: "<i>" + fn() + "</i>"
@make_bold
@make_italic
def hello():
return "hello world"
helloHTML = hello()
print(helloHTML)
<b><i>hello world</i></b>
will output: make_italic(fn)
正确!👍
装饰器的应用是由下至上的。首先应用@make_italic,然后是@make_bold。
19 以下代码片断的输出是什么?
True or False ?
f = None
with open("data.txt", "w") as f:
pass
print(f.closed)
will output: True
正确!👍
在这里,with语句保证了文件在块的末尾被关闭。
20 subclass()可以接受类实例作为属性吗?
True or False ?
class MyDict(dict):
pass
print(issubclass(MyDict, dict))
However, this will throw a TypeError exception:
class MyDict(dict):
pass
my_dict = MyDict()
a_dict = dict()
print(issubclass(my_dict, a_dict))
(programiz.com/python-programming/methods/built-in/issubclass)
will output: False
正确!👍
issubclass()只接受类作为属性,不接受类的实例。这样就可以了。
网友评论