PYTHON

  minte9
REMEMBERS




Mutable

p163 Unlike strings, lists are mutable.
 
# A list contains multiple values in an ordered sequence
#
# Unlike strings, list are mutable, they can be changes

numbers = [1, 2]
numbers[1] = 3

assert numbers == [1, 3]
assert numbers != [1, 2]

Concat

p167 You can use the + operator concatenate lists.
 
# To concatenate two list use + operator

a = [1, 2, 3]
b = [4, 5, 6]
c = a + b

assert c == [1, 2, 3, 4, 5, 6]
assert c != [1, 2, 7, 5, 6]
assert c != [1, 2, 3]

# To multiply a list use * operator

a = [2]
b = [2] * 4

assert b == [2, 2, 2, 2]
assert b != 8
assert b != [ [2], [2], [2], [2] ]

Slice

The slice operator also works on lists.
 
# The slice operator [:] works on list
# (same as with strings)
#
# The value -1 refers to the last index in a list

word = "abcde"

assert word[:1] == "a"
assert word[1:] == "bcde"
assert word[1:3] == "bc"    # 3 limit, not included


list = ["a", "b", "c", "d", "e"]

assert list[:1] != "a"
assert list[:1] == ["a"]
assert list[1:] == ["b", "c", "d", "e"]
assert list[1:3] == ["b", "c"]

assert list[-1] == "e"  # last

Append

p168 You can append the list.
 
# To add an element to a list use append()
# To add a list to another list use extend()
#
# The del statement removes values at the index in a list

A = ['a', 'b', 'c']
A.append('x')

assert A != ['a', 'b', 'c']
assert A == ['a', 'b', 'c', 'x']

B = ['d', 'e']
A.extend(B)

assert A != ['a', 'b', 'c']
assert A == ['a', 'b', 'c', 'x', 'd', 'e']

del A[3]

assert A == ['a', 'b', 'c', 'd', 'e']

Sorted

Display a sorted list of installed python modules.
 
"""Display all installed python modules
CLI commands examples: 
pip list
pip list --outdated
pip show pyperclip
pip install pyperclip
"""
import pkg_resources

pkgs = pkg_resources.working_set
pkgs = sorted(['%s \t %s' % (k.key, k.version) for k in pkgs])

print('\n'.join(pkgs))
    # ...
    # ufw 0.36
    # unattended-upgrades 0.1
    # urllib3 1.25.8
    # usb-creator 0.3.7
    # wadllib 1.3.3
    # wheel 0.34.2
    # xkit 0.0.0

Questions     github Collections
Dictionaries
        A B C D E F
🔔
1/4