Python len函數 - Python零基礎入門教程


目錄

零基礎 Python 學習路線推薦 : Python 學習目錄 >> Python 基礎入門

Python 中除了 print 函數之外,len 函數和 type 函數應該算是使用最頻繁的 API 了,操作都比較簡單。

一.Python len 函數簡介

返回對象的長度(項目數)參數可以是序列(例如字符串 str** 、元組 tuple** 、列表 list )或集合(例如字典 dict** 、集合 set凍結集合 frozenset ),語法如下:**

'''
參數:
    s – 對象或者序列(例如字符串str、元組tuple、列表list)或集合(例如字典dict、集合set或凍結集合)

返回值:返回長度(>=0)
'''

len(s)

二.Python len 函數使用

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python len 函數.py
@Time:2021/04/29 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""


a = (1,2,3,4,5,6) #元組
b = [1,2,3,4] #列表
c = range(0,11) #range
d = {'name':'lisi','age':14} #字典
e = 'helloworld' #字符串
f = {1,2,3,4,5} #集合
g = frozenset([1,2,3,4,5,8]) #凍結集合

print("a:",len(a))
print("b:",len(b))
print("c:",len(c))
print("d:",len(d))
print("e:",len(e))
print("f:",len(f))


‘’‘
輸出結果:

a: 6
b: 4
c: 11
d: 2
e: 10
f: 5
’‘’

三.猜你喜歡

  1. Python for 循環
  2. Python 字符串
  3. Python 列表 list
  4. Python 元組 tuple
  5. Python 字典 dict
  6. Python 條件推導式
  7. Python 列表推導式
  8. Python 字典推導式
  9. Python 函數聲明和調用
  10. Python 不定長參數 *argc/**kargcs
  11. Python 匿名函數 lambda
  12. Python return 邏輯判斷表達式
  13. Python 字符串/列表/元組/字典之間的相互轉換
  14. Python 局部變量和全局變量
  15. Python type 函數和 isinstance 函數區別
  16. Python is 和 == 區別
  17. Python 可變數據類型和不可變數據類型
  18. Python 淺拷貝和深拷貝

未經允許不得轉載:猿說編程 » Python len 函數

本文由博客 - 猿說編程 猿說編程 發布!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM