Learn to program: The Fundamentals y Learn to program: Crafting Quality Code son dos cursos para aprender a programar en Python. Los instructores son bastante simpáticos (sobre todo Paul Gries, que es como una versión nerd de Kenneth Branagh), y entre los dos cursos repasan las principales features de Python (3).
Los videos muestran a los profes trabajando en IDLE (un IDE de Python), ya sea mostrando los fundamentos (loops, listas, diccionarios, etc), o a veces escribiendo programas enteros.
Si bien hace tiempo que conozco los statements básicos de algunos lenguajes (Visual Basic, R, Python) me cuesta mucho hacer programas complicados. Estos cursos explican una metodología para desarrollar programas que me resulta bastante útil para solucionar este problema (escribir pseudo-código, doc strings y ejemplos para cada procedimiento).
Este programa toma un archivo con las notas de uno de los assignments del curso y escribe un histograma en un archivo de texto. Paul lo escribe completo en un video de 34 minutos.
Este es el programa principal:
import tkinter.filedialog
import grades
a1_filename = tkinter.filedialog.askopenfilename()
a1_file = open(a1_filename,'r')
a1_histfilename = tkinter.filedialog.asksaveasfilename()
a1_histfile = open(a1_histfilename,'w')
# read the grades into a list
grades_list = grades.read_grades(a1_file)
# count the number of grades per range
counts = grades.count_grade_ranges(grades_list)
# write the histogram to the file the user picks
grades.write_histogram(counts, a1_histfile)
# close files
a1_histfile.close()
a1_file.close()
Y este es otro archivo son las funciones «ayudantes» que fuimos desarrollando:
def read_grades(gradefile):
'''(file open reading) -> list of float
Read and return the list of grades in the gradefile
Precondition: gradefile starts with a header that contains no blank lines
and then lines containting student id and grades
'''
# skip over the header
line = gradefile.readline()
while line != '\n':
line = gradefile.readline()
# read each line into a list
grades = []
line = gradefile.readline()
while line != '':
grades.append(float(line[line.rfind(" ") + 1:]))
#print(line[line.rfind(" ") + 1:])
line = gradefile.readline()
return grades
def count_grade_ranges(grades_list):
'''(list of float) -> list of int
0-9: index 0
10 - 19: 1
:
100 - 10
>>>count_grade_ranges([77.5, 37.5, 0.5, 9.5, 72.5, 100.0, 55.0, 70.0, 79.5])
[2, 0, 0, 1, 0, 1, 0, 4, 0, 0,1]
'''
range_counts = [0] * 11
for grade in grades_list:
which_count = int(grade // 10)
range_counts[which_count] = range_counts[which_count] + 1
return range_counts
def write_histogram(range_counts, histfile):
'''(list of counts, file open for writing) -> NoneType
Writes a histogram of the grades count to the file opened for writing
Output Format:
0-9: **
10-19: *
20-29: *
30-39: ***
40-49: ******
50-59: ********
60-69: **
70-79: *****
80-89: *********
90-99: ****
100: *
'''
histfile.write('0-9: ')
histfile.write('*' * range_counts[0])
histfile.write('\n')
for i in range(1,10):
start = i * 10
end = start + 9
histfile.write(str(start) + '-' + str(end) + ': ')
histfile.write('*' * range_counts[i])
histfile.write('\n')
histfile.write('100: ')
histfile.write('*' * range_counts[10])
Y el resultado final, que puede no ser bonito, pero tiene ese sabor único de lo hecho en casa:
0-9: **
10-19:
20-29:
30-39: *
40-49: **
50-59: *
60-69: ********
70-79: **********
80-89: *******
90-99: **
100: *
Bueno, espero poder seguir el camino hacia la python-guru-dom de la mano de estos canadienses (y, algunos materiales de otros lados por supuesto), Si bien The Fundamentals parece bastante básico, Crafting Quality Code tiene videos de testing, OOP, y otros temas interesantes.