输出元素《Python for Beginners》学习笔记(5) 输出元素

本篇文章是一篇关于输出元素的帖子

    《Python for Beginners》为LearnStreet上的Python入门课程。本节要主学习内容为Lists and Tuples

    Lesson 6 Lists and Tuples

    1. Length of List
盘算List变量的度长。

1 def run(var):
2     #return your code here
3     return len(var)
4 
5 #This is just for you to see what happens when the function is called
6 print run([1,2,3,4,5])

输出结果:
5

    2. Removing Elements of a List
移除表列中的某个元素。

    days = ["funday","sunday","monday","tuesday","wednesday","thursday","friday"]
days.pop(0)
注意:pop(0)应用的是圆括号。

    3. Appending Lists
在表列尾末加添元素。
days.append("saturday")
其中,days为List型类变量。

    4. Concatenating Lists
连接两个表列。

1 def run(first, second):
2     #your code here
3     return first + second
4 
5 #This is just for you to see what happens when the function is called
6 print run([1,2,3],[4,5,6])

输出结果:
[1, 2, 3, 4, 5, 6]

    5. Tuples
建创tuple变量应用圆括号而不是方括号,tuple型类的变量值是不可变的。
例如:tup = (10, 20, 30)

    每日一道理
美丽是平凡的,平凡得让你感觉不到她的存在;美丽是平淡的,平淡得只剩下温馨的回忆;美丽又是平静的,平静得只有你费尽心思才能激起她的涟漪。
1 def run():
2     #your code here
3     name = ("Guinevere", 9102)
4     return name
5 
6 #This is just for you to see what happens when the function is called
7 print run()

 

    输出结果:
('Guinevere', 9102)

    6. 温习训练

 1 # assume lst is a list and tup is a tuple
 2 def run(lst, tup):
 3     #your code here
 4     if lst[1] == tup[1]:
 5         return tuple(lst) == tup
 6     else:
 7         return "not equal"
 8 
 9 #This is just for you to see what happens when the function is called
10 print run([1,2,3],(1,2,3))

输出结果:
True

    注意:型类转换tuple(lst)

    总结:
tuple和list是两种不同的数据型类,他们远永不会等相。但是他们外部的元素都是string型类,可以行进比拟。

文章结束给大家分享下程序员的一些笑话语录: 真正的程序员喜欢兼卖爆米花,他们利用CPU散发出的热量做爆米花,可以根据米花爆裂的速度听出正在运行什么程序。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值