180
181 # estimating coefficients
182 b = estimate_coef(x, y)
183 element = "Estimated coefficients:\nb_0 = {} \184 \nb_1 = {}".format(b[0] , b[1])
185 print(element)
186 with open('Log.txt', 'a') as f:
47
48 # estimating coefficients
49 b = estimate_coef(x, y)
50 print("Estimated coefficients:\nb_0 = {} \51 \nb_1 = {}".format(b[0], b[1]))
52
53 # plotting regression line
215 snake = Snake(s, genome=None)
216 fitness, score = snake.run()
217
218 print('Fitness: %s, Score: %s' % (fitness, score))
28
29 genome.fitness = fitness
30
31 print('Generation #%s, Genome #%s, Fitness: %s, Score: %s' % (n_gen, i, fitness, score))32
33 if best_genomes is not None:
34 genomes.extend(best_genomes)
34 genomes.extend(best_genomes)
35 genomes.sort(key=lambda x: x.fitness, reverse=True)
36
37 print('===== Generaton #%s\tBest Fitness %s =====' % (n_gen, genomes[0].fitness))38 # print(genomes[0].w1, genomes[0].w2)
39
40 best_genomes = deepcopy(genomes[:N_BEST])
f-strings are the fastest way to format strings as compared to the following methods:
%
format()
str.join
+
operator to concatinate stringTemplate.substitute
Some less preferred ways to format strings are the following:
from string import Template
menu = ('eggs', 'spam', 42.4)
old_order = "%s and %s: %.2f ¤" % menu # [consider-using-f-string]
beginner_order = menu[0] + " and " + menu[1] + ": " + str(menu[2]) + " ¤"
joined_order = " and ".join(menu[:2])
format_order = "{} and {}: {:0.2f} ¤".format(menu[0], menu[1], menu[2])
named_format_order = "{eggs} and {spam}: {price:0.2f} ¤".format(eggs=menu[0], spam=menu[1], price=menu[2])
template_order = Template('$eggs and $spam: $price ¤').substitute(eggs=menu[0], spam=menu[1], price=menu[2])
Consider using f-strings as shown below:
menu = ('eggs', 'spam', 42.4)
f_string_order = f"{menu[0]} and {menu[1]}: {menu[2]:0.2f} ¤"