python 列出Python中的内置函数和操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 列出Python中的内置函数和操作相关的知识,希望对你有一定的参考价值。

my_string = "egg, egg, Spam, egg and Spam"
print my_string.replace("egg", "Spam", 2)
# the output would be:
# Spam, Spam, Spam, egg and Spam
# notice that only the first 2 "egg" matches were replaced in the string.

x = [1,2,3,4,5]
x.append(99)
print x
#the output would be [1,2,3,4,5,99]

x = [1,2,3,4,5]
x.insert(2,99)
print x
#the output would be [1,2,99,3,4,5]

x = [1,2,3,4,5]
x.remove(3)
print x
#the output would be [1,2,4,5]

x = [1,2,3,4,5]
x.pop()
print x
#the output would be [1,2,3,4]
y = [10,11,12,13,14]
y.pop(1)
print y
#the output would be [10,12,13,14]

x = [99,4,2,5,-3];
x.sort()
print x
#the output would be [-3,2,4,5,99];

x = [99,4,2,5,-3];
print x[:]
#the output would be [99,4,2,5,-3]
print x[1:]
#the output would be [4,2,5,-3];
print x[:4]
#the output would be [99,4,2,5]
print x[2:4]
#the output would be [2,5];

my_list = [1, 'Zen', 'hi']
print len(my_list)
# the output would be 3

my_list = [1, 7, 3]
print max(my_list)
# the output would be 7

my_list = [1, 7, 3]
print min(my_list)
# the output would be 1

my_list = [0, 'hi', '']
print any(my_list)
# the output would be True since a string would equate to true in this case
my_list = [0, '']
print any(my_list)
# the output would be False since 0 (zero) and an empty string will both be false

my_list = [0, 'Zen', '']
print all(my_list)
# the output would be False
my_list = [4, 'hi']
print all(my_list)
# the output would be True

以上是关于python 列出Python中的内置函数和操作的主要内容,如果未能解决你的问题,请参考以下文章

python内置常用高阶函数(列出了5个常用的)

Python3中的68个内置函数总结

它们真的是Python内置函数吗?

Python - 内置函数

Python内置函数之enumerate() 函数

python内置方法总结