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 - Control Flow

[!WARNING] Indentation matters

if else statements

Syntaxe

if condition:
    # execute if conddition is True
elif other_condition:
    # execute if other_condition is True
else:
    # execute if none of the previous conditions are True

Example

number = 20

if number < 12:
    print("Number is inferior to 12")
elif 12 <= age < 18:
    print("Number is superior or equal to 12 but inferior to 18")
else:
    print("Number is superior or equal to 18")

and, or, not logical operators

see: Operators

number = 1
boolean = True

if number > 0 and boolean:
  print('conddition is True')
else:
  print('conddition is False')

if number > 5 or boolean:
  print('conddition is True')
else:
  print('conddition is False')

if not boolean:
  print('conddition is True')
else:
  print('conddition is False')

if (number > 5 or number == 1) and not boolean:
  print('conddition is True')
else:
  print('conddition is False')

# Outputs
  
conddition is True
conddition is True
conddition is False
conddition is False

Ternary operators

my_var = 'hello' if 3 - 5 > 0 else 'bye'
# Inside a list comprehension
my_list = ['pair' if n % 2 == 0 else n for n in range(1, 101)]

Loops

Loops allow to iterate a code block till a specific condition is fulfilled.

What is an iteration ?

An iteration is a run through a code block in a loop. Each iteration, the loop execute the code block till the stop condition is fulfilled or all the values have been ran through.

For loop

Used if you know how many iterations you need.

for variable in sequence:
  # Executed code

Using range()

# iterate from 0 to 4
for i in range(5): print(i)

when should a for loop be used

  • When you need to run through a sequence (Lists, tuples, string...)
  • When you have a known iteration number or a fixed interval
  • When you need to iterate through values without checking for a condition

How does an iteration work

my_array = ['hello', 'world', '!']
for word in my_array:
  print(word)
  • Each iteration the next value of the list is assigned to the variable word then print(fruit) is executed.
  • The loop goes on till all of the values have been used

Different ways of defining a for loop

# Lists
for value in [1, 2, 3, 4]:
  print(value)

# String
for character in 'Hello':
  print(character)

# Dictionary
my_dict = {'name': 'Joe', 'age': 26}
for key, value in my_dict.items():
  print(key, value)

# Range with a step
for i in range(0, 10, 2): # Iteration with a step of 2 
  print(i)

# Returning a value in a list
my_array = [x for x in [1, 2, 3, 4]] # can be useful if you need to format the values of a list

For the last example see: List Comprehensions

Other operations with for loops

  • break: stop and exit the loop immediately
  • continue: skip the current iteration and goes to the next
  • pass: stub to allow the loop to run even when empty
for i in range(5):
  if i == 3:
    break # exit the loop when i equals 3
  print(i)  

for i in range(5):
  if i == 2:
    continue # go to the next iteration when i equals 2
  print(i)

For / Else

else define a block of code that should be executed when the loop ends, if the loop is stopped by a break the else block will not be executed

for number in range(2, 100):
  for factor in range(2, int(number**0.5) + 1):
    if number % factor == 0:
      break
  else:
    print(f'{number} is prime')

While loop

Executed till a condition is true, in opposition to for loops it iterate without knowing how many iterations will be needed.

while condition:
  # Executed code

Example:

i = 0
while i < 5:
  i += 1
  • The loop iterate while i value is inferior to 5
  • Each iteration i is incremented by 1
  • The loop stops when i reaches 5

when should a while loop be used

  • When you don't know how many times you need to iterate
  • When you need the iteration to continue till a condition becomes falsy
  • When you need manual control for stopping a loop

Stop condition in a while loop

# In this example the user needs to guess a number with limited retries
chances = 3
num_to_guess = 50

user_input = int(input("number"))
chances -= 1

while chances > 0 and user_input != num_to_guess:
  chances -= 1
  user_input = int(input("number"))

if user_input == num_to_guess:
  print("gg")
else: 
  print("failed")

# You could also do

chances = 10
num_to_guess = 50
while chances > 0:
  user_input = int(input("number"))
  chances -= 1
  if user_input == num_to_guess:
    print("GG")
    break

# But you should avoid relying to much on "break" for readibility
ยฉ 2024 d0z.eu | 0.1.0-beta.39