len()方法返回列表中的元素的数量。
语法
以下是 len() 方法的语法-
len(list)
参数
-
list -- 这是要被计算元素个数的列表
返回值
此方法返回列表中的元素的数量。
示例
下面的示例演示 len() 方法的使用。
#!/usr/bin/python3 list1 = ['physics', 'chemistry', 'maths'] print (len(list1)) list2=list(range(5)) #creates list of numbers between 0-4 print (len(list2))
当我们运行上面的程序,会产生以下结果 -
3 5
上一篇:
Python3字符串
下一篇:
Python3列表