10 月 222020
 
# -*- coding:utf-8 -*-
# 数据结构,包括列表lists,字典dictionaries,元组tuples,集合sets
# https://docs.python.org/3/tutorial/datastructures.html

Weekday = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(Weekday[0])

# 列表中的每一个元素都是可变的
# 列表中的元素是有序的,也就是每一个元素都有一个位置
# 列表可以容纳Python中的任何对象

all_in_list = [
    1,  # 整数(integer numbers)
    1.0,  # 浮点数(floating point numbers)
    'a word',  # 字符串(strings)
    print(168),  # 函数(functions)
    True,  # 布尔值(boolean value)
    [1, 2],  # 列表中套列表(lists)
    (1, 2),  # 元组(tuples)
    {'key': 'value'}  # 字典(dictionaries)
]

# 列表插入,插入元素(item)的位置(position)是指定元素位置之前的位置
fruit = ['pineapple', 'pear']
fruit.insert(1, 'grape')  # 插入方法method
print(fruit)

# 列表插入的另一种方法
fruit[0:0] = ['Orange']  # 列表最前面,列表索引
print(fruit)

# 列表删除
cars = ['benz', 'bmw', 'audi', 'byd']
cars.remove('bmw')  # 删除方法method
print(cars)

# 替换修改
cars[0] = 'ford'  # 替换第1个元素benz,列表索引
print(cars)

# 列表删除的另一种方法是,使用del关键字
del cars[0:2]  # 删除第1-2个元素ford和audi,列表索引
print(cars)

# 字符串分片和列表索引,都支持正反两种索引方式
# 正向索引编号Positive Index Number和反向索引编号Negative Index Number
# https://www.digitalocean.com/community/tutorials/how-to-index-and-slice-strings-in-python-3

# 元素周期表(列表索引)
periodic_table = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne']
print(periodic_table[0])  # H
print(periodic_table[-2])  # F
print(periodic_table[0:3])  # H He Li
print(periodic_table[-10:-7])  # H He Li
print(periodic_table[-10:])  # 打印全部
print(periodic_table[:9])  # H - F 打印第1-8个元素
C:\Users\harveymei\PycharmProjects\hellopython\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/hellopython/datastructure.py
Monday
168
['pineapple', 'grape', 'pear']
['Orange', 'pineapple', 'grape', 'pear']
['benz', 'audi', 'byd']
['ford', 'audi', 'byd']
['byd']
H
F
['H', 'He', 'Li']
['H', 'He', 'Li']
['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne']
['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F']

Process finished with exit code 0

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)