if函数python_python之BIF函数在列表中的应用

1 Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64)] on win32

2 Type "copyright", "credits" or "license()" for more information.

3 >>> cast=["cleese","palin","jones","idle"]

4 >>> print(cast)

5 [""cleese"", ""palin"", ""jones"", ""idle""]

6 >>> print(len(cast))#显示数据项数量

7 4

8 >>> print(cast[1])#显示列表中第2个数据项的值

9 palin

10 >>> cast.append("gilliam")#在列表末尾添加一个数据项

11 >>> print(cast)

12 [""cleese"", ""palin"", ""jones"", ""idle"", ""gilliam""]

13 >>> cast.pop()#删除列表末尾的数据项

14 ""gilliam""

15 >>> print(cast)

16 [""cleese"", ""palin"", ""jones"", ""idle""]

17 >>> cast.extend(["gilliam","chapman"])#在列表末尾增长一个数据项凑集

18 >>> print(cast)

19 [""cleese"", ""palin"", ""jones"", ""idle"", ""gilliam"", ""chapman""]

20 >>> cast.remove("chapman")#删除指定的数据项

21 >>> print(cast)

22 [""cleese"", ""palin"", ""jones"", ""idle"", ""gilliam""]

23 >>> cast.(0,"chapman")#在指定的地位增长数据项

24 >>> print(cast)

25 [""chapman"", ""cleese"", ""palin"", ""jones"", ""idle"", ""gilliam""]

26 >>>

下面是讲定义一个def函数,isinstance()函数,for in,if else等的应用以及逻辑

1 movies=["the holy grail",1975,"terry jone & terry gilliam",91,

2 ["graham chapman",

3 ["michael palin","john cleese","terry gilliam",

4 "eric idle","terry jones"]]]

5 def print_something(the_list):#定义一种函数

6 for each_item in the_list:#for in轮回迭代处理惩罚列表,从列表肇端地位到末尾

7 if isinstance(each_item,list):#isinstance()检测each_item里每一项

8 #是不是list类型

9 print_something(each_item)#若是是,调用函数print_something

10 else:print(each_item)#若是不是,输出这一项

11

12 print_something(movies)#在movies列表中调用函数

13 """

14 之前if else语句不合错误齐导致报错

15 """

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值