Course materials · Microprocessor Applications

3. Working with data collections

English translation

Contents

3.1 Lists

list_mixed = [0, 2.5, 'abc']
# Create an empty list
empty_list = []
print(empty_list)
[]
# Create by entering values directly
list_manual = [0, 1, 2, 3, 4]
print(list_manual)
[0, 1, 2, 3, 4]
# Create using range (A, B, C below are all integers)
# range(B) - create a list from 0 in steps of 1 up to integers less than B
list_new = list(range(10))
print(list_new)

# range(A,B) - create a list starting at A in steps of 1 up to integers less than B
list_new = list(range(1,10))
print(list_new)

# range(A,B,C) - create a list starting at A in steps of C up to integers less than B (reverse for negative C)
list_new = list(range(2,10,2))
print(list_new)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6, 8]
# + and * operations on lists
list_manual = [0, 1, 2, 3, 4]
list_new = list(range(4,-1,-1))
print(list_manual+list_new)
print(list_manual*2)
[0, 1, 2, 3, 4, 4, 3, 2, 1, 0]
[0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
# Append an element to the end of a list
list_manual = [0, 1, 2, 3, 4]
list_manual.append(5)
print(list_manual)
[0, 1, 2, 3, 4, 5]
# Add an element at a specified position in a list
# insert(position, value) Note that the starting position is 0
list_manual.insert(2, 10)
print(list_manual)
[0, 1, 10, 2, 3, 4, 5]
# Remove an element from the end of a list
list_manual.pop()
print(list_manual)
[0, 1, 10, 2, 3, 4]
# Remove an element at a specified position in a list
# pop(position)
list_manual.pop(2)
print(list_manual)
[0, 1, 2, 3, 4]
# Count the elements in a list
num_data = len(list_manual)
print(num_data)
5
# Find an element's position (return the first occurrence)
position = list_manual.index(2)
print(position)
2
# Sort a list by value (ascending by default)
list_manual.sort()
print(list_manual)
list_manual.sort(reverse=True)
print(list_manual)
[0, 1, 2, 3, 4]
[4, 3, 2, 1, 0]
# Select a desired segment (slicing)
# Select and return one element
print(list_manual[2])
2
# [A:B] Select positions from A up to, but excluding, B
# [2:4] = select positions 2,3
print(list_manual[2:4])
[2, 1]
# [A:] From position A to the end
# [:B] Select from the start up to, but excluding, B
print(list_manual[2:])
print(list_manual[:4])
[2, 1, 0]
[4, 3, 2, 1]
# [-A] The Ath element from the end
print(list_manual[-2])
1

3.2 Tuples

# Create an empty tuple = effectively pointless, since it cannot be modified.
empty_tuple = ()
print(empty_tuple)
()
# Create by entering values directly
tuple_manual = (0, 1, 2, 3, 4)
print(tuple_manual)
(0, 1, 2, 3, 4)
# Create using range (A, B, C below are all integers)
print(tuple(range(2,10,2)))
(2, 4, 6, 8)
len(tuple_manual)
5
tuple_manual.index(4)
4
tuple_manual[1:]
(1, 2, 3, 4)

3.3 Sets

# Create an empty set
empty_set = {}
print(empty_set)
{}
# Create by entering values directly - duplicates are removed.
set_manual = {0, 1, 2, 3, 4, 4, 3, 2, 1, 0}
print(set_manual)
{0, 1, 2, 3, 4}
# Create using range (A, B, C below are all integers)
print(set(range(2,10,2)))
{8, 2, 4, 6}
# Intersection '&'
set_first = {1, 3, 5}
set_second = {1, 2, 4, 8}
print(set_first&set_second)
{1}
# Union '|'
print(set_first|set_second)
{1, 2, 3, 4, 5, 8}
# Difference '-'
print(set_first-set_second)
{3, 5}
# Check whether it is a subset
# A.issuperset(B) - whether B is a subset of A (whether A contains B)
set_third = {1, 3, 5, 7}
set_third.issuperset(set_first)
True
# Check membership
3 in set_first
True

3.4 Dictionaries

  1. Add an item
  2. Add an item

is managed.

my_dictionary = {'name':'John', 'age':15, 'height': 1.76}
print(my_dictionary)
{'name': 'John', 'age': 15, 'height': 1.76}
my_dictionary['name']
'John'
first_user = {'name':'John', 'age':15, 'height':1.76, 'position':[15,3]}
second_user = {'name':'Paul', 'age':17, 'height':1.88, 'position':[-2,7]}
users = [first_user, second_user]
print(users[0]['name'])
John