Foreground
Foreground
D0Z - motion / graphic / dev๐Ÿงบ

Brain ๐Ÿ”’

โœ•

brain

CAUTION

Some files are private or not ready yet, if a link gives you a 404 that's why :)
This is a work in progress, new notes will be added daily

List of note Indexes


                .... |\| .|||.    |\/|  .|\\. .|.
              .\\||/|.  .\|..||  .\|..|||...||.....
            .|   .....||.|\\\\\\    ... |\\\/. .\\. .||
        .\\|   .|\\|..|\\ .\\\\  .\\|. .\\\\\|  |\| |\\|
        |\\  |\\|..  /\\\. |\|\\   \\\\|.   .|\\. .\\\\|
        .\\. |\\\\\\\|.. .\\|      .\\||\\/|. |\\|     .|/\|
      \. .\\\||/\|....   \\| |\/   |\\\  |\\\| .\\|  \\| ....
    |\/  |\\| |\\\\\\\| |\|.\\\   \\\\. |\\\\| |\\  \\.  ||..
      ..|||\|  .. .\\\\. |\\\\\\  .\\|....|\\\. |\\\\\\  |/. |/
    \\\|..   .\\\ |\\. .\\\| \\|   |||\\\.  ...|\\/||.  .\\\
  |\|  |\/.  /\\\\\| .\\\\. \\.   .../|.   .|\\\|.   |\\\|  |\\
    .  |||\\|.  ..||| .\\|.  |\|  \\|..../\\| |\\|  .\\\\\   ..
    \\|  .||\|.|..      |\\\|./\  \\||\. ||...    .\\\\/.  |\/||
    ..|/||.   |\\\\\|.     .|/\|  ||.        .||\\\     .|||. |\
  .\/|  \\\. .|\\\|||\\\||..       .|\\\\\\\|\\\\.  /\\\\||. |
    .|| |\\\\|  .......|\\\\\\.  .\\\. ..|||....  .\\\\|.. ...
    \\||....|\\\\\\\\||\\\\\\\/   |\|....|||\\\\\\\/....|\\\|
      ||.|\||\\| .....|\\\\..\\|    .|\\\. ||........ .|\|...
          . |\  /\\/\\\\\\. |\.  .\\\\\\.  ./\\\\/  |\\\||.
        .|\.|\| .|\|... ..              ./\\|..|\\|  ..
            .|\\|  .\\\\.\\\\\\\   \||...\\\|..|\\\|.||
                |\. ..||....||     ... ..||\\\. .|.
                        ...            .......

โ†‘ Python


Python - Lists

A list in python is a sequence of ordered elements (integer, float, string, ...). To define a list use brackets [] and separate its elements using a coma.

my_list = [1, 2, 3, "some text", 3.5, [1, 2, 3]]

Indexing

Elements of a list are indexed starting from 0, you can access any element using its index.

my_list = [1, 2, 3, "some text", 3.5]
print(my_list[1]) # Displays 2

You also can use negative indexes to access elements from the end of the list

print(my_list[-1]) # Displays 3.5

Slicing

Slicing allows to extract a part of a list using a range of indexes

# Syntax
my_list[start:stop:step]

# start: start index (included)
# stop: end index (excluded)
# step: optional

# Example
my_list = [1, 2, 3, "some text", 3.5]
print(my_list[1:3]) # Displays [2, 3]

# You can ommit the start index if its 0
print(my_list[:3]) # Displays [1, 2, 3]

# Same for the end index to get everything after the start index
print(my_list[3:]) # Displays ["some text", 3.5]

# You can use negative indexes
print(my_list[-2:]) # Displays ["some text", 3.5]

# Using a step
print(my_list[::2]) # Displays [1, 3, 3.5]
# Reverse the list
print(my_list[::-1]) # Displays [3.5, 'some text', 3, 2, 1]

Add elements

append

Add one element to the end of the list

my_list = [1, 2, 3]
my_list.append(4)
print(my_list)  # Displays [1, 2, 3, 4]

extend

Add multiple elements at the end of the list

my_list = [1, 2, 3]
my_list.extend([4, 5])
print(my_list)  # Displays [1, 2, 3, 4, 5]

insert

Insert one element at the specified index

my_list = [1, 2, 4]
my_list.insert(2, 3) # insert(<index>, <value>)
print(my_list)  # Displays [1, 2, 3, 4]

Remove elements

remove

Delete the first occurrence of the specified value

my_list = [1, 2, 3, 2]
my_list.remove(2)
print(my_list)  # Displays [1, 3, 2]

pop

Delete and return an element using the specified index (defaults to the last)

my_list = [1, 2, 3, 2]
my_list.pop(1)
print(my_list)  # Displays [1, 3, 2]
my_list.pop()
print(my_list)  # Displays [1, 3]

Copy

[!WARNING] you cannot use my_list_copy = my_list because my_list_copy will be a reference to my_list and the changes made to one of them will affect the other

my_list = [1, 2, 3]
my_list_copy = my_list.copy()
# or
my_list_copy = my_list[:]
# or 
my_list_copy = list(my_list)

Find

count

Count the number of occurrences of a value in the list

my_list = [1, 2, 2, 3]
print(my_list.count(2))  # Displays 2

index

Return the index of the first occurrence of a value

my_list = [1, 2, 2, 3]
print(my_list.index(2))  # Displays 1

min and max

Return the min or the max value of a list

my_list = [1, 2, 3]
print(min(my_list))  # Displays 1
print(max(my_list))  # Displays 3

Sort

Sort

my_list = [3, 1, 2]
print(my_list.sort()) # Displays [1, 2, 3]

Reverse

my_list = [1, 2, 3]
print(my_list.reverse()) # Displays [3, 2, 1]

Remove duplicates

my_list = ['a', 'b', 'c', 'a', 'c']
my_list = list(set(my_list)) # ['c', 'a', 'b']

List Comprehensions

Here comprehension doesn't means understanding but comprehensive, systematically listing everything out.

my_list = [1, 2, 3, 4, 5]
[item * 2 for item in my_list] # [2, 4, 6, 8, 10]

Filters

my_list = list(range(100))
filtered_list = [item for item in my_list if item % 10 == 0]
print(filtered_list) # [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

my_string = 'Hello world. This is python'

def clean_word(word):
ย  return word.replace('.', '').lower()

words_list = [clean_word(word) for word in my_string.split()] 
print(words_list) # ['hello', 'world', 'this', 'is', 'python']

words_list = [clean_word(word) for word in my_string.split() if len(word) < 5]
print(words_list) # ['this', 'is']

Nested

def clean_word(word):
ย  return word.replace('.', '').lower()
ย  
my_string = 'Hello world. This is python'
words_list = [[clean_word(word) for word in sentence.split()] for sentence in my_string.split('.')]
print(words_list) # [['hello', 'world'], ['this', 'is', 'python']]
ยฉ 2024 d0z.eu | 0.1.0-beta.39