@@ -33,19 +33,30 @@ Let's write a simple for loop that simulates what a kid might see during a
3333visit to the zoo:
3434
3535~~~
36- >>> animals = ['lion', 'tiger', 'crocodile', 'vulture', 'hippo']
37- >>> print(animals)
36+ animals = ['lion', 'tiger', 'crocodile', 'vulture', 'hippo']
37+ print(animals)
38+ ~~~
39+ {: .language-python}
40+
41+ ~~~
3842['lion', 'tiger', 'crocodile', 'vulture', 'hippo']
43+ ~~~
44+ {: .output}
3945
40- >>> for creature in animals:
41- ... print(creature)
46+ ~~~
47+ for creature in animals:
48+ print(creature)
49+ ~~~
50+ {: .language-python}
51+
52+ ~~~
4253lion
4354tiger
4455crocodile
4556vulture
4657hippo
4758~~~
48- {: .language-python }
59+ {: .output }
4960
5061The line defining the loop must start with ` for ` and end with a colon, and the
5162body of the loop must be indented.
@@ -56,15 +67,26 @@ anything we like. After the loop finishes, the loop variable will still exist
5667and will have the value of the last entry in the collection:
5768
5869~~~
59- >>> animals = ['lion', 'tiger', 'crocodile', 'vulture', 'hippo']
60- >>> for creature in animals:
61- ... pass
70+ animals = ['lion', 'tiger', 'crocodile', 'vulture', 'hippo']
71+ for creature in animals:
72+ pass
73+ ~~~
74+ {: .language-python}
75+
76+ ~~~
77+ ~~~
78+ {: .output}
6279
63- >>> print('The loop variable is now: ' + creature)
64- The loop variable is now: hippo
80+ ~~~
81+ print(' The loop variable is now: ' + creature)
6582~~~
6683{: .language-python}
6784
85+ ~~~
86+ The loop variable is now: hippo
87+ ~~~
88+ {: .output}
89+
6890We are not asking Python to print the value of the loop variable anymore, but
6991the for loop still runs and the value of ` creature ` changes on each pass through
7092the loop. The statement ` pass ` in the body of the loop just means "do nothing".
@@ -88,9 +110,9 @@ Let's start by making a new directory inside the folder `data` to store all of
88110these files using the module ` os ` :
89111
90112~~~
91- import os
113+ import os
92114
93- os.mkdir('data/yearly_files')
115+ os.mkdir('data/yearly_files')
94116~~~
95117{: .language-python}
96118
@@ -168,20 +190,25 @@ but we want only unique years, which we can get using the `unique` method
168190which we have already seen.
169191
170192~~~
171- >>> surveys_df['year'].unique()
193+ surveys_df['year'].unique()
194+ ~~~
195+ {: .language-python}
196+ ~~~
172197array([1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987,
173198 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
174199 1999, 2000, 2001, 2002], dtype=int64)
175200~~~
176- {: .language-python }
201+ {: .output }
177202
178203Putting this into our for loop we get
179204
180205~~~
181- >>> for year in surveys_df['year'].unique():
182- ... filename='data/yearly_files/surveys' + str(year) + '.csv'
183- ... print(filename)
184- ...
206+ for year in surveys_df['year'].unique():
207+ filename='data/yearly_files/surveys' + str(year) + '.csv'
208+ print(filename)
209+ ~~~
210+ {: .language-python}
211+ ~~~
185212data/yearly_files/surveys1977.csv
186213data/yearly_files/surveys1978.csv
187214data/yearly_files/surveys1979.csv
@@ -209,7 +236,7 @@ data/yearly_files/surveys2000.csv
209236data/yearly_files/surveys2001.csv
210237data/yearly_files/surveys2002.csv
211238~~~
212- {: .language-python }
239+ {: .output }
213240
214241We can now add the rest of the steps we need to create separate text files:
215242
@@ -236,7 +263,7 @@ just created to confirm that everything worked as expected.
236263Notice that the code above created a unique filename for each year.
237264
238265~~~
239- filename = 'data/yearly_files/surveys' + str(year) + '.csv'
266+ filename = 'data/yearly_files/surveys' + str(year) + '.csv'
240267~~~
241268{: .language-python}
242269
@@ -310,14 +337,25 @@ it is called, it includes a return statement at the end.
310337This is how we call the function:
311338
312339~~~
313- >>> product_of_inputs = this_is_the_function_name(2,5)
340+ product_of_inputs = this_is_the_function_name(2,5)
341+ ~~~
342+ {: .language-python}
343+
344+ ~~~
314345The function arguments are: 2 5 (this is done inside the function!)
346+ ~~~
347+ {: .output}
315348
316- >>> print('Their product is:', product_of_inputs, '(this is done outside the function!)')
317- Their product is: 10 (this is done outside the function!)
349+ ~~~
350+ print(' Their product is:', product_of_inputs, ' (this is done outside the function!)' )
318351~~~
319352{: .language-python}
320353
354+ ~~~
355+ Their product is: 10 (this is done outside the function!)
356+ ~~~
357+ {: .output}
358+
321359> ## Challenge - Functions
322360>
323361> 1 . Change the values of the arguments in the function and check its output
@@ -448,31 +486,31 @@ values (here, `all_data`) is a required argument and MUST come before the
448486argument with default values (which are optional in the function call).
449487
450488~~~
451- def yearly_data_arg_test(all_data, start_year = 1977, end_year = 2002):
452- """
453- Modified from yearly_data_csv_writer to test default argument values!
489+ def yearly_data_arg_test(all_data, start_year = 1977, end_year = 2002):
490+ """
491+ Modified from yearly_data_csv_writer to test default argument values!
454492
455- start_year --- the first year of data we want --- default: 1977
456- end_year --- the last year of data we want --- default: 2002
457- all_data --- DataFrame with multi-year data
458- """
493+ start_year --- the first year of data we want --- default: 1977
494+ end_year --- the last year of data we want --- default: 2002
495+ all_data --- DataFrame with multi-year data
496+ """
459497
460- return start_year, end_year
498+ return start_year, end_year
461499
462500
463- start,end = yearly_data_arg_test (surveys_df, 1988, 1993)
464- print('Both optional arguments:\t', start, end)
501+ start,end = yearly_data_arg_test (surveys_df, 1988, 1993)
502+ print('Both optional arguments:\t', start, end)
465503
466- start,end = yearly_data_arg_test (surveys_df)
467- print('Default values:\t\t\t', start, end)
504+ start,end = yearly_data_arg_test (surveys_df)
505+ print('Default values:\t\t\t', start, end)
468506~~~
469507{: .language-python}
470508
471509~~~
472- Both optional arguments: 1988 1993
473- Default values: 1977 2002
510+ Both optional arguments: 1988 1993
511+ Default values: 1977 2002
474512~~~
475- > > {: .output}
513+ {: .output}
476514
477515The "\t" in the ` print ` statements are tabs, used to make the text align and be
478516easier to read.
@@ -482,35 +520,35 @@ function so that it looks for the start and end years in the dataset if those
482520dates are not provided:
483521
484522~~~
485- def yearly_data_arg_test(all_data, start_year = None, end_year = None):
486- """
487- Modified from yearly_data_csv_writer to test default argument values!
523+ def yearly_data_arg_test(all_data, start_year = None, end_year = None):
524+ """
525+ Modified from yearly_data_csv_writer to test default argument values!
488526
489- start_year --- the first year of data we want --- default: None - check all_data
490- end_year --- the last year of data we want --- default: None - check all_data
491- all_data --- DataFrame with multi-year data
492- """
527+ start_year --- the first year of data we want --- default: None - check all_data
528+ end_year --- the last year of data we want --- default: None - check all_data
529+ all_data --- DataFrame with multi-year data
530+ """
493531
494- if start_year is None:
495- start_year = min(all_data.year)
496- if end_year is None:
497- end_year = max(all_data.year)
532+ if start_year is None:
533+ start_year = min(all_data.year)
534+ if end_year is None:
535+ end_year = max(all_data.year)
498536
499- return start_year, end_year
537+ return start_year, end_year
500538
501539
502- start,end = yearly_data_arg_test (surveys_df, 1988, 1993)
503- print('Both optional arguments:\t', start, end)
540+ start,end = yearly_data_arg_test (surveys_df, 1988, 1993)
541+ print('Both optional arguments:\t', start, end)
504542
505- start,end = yearly_data_arg_test (surveys_df)
506- print('Default values:\t\t\t', start, end)
543+ start,end = yearly_data_arg_test (surveys_df)
544+ print('Default values:\t\t\t', start, end)
507545~~~
508546{: .language-python}
509547~~~
510- Both optional arguments: 1988 1993
511- Default values: 1977 2002
548+ Both optional arguments: 1988 1993
549+ Default values: 1977 2002
512550~~~
513- > > {: .output}
551+ {: .output}
514552
515553The default values of the ` start_year ` and ` end_year ` arguments in the function
516554` yearly_data_arg_test ` are now ` None ` . This is a build-it constant in Python
@@ -540,31 +578,31 @@ check the values of `start_year` and `end_year`. If statements execute a segment
540578of code when some condition is met. They commonly look something like this:
541579
542580~~~
543- a = 5
581+ a = 5
544582
545- if a<0: # Meets first condition?
583+ if a<0: # Meets first condition?
546584
547- # if a IS less than zero
548- print('a is a negative number')
585+ # if a IS less than zero
586+ print('a is a negative number')
549587
550- elif a>0: # Did not meet first condition. meets second condition?
588+ elif a>0: # Did not meet first condition. meets second condition?
551589
552- # if a ISN'T less than zero and IS more than zero
553- print('a is a positive number')
590+ # if a ISN'T less than zero and IS more than zero
591+ print('a is a positive number')
554592
555- else: # Met neither condition
593+ else: # Met neither condition
556594
557- # if a ISN'T less than zero and ISN'T more than zero
558- print('a must be zero!')
595+ # if a ISN'T less than zero and ISN'T more than zero
596+ print('a must be zero!')
559597~~~
560598{: .language-python}
561599
562600Which would return:
563601
564602~~~
565- a is a positive number
603+ a is a positive number
566604~~~
567- > > {: .output}
605+ {: .output}
568606
569607Change the value of ` a ` to see how this function works. The statement ` elif `
570608means "else if", and all of the conditional statements must end in a colon.
@@ -588,34 +626,34 @@ function definition is associated with a keyword and the function call passes
588626values to the function using these keywords:
589627
590628~~~
591- start,end = yearly_data_arg_test (surveys_df)
592- print('Default values:\t\t\t', start, end)
629+ start,end = yearly_data_arg_test (surveys_df)
630+ print('Default values:\t\t\t', start, end)
593631
594- start,end = yearly_data_arg_test (surveys_df, 1988, 1993)
595- print('No keywords:\t\t\t', start, end)
632+ start,end = yearly_data_arg_test (surveys_df, 1988, 1993)
633+ print('No keywords:\t\t\t', start, end)
596634
597- start,end = yearly_data_arg_test (surveys_df, start_year = 1988, end_year = 1993)
598- print('Both keywords, in order:\t', start, end)
635+ start,end = yearly_data_arg_test (surveys_df, start_year = 1988, end_year = 1993)
636+ print('Both keywords, in order:\t', start, end)
599637
600- start,end = yearly_data_arg_test (surveys_df, end_year = 1993, start_year = 1988)
601- print('Both keywords, flipped:\t\t', start, end)
638+ start,end = yearly_data_arg_test (surveys_df, end_year = 1993, start_year = 1988)
639+ print('Both keywords, flipped:\t\t', start, end)
602640
603- start,end = yearly_data_arg_test (surveys_df, start_year = 1988)
604- print('One keyword, default end:\t', start, end)
641+ start,end = yearly_data_arg_test (surveys_df, start_year = 1988)
642+ print('One keyword, default end:\t', start, end)
605643
606- start,end = yearly_data_arg_test (surveys_df, end_year = 1993)
607- print('One keyword, default start:\t', start, end)
644+ start,end = yearly_data_arg_test (surveys_df, end_year = 1993)
645+ print('One keyword, default start:\t', start, end)
608646~~~
609647{: .language-python}
610648~~~
611- Default values: 1977 2002
612- No keywords: 1988 1993
613- Both keywords, in order: 1988 1993
614- Both keywords, flipped: 1988 1993
615- One keyword, default end: 1988 2002
616- One keyword, default start: 1977 1993
649+ Default values: 1977 2002
650+ No keywords: 1988 1993
651+ Both keywords, in order: 1988 1993
652+ Both keywords, flipped: 1988 1993
653+ One keyword, default end: 1988 2002
654+ One keyword, default start: 1977 1993
617655~~~
618- > > {: .output}
656+ {: .output}
619657
620658> ## Challenge - Modifying functions
621659>
@@ -632,11 +670,11 @@ values to the function using these keywords:
632670> for a directory to write to.
633671>
634672> ~~~
635- > if 'dir_name_here' in os.listdir('.'):
636- > print('Processed directory exists')
637- > else:
638- > os.mkdir('dir_name_here')
639- > print('Processed directory created')
673+ >if 'dir_name_here' in os.listdir('.'):
674+ > print('Processed directory exists')
675+ >else:
676+ > os.mkdir('dir_name_here')
677+ > print('Processed directory created')
640678> ~~~
641679> {: .language-python }
642680>
0 commit comments