目錄
零基礎 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
’‘’
三.猜你喜歡
- Python for 循環
- Python 字符串
- Python 列表 list
- Python 元組 tuple
- Python 字典 dict
- Python 條件推導式
- Python 列表推導式
- Python 字典推導式
- Python 函數聲明和調用
- Python 不定長參數 *argc/**kargcs
- Python 匿名函數 lambda
- Python return 邏輯判斷表達式
- Python 字符串/列表/元組/字典之間的相互轉換
- Python 局部變量和全局變量
- Python type 函數和 isinstance 函數區別
- Python is 和 == 區別
- Python 可變數據類型和不可變數據類型
- Python 淺拷貝和深拷貝
未經允許不得轉載:猿說編程 » Python len 函數
本文由博客 - 猿說編程 猿說編程 發布!