Python for Datawhale Task2

1. 列表

1.1 列表标志

列表(List)用[ ]标识,是Python中最通用的复合数据类型。列表可以完成大多数集合类的数据结构实现。它支持字符,数字,字符串甚至可以包含列表(即嵌套)。

1.2 列表的基本操作

  • 列表的创建
    >>> foods = ['apple','pizza','cake','肉肉']
    >>> foods = ['apple','pizza','cake','肉肉']
    >>> print(foods)
    ['apple', 'pizza', 'cake', '肉肉']

  • 创建数字列表
    >>> numbers = list(range(1,6))
    >>> even_numbers = list(range(2,11,2))
    >>> print(numbers,even_numbers)
    [1, 2, 3, 4, 5] [2, 4, 6, 8, 10]

  • 访问及修改列表元素
    >>> print(foods[0])
    apple
    >>> print(foods[-1])
    肉肉
    >>> foods[0] = 'candy'
    >>> print(foods)
    ['candy', 'pizza', 'cake', '肉肉']

  • 在列表中添加元素
    在末尾添加
    >>> foods.append('chicken')
    >>> print(foods)
    ['candy', 'pizza', 'cake', '肉肉', 'chicken']
    在任意位置插入
    >>> foods.insert(1,'noodles')
    >>> print(foods)
    ['candy', 'noodles', 'pizza', 'cake', '肉肉', 'chicken']

  • 从列表中删除元素
    使用del语句删除元素,可删除任意位置的元素,删除后无法访问删除的元素
    >>> del foods[0]
    >>> print(foods)
    ['noodles', 'pizza', 'cake', '肉肉', 'chicken']
    使用pop()方法删除元素。可删除列表末尾的元素,并能够接着使用被删除的元素。
    >>> popped_food = foods.pop()
    >>> print(foods,popped_food)
    ['noodles', 'pizza', 'cake', '肉肉'] chicken
    弹出列表中任意位置元素
    >>> first_food = foods.pop(0)
    >>> print(first_food)
    noodles
    根据值删除元素
    >>> print(foods)
    ['pizza', 'cake', '肉肉']
    >>> foods.remove('cake')
    >>> print(foods)
    ['pizza', '肉肉']

  • 列表的复制
    复制后为两个列表
    >>> my_foods = ['candy','cake','pizza','muffin']
    >>> her_foods = my_foods[:]
    >>> print(my_foods,her_foods)
    ['candy', 'cake', 'pizza', 'muffin'] ['candy', 'cake', 'pizza', 'muffin']
    >>> my_foods.append('ice cream')
    >>> her_foods.append('cannoli')
    >>> print(my_foods,her_foods)
    ['candy', 'cake', 'pizza', 'muffin', 'ice cream'] ['candy', 'cake', 'pizza', 'muffin', 'cannoli']
    不能使用=号直接复制,这样复制后仍然为同一个列表
    >>> her_foods = my_foods
    >>> my_foods.append('stake')
    >>> her_foods.append('chicken')
    >>> print(my_foods,her_foods)
    ['candy', 'cake', 'pizza', 'muffin', 'ice cream', 'stake', 'chicken'] ['candy', 'cake', 'pizza', 'muffin', 'ice cream', 'stake', 'chicken']

1.3 列表相关方法

  • 组织列表
    >>> print(my_foods)
    ['candy', 'cake', 'pizza', 'muffin', 'ice cream', 'stake', 'chicken']
    >>> my_foods.sort() #使用sort对列表进行永久性排序(按字母顺序排列)
    >>> print(my_foods)
    ['cake', 'candy', 'chicken', 'ice cream', 'muffin', 'pizza', 'stake']
    >>> my_foods.sort(reverse=True) #倒序
    >>> print(my_foods)
    ['stake', 'pizza', 'muffin', 'ice cream', 'chicken', 'candy', 'cake']

  • 获取列表长度
    >>> len(my_foods)
    7

  • 遍历列表
    >>> for food in my_foods:
    . print(food)
    stake
    pizza
    muffin
    ice cream
    chicken
    candy
    cake

  • 列表解析
    >>> squares = [value**2 for value in range(1,11)]
    >>> print(squares)
    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

  • 列表切片
    >>> part_my_foods = my_foods[1:4]
    >>> print(part_my_foods)
    ['pizza', 'muffin', 'ice cream']

2. 元组

2.1 元组标志

元组用 () 标识。内部元素用逗号隔开。但是元组不能二次赋值,相当于只读列表。

2.2 元组基本操作

  • 元组的创建
    >>> numbers = (1,2,3,4,5)
    >>> print(numbers)
    (1, 2, 3, 4, 5)

  • 元组的不可变性
    >>> numbers[0] = 6
    Traceback (most recent call last):
    File “”, line 1, in
    TypeError: ‘tuple’ object does not support item assignment

  • 遍历元组中所有值
    >>> for number in numbers:
    ... print(number)
    1
    2
    3
    4
    5

3. string字符串

3.1 字符串定义及基本操作

  • 字符串定义
    字符串或串(String)是由数字、字母、下划线组成的一串字符。
  • 字符串基本操作
    字符串读取
    >>> str_1 = 'abcdef'
    >>> print(str_1[0]) #从左到右索引
    a
    >>> print(str_1[-1]) #从右到左索引
    f
    >>> print(str_1[1:3]) #获取一段字符串
    bc
    字符串拼接(+)
    >>> first_name = 'Lee'
    >>> last_name = 'andy'
    >>> full_name = first_name + ' ' + last_name
    >>> print(full_name)
    Lee andy
    字符串重复操作(*)
    >>> print(full_name * 3)
    Lee andyLee andyLee andy

3.2 字符串相关方法

  • 获取字符串长度,即字符串包含的字符数
    >>> str_2 = 'dejufe'
    >>> len(str_2)
    6
  • 单个字符编码操作
    ``>>> ord(‘A’) #函数获取字符的整数表示
    65
    >>> ord('上')
    19978
    >>> chr(67) #把编码转换为对应的字符
    'C'

4. 字符串格式化

  • %运算符
    字符串格式化采用%实现,在字符串内部,%s表示用字符串替换,%d表示用整数替换,有几个%?占位符,后面就跟几个变量或者值,顺序要对应好。如果只有一个%?,括号可以省略。
    >>> 'hi, %s, your score is %d.'%('lee',99)
    'hi, lee, your score is 99.'
    常用的占位符有
占位符替换内容
%d整数
%f浮点数
%s字符串
%x十六进制整数
  • format()
    另一种格式化字符串的方法是使用字符串的format()方法,它会用传入的参数依次替换字符串内的占位符{0}、{1}……,不过这种方式写起来比%要麻烦得多:
    >>> 'hello, {0}, your score is {1:.1f}%'.format('lee',89.998)
    'hello, lee, your score is 90.0%'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值