<jupyter_start><jupyter_text>Loop com `for`<jupyter_code>### Soma dos termos de um P.A.

s = 0  # accumulator initialization
for i in range(1, 100, 2):
    s = s + i
print(s)<jupyter_output>2500<jupyter_text>Loop com `while`<jupyter_code>### Números de Fibonacci menores que um dado valor

x, y = 0, 1
while x < 1000:
    print(x)
    x, y = y, x + y  # multiple assignment<jupyter_output>0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987<jupyter_text>Loops and array operations<jupyter_code>import numpy as np

import time
start_time = time.time()

a = np.linspace(0, 32, 1e7)
print(a)
for i in range(len(a)):
    a[i] = a[i] * a[i]
print(a)

print("--- %s seconds ---" % (time.time() - start_time))
import numpy as np

import time
start_time = time.time()

a = np.linspace(0, 32, 1e7)
print(a)
a = a * a
print(a)

print("--- %s seconds ---" % (time.time() - start_time))<jupyter_output>[  0.00000000e+00   3.20000032e-06   6.40000064e-06 ...,   3.19999936e+01
   3.19999968e+01   3.20000000e+01]
[  0.00000000e+00   1.02400020e-11   4.09600082e-11 ...,   1.02399959e+03
   1.02399980e+03   1.02400000e+03]
--- 0.1344153881072998 seconds ---<jupyter_text>This illustrates an important point: **`for` loops are slow**. Array operations run *much faster* and are therefore to be preferred in any case where you have a choice. Sometimes finding an array operation that is equivalent to a loop can be difficult, especially for a novice. Nevertheless, doing so pays rich rewards in execution time. Moreover, the *array notation is usually simpler and clearer*, providing further reasons to prefer array operations over loops.  List comprehensions<jupyter_code>import time
start_time = time.time()

x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# construct a vector from the diagonal elements of this matrix

diag = []  #accumulator
for i in [0, 1, 2]:
    diag.append(x[i][i])

print(diag)

print("--- %s seconds ---" % (time.time() - start_time))
# List comprehensions provide a simpler, cleaner, and faster way of doing the same thing
import time
start_time = time.time()

diagLC = [x[i][i] for i in [0, 1, 2]]

print(diagLC)

print("--- %s seconds ---" % (time.time() - start_time))
[y * y for y in diagLC]
# extracting a row

x[1]
# extracting a column

c1 = [a[1] for a in x]
c1
# extracting a column

[x[i][1] for i in range(3)]
# list comprehension with conditional statement

y = [-5, -3, 1, 7, 4, 23, 27, -9, 11, 41]
[a for a in y if a % 3 == 0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13875404530744337, 'fraction_numerical': 0.09870550161812297, 'mean_word_length': 3.7375478927203063, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '60921', 'license': 'MIT', 'n_tokens_mistral': 1099, 'n_tokens_neox': 937, 'n_words': 326, 'path': 'chap6/chapter_6_examples.ipynb', 'repo_name': 'chicolucio-python-learning/introduction-to-python-for-science', 'size': '2472'}