Python API len函數操作過程解析
在python中除了print函數之外,len函數和type函數應該算是使用最頻繁的API了,操作都比較簡單。
一.len函數簡介
返回對象的長度(項目數)參數可以是序列(例如字符串str、元組tuple、列表list)或集合(例如字典dict、集合set或凍結集合frozenset)
語法:
len(s)
參數:
s ? 對象或者序列(例如字符串str、元組tuple、列表list)或集合(例如字典dict、集合set或凍結集合)
返回值:返回長度(>=0)
二.len函數使用
# !usr/bin/env python# -*- coding:utf-8 _*-'''@Author:何以解憂@Blog(個人博客地址): shuopython.com@WeChat Official Account(微信公眾號):猿說python@Github:www.github.com @File:python_len.py@Time:2020/1/16 21:25 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!'''a = (1,2,3,4,5,6) #元組b = [1,2,3,4] #列表c = range(0,11) #ranged = {’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: 6b: 4c: 11d: 2e: 10f: 5
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
