python for beginners_《Python for Beginners》学习笔记(5)

本文介绍了LearnStreet上的Python入门课程中关于Lists和Tuples的内容。包括计算列表长度、移除列表元素、向列表添加元素、连接列表、创建元组以及比较列表和元组。通过实例展示了如何使用pop、append、concatenate等方法,并强调了元组的不可变性。此外,还提供了一个复习练习,比较列表和元组内部元素的相等性。
摘要由CSDN通过智能技术生成

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

Lesson 6 Lists and Tuples

1. Length of List

计算List变量的长度。

1 defrun(var):2 #return your code here

3 returnlen(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 defrun(first, second):2 #your code here

3 return first +second4

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 defrun():2 #your code here

3 name = ("Guinevere", 9102)4 returnname5

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 defrun(lst, tup):3 #your code here

4 if lst[1] == tup[1]:5 return tuple(lst) ==tup6 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类型,可以进行比较。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值