├── .gitignore
├── LICENSE
├── README.md
├── contacts.json
├── installing_pip.md
├── knowledge_share
└── lesson05_apis.py
├── playtime
├── lesson01_pbj.py
├── lesson01_twitter.py
├── lesson02_99bottles.py
├── lesson02_fibonnaci.py
├── lesson02_fizzbuzz.py
├── lesson02_movies.py
├── lesson02_pbj_while.py
├── lesson02_states.py
├── lesson03_compare.py
├── lesson03_contacts.py
├── lesson03_states.py
├── lesson04_csvtolist.py
├── lesson04_deduplicate.py
├── lesson04_group_csvtodict.py
└── lesson05_firstapi.py
├── section_01_(basics)
├── basic_syntax.py
├── data_types.py
├── simple_math.py
└── variable_assignment.py
├── section_02_(strings)
├── is_alphaspace.py
├── slicing.py
├── string_count.py
├── string_find.py
├── string_format.py
├── string_lower.py
└── string_replace.py
├── section_03_(conditionals)
├── happy_hour.py
├── life.py
└── volunteer_recruitment.py
├── section_04_(lists)
├── list_basics.py
└── list_deduplicate.py
├── section_05_(loops)
├── 12days.py
├── calendar.py
├── enumerate.py
├── for_quadrants.py
├── loops.py
├── loops_gif.py
├── movies.csv
├── states.html
├── while_menu.py
├── while_quadrants.py
└── zip_bingo.py
├── section_06_(str-list)
├── join.py
├── quadrants_exercise.py
└── split.py
├── section_07_(files)
├── all_employees.csv
├── class-stats.csv
├── contacts.csv
├── read_csv.csv
├── read_csv.py
├── read_text.py
├── state_info.csv
├── states.csv
├── states.txt
├── states_enumerate.py
├── states_pop.txt
├── survey.csv
├── with_open.py
└── write_population.py
├── section_09_(functions)
├── csv_to_dict.py
├── division.py
├── dropdown_states.py
├── events.csv
├── film_screening_attendees.txt
├── happy_hour_attendees.txt
├── lessons.txt
├── open_csvfile.py
├── remove_duplicates.py
└── textfile_to_string.py
├── section_10_(dictionaries)
├── dict_access.py
├── dict_exercise.py
├── dict_exercise_2.py
├── dict_get.py
├── dict_items.py
├── dict_keys.py
├── dict_update.py
├── dict_values.py
└── nested_access.py
├── section_11_(api)
├── contacts.json
├── dict_to_json.py
├── dicts_and_lists.py
├── json_to_dict.py
└── using_requests.py
├── section_14_(exceptions)
├── exceptions_01.py
├── exceptions_02.py
├── exceptions_03.py
├── exceptions_04.py
├── exceptions_05.py
├── exceptions_06.py
├── exceptions_07.py
├── exceptions_08.py
└── multiple_exceptions.py
├── section_15_(intermediate)
├── quadrant.py
└── timestamp.py
└── start-here.md
/.gitignore:
--------------------------------------------------------------------------------
1 | #################
2 | ## Eclipse
3 | #################
4 |
5 | *.pydevproject
6 | .project
7 | .metadata
8 | bin/
9 | tmp/
10 | *.tmp
11 | *.bak
12 | *.swp
13 | *~.nib
14 | local.properties
15 | .classpath
16 | .settings/
17 | .loadpath
18 |
19 | # External tool builders
20 | .externalToolBuilders/
21 |
22 | # Locally stored "Eclipse launch configurations"
23 | *.launch
24 |
25 | # CDT-specific
26 | .cproject
27 |
28 | # PDT-specific
29 | .buildpath
30 |
31 |
32 | #################
33 | ## Visual Studio
34 | #################
35 |
36 | ## Ignore Visual Studio temporary files, build results, and
37 | ## files generated by popular Visual Studio add-ons.
38 |
39 | # User-specific files
40 | *.suo
41 | *.user
42 | *.sln.docstates
43 |
44 | # Build results
45 |
46 | [Dd]ebug/
47 | [Rr]elease/
48 | x64/
49 | build/
50 | [Bb]in/
51 | [Oo]bj/
52 |
53 | # MSTest test Results
54 | [Tt]est[Rr]esult*/
55 | [Bb]uild[Ll]og.*
56 |
57 | *_i.c
58 | *_p.c
59 | *.ilk
60 | *.meta
61 | *.obj
62 | *.pch
63 | *.pdb
64 | *.pgc
65 | *.pgd
66 | *.rsp
67 | *.sbr
68 | *.tlb
69 | *.tli
70 | *.tlh
71 | *.tmp
72 | *.tmp_proj
73 | *.log
74 | *.vspscc
75 | *.vssscc
76 | .builds
77 | *.pidb
78 | *.log
79 | *.scc
80 |
81 | # Visual C++ cache files
82 | ipch/
83 | *.aps
84 | *.ncb
85 | *.opensdf
86 | *.sdf
87 | *.cachefile
88 |
89 | # Visual Studio profiler
90 | *.psess
91 | *.vsp
92 | *.vspx
93 |
94 | # Guidance Automation Toolkit
95 | *.gpState
96 |
97 | # ReSharper is a .NET coding add-in
98 | _ReSharper*/
99 | *.[Rr]e[Ss]harper
100 |
101 | # TeamCity is a build add-in
102 | _TeamCity*
103 |
104 | # DotCover is a Code Coverage Tool
105 | *.dotCover
106 |
107 | # NCrunch
108 | *.ncrunch*
109 | .*crunch*.local.xml
110 |
111 | # Installshield output folder
112 | [Ee]xpress/
113 |
114 | # DocProject is a documentation generator add-in
115 | DocProject/buildhelp/
116 | DocProject/Help/*.HxT
117 | DocProject/Help/*.HxC
118 | DocProject/Help/*.hhc
119 | DocProject/Help/*.hhk
120 | DocProject/Help/*.hhp
121 | DocProject/Help/Html2
122 | DocProject/Help/html
123 |
124 | # Click-Once directory
125 | publish/
126 |
127 | # Publish Web Output
128 | *.Publish.xml
129 | *.pubxml
130 |
131 | # NuGet Packages Directory
132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line
133 | #packages/
134 |
135 | # Windows Azure Build Output
136 | csx
137 | *.build.csdef
138 |
139 | # Windows Store app package directory
140 | AppPackages/
141 |
142 | # Others
143 | sql/
144 | *.Cache
145 | ClientBin/
146 | [Ss]tyle[Cc]op.*
147 | ~$*
148 | *~
149 | *.dbmdl
150 | *.[Pp]ublish.xml
151 | *.pfx
152 | *.publishsettings
153 |
154 | # RIA/Silverlight projects
155 | Generated_Code/
156 |
157 | # Backup & report files from converting an old project file to a newer
158 | # Visual Studio version. Backup files are not needed, because we have git ;-)
159 | _UpgradeReport_Files/
160 | Backup*/
161 | UpgradeLog*.XML
162 | UpgradeLog*.htm
163 |
164 | # SQL Server files
165 | App_Data/*.mdf
166 | App_Data/*.ldf
167 |
168 | #############
169 | ## Windows detritus
170 | #############
171 |
172 | # Windows image file caches
173 | Thumbs.db
174 | ehthumbs.db
175 |
176 | # Folder config file
177 | Desktop.ini
178 |
179 | # Recycle Bin used on file shares
180 | $RECYCLE.BIN/
181 |
182 | # Mac crap
183 | .DS_Store
184 |
185 |
186 | #############
187 | ## Python
188 | #############
189 |
190 | *.py[co]
191 |
192 | # Packages
193 | *.egg
194 | *.egg-info
195 | dist/
196 | build/
197 | eggs/
198 | parts/
199 | var/
200 | sdist/
201 | develop-eggs/
202 | .installed.cfg
203 |
204 | # Installer logs
205 | pip-log.txt
206 |
207 | # Unit test / coverage reports
208 | .coverage
209 | .tox
210 |
211 | #Translations
212 | *.mo
213 |
214 | #Mr Developer
215 | .mr.developer.cfg
216 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2013-2014 Shannon Turner
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of
4 | this software and associated documentation files (the "Software"), to deal in
5 | the Software without restriction, including without limitation the rights to
6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7 | the Software, and to permit persons to whom the Software is furnished to do so,
8 | subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## First time installing/running Python? Be sure to check out the Start Here guide: https://github.com/shannonturner/python-lessons/blob/master/start-here.md
2 |
3 | In here are code snippets, examples, and other ways to help teach some of the concepts and tricks in Python.
4 |
5 | ## Slides are available to download here: https://github.com/hearmecode/slides
6 |
7 | #### Suggested Learning Progression
8 |
9 | If you're new to Python you may wonder what you should learn and when you should learn it. I recommend this order seen below. **Each section below has its own folder above; each bullet point has its own file within that folder.**
10 |
11 | So if you wanted to learn more about variable assignment, go to the folder **section_01_(basics)** and open up **variable_assignment.py**
12 |
13 | #### Section 1: General Programming Basics
14 | - Simple Math
15 | - Variable Assignment
16 | - Basic Syntax and Logic
17 | - Data Types: int, float, bool, str
18 |
19 | #### Section 2: Strings
20 | - Identifying and using strings
21 | - String slicing
22 | - String formatting
23 | - String methods
24 | - - str.replace()
25 | - - str.find()
26 | - - str.count()
27 | - - str.lower()
28 |
29 | #### Section 3: Conditionals
30 | - Logical control; changing the behavior of your programs
31 | - and keyword, or keyword
32 | - if / else structure
33 | - if / elif ... / else structure
34 | - Nesting conditionals
35 |
36 | #### Section 4: Lists
37 | - Containers for data types
38 | - First In, Last Out (Stack)
39 | - Accessing specific list items through index (slice notation)
40 | - List methods
41 | - - list.append()
42 | - - list.insert()
43 | - - list.pop()
44 | - - list.extend()
45 | - Finding items in list (in keyword)
46 | - Deduplicating a list using list(set(list_to_deduplicate))
47 |
48 | #### Section 5: Loops
49 | - for loop (for each item in a list: ...)
50 | - enumerate()
51 | - zip()
52 | - range()
53 | - while loop (ask each time: is this still true?)
54 |
55 | #### Section 6: Strings to Lists and Vice-Versa
56 | - str.join() (create a string from a list)
57 | - str.split() (create a list from a string)
58 |
59 | #### Section 7: File handling
60 | - with open(filename) as textfile: ...
61 | - file.read()
62 | - Reading text files
63 | - Reading CSV files
64 | - File handling flags (r, w, b, +)
65 | - Writing to files
66 |
67 | #### Section 8: Python's Most Commonly Used Built-in Functions
68 | - - Types (used primarily for converting one type to another)
69 | - int()
70 | - float()
71 | - str()
72 | - bool()
73 | - list()
74 | - set()
75 | - dict()
76 | - - Comparison and Calculation
77 | - abs()
78 | - len()
79 | - max()
80 | - min()
81 | - round()
82 | - sum()
83 | - - Loop (Sequence) Control
84 | - enumerate()
85 | - range() / xrange()
86 | - reversed()
87 | - sorted()
88 | - zip()
89 |
90 | #### Section 9: Functions
91 | - def keyword
92 | - arguments
93 | - default arguments
94 | - *args
95 | - **kwargs
96 | - return keyword
97 | - sequence unpacking (return and receive multiple values)
98 | - namespaces
99 |
100 | #### Section 10: Dictionaries
101 | - Accessing specific dictionary items through key (looks like a slice)
102 | - Accessing all keys as a list using .keys()
103 | - Faking a sorted dictionary by using sorted() on the .keys()
104 | - Accessing all values as a list using .values()
105 | - Accessing all key, value pairs as a list using .items()
106 | - Adding new items through .fromkeys()
107 | - Checking for whether a key exists with .has_key
108 | - Using .get() to safely get a key's value if it exists without getting an error if not
109 | - Adding new items with direct assignment and .update()
110 |
111 | #### Section 11: Dictionaries and Lists, together
112 | - Accessing specific items in a nested list
113 | - Accessing specific items in a nested dictionary
114 | - Accessing specific items in a nested list within a dictionary
115 | - Accessing specific items in a nested dictionary within a list
116 | - If you can do those four above, you can handle receiving JSON API returns
117 |
118 | #### Section 12: Standard Library
119 | - import keyword
120 | - from ... import ... as ... structure
121 | - time
122 | - random
123 | - math
124 | - re (regular expressions)
125 | - os
126 | - sys
127 | - json
128 |
129 | #### Section 13: External Libraries (Not necessarily in order; keep these in mind)
130 | - Installing external libraries with easy_install
131 | - Using easy_install to install pip (an easier / better way to install external libraries)
132 | - requests (web crawling made easy)
133 | - BeautifulSoup (parsing HTML)
134 | - xlrd (Read Excel .xls files)
135 | - xlwt (Write to Excel .xls files)
136 | - xlsxwriter (Write to Excel .xls and .xlsx files, with additional functionality beyond xlwt)
137 | - cherrypy (Simple, lightweight framework for serving web pages)
138 | - psycopg2 (Connect to and issue SQL commands to your postgresql database)
139 |
140 | #### Section 14: Exception Handling
141 | - try / except syntax
142 | - Using multiple excepts
143 | - Recognizing the different error types
144 | - Exception, the generic exception type (use sparingly)
145 | - Nesting exception handling
146 | - try / except / else syntax
147 |
148 | #### Section 15: Intermediate Concepts
149 | - List Comprehensions
150 | - Inline Conditionals
151 | - Generators
152 |
153 | #### Section 16: Classes
154 | - Classes
155 | - Magic Methods
156 |
--------------------------------------------------------------------------------
/contacts.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "enemies": [],
4 | "friends": [
5 | {
6 | "Shannon": {
7 | "github": "@shannonturner",
8 | "phone": "202-555-1234",
9 | "twitter": "@svt827"
10 | }
11 | },
12 | {
13 | "Amy": {
14 | "email": "amy@amy.org",
15 | "fax": "410-555-3001",
16 | "phone": "410-515-3000"
17 | }
18 | },
19 | {
20 | "Jen": {
21 | "email": "jen@jen.biz",
22 | "phone": "301-600-5555"
23 | }
24 | },
25 | {
26 | "Julie": {
27 | "phone": "202-333-9876"
28 | }
29 | }
30 | ]
31 | }
32 | ]
--------------------------------------------------------------------------------
/installing_pip.md:
--------------------------------------------------------------------------------
1 | ## Installing the Python Package Managers
2 |
3 | Packages are groups of functions that someone else has built in Python to share with the world
4 | Packages can help you perform a set of functions
5 | For example, the **requests** package lets you visit a webpage and retrieve the contents (source) as a string.
6 |
7 | Before you can use someone else's package, you must install it onto your computer.
8 | Before you can install it on your computer, you'll need to install a package manager.
9 |
10 | The preferred package manager is called pip, but we need to take a few steps to get there.
11 |
12 | ## Complete these two steps before going on to your system-specific instructions below.
13 | * First, go here: https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
14 | * Copy / save that file to your computer
15 |
16 | **Windows-specific instructions**
17 | * Make sure you complete the first two steps above first.
18 | * Create the folder 'scripts' inside C:\python27
19 | * Save the ez_setup.py inside C:\python27\scripts
20 | * If in Windows, now you'll need to add "C:\Python27\scripts" to your PATH environment variable.
21 | * To do that, go to My Computer > Properties > Advanced System Settings > Environment Variables > Path
22 | * You'll also need to add "C:\Python27" to your PATH environment variable.
23 | * Don't delete everything else. Add onto it, separating the entries with a semicolon.
24 | * Open your terminal (Start Menu > Run > cmd) or (Start Menu > Search > cmd)
25 | * Use cd to change the directory you're working in: cd c:/python27/scripts
26 | * Type **python ez_setup.py** and hit enter.
27 | * Type **easy_install pip** and hit enter.
28 | * Type **pip install requests** and hit enter. If it was successful, you're done! Help a neighbor.
29 |
30 | **Mac-specific instructions**
31 | * Make sure you complete the first two steps above first.
32 | * Run that program in Python.
33 | * Type **easy_install pip** and hit enter. If you get permission errors, try **sudo easy_install pip**
34 | * Type **pip install requests** and hit enter. If you get permission errors, try **sudo pip install requests**
35 | * If it was successful, you're done! Help a neighbor.
--------------------------------------------------------------------------------
/knowledge_share/lesson05_apis.py:
--------------------------------------------------------------------------------
1 | # Knowledge Sharing from Lesson 5: APIs
2 |
3 | print len(response)
4 |
5 | if response == []:
6 | print "Sorry not found"
7 |
8 | if title[0:3] == 'The':
9 | title = title.replace("The", "")
10 |
11 | if title.isdigits():
12 | print "It's all digits!"
13 |
14 | # Getting KeyErrors? use .get()
15 |
16 | if response.get('title'):
17 | print "There's a title here"
18 |
19 | # Don't do too much at once!
20 | # Break it down step by step.
21 |
22 | # Be sure to use the right endpoint and parameters
23 |
24 | # Getting user input to attach as a parameter
25 | title = raw_input("What movie do you want? ")
26 |
27 | url = 'http://bechdeltest.com/api/v1/getMoviesByTitle?title={0}'.format(title).replace(" ","+").replace("'","'")
28 |
29 | print url
30 |
31 | response = requests.get(url).json()
32 |
33 | # Formatting of an IMDB ID vs. a title
34 |
35 | # Having lots of users makes testing great
36 |
37 | # Use multiple raw_inputs if it didn't find the movie!
38 |
39 | # Sometimes the endpoints return different responses!
40 |
41 | # Removing spaces from the user input
42 | title = title.strip()
43 |
44 | # Removing 'The' from the user input ... any of these characters
45 | title = title.strip('The')
46 |
47 | if title[0:4] == 'The ':
48 | title = title[4:]
49 |
50 | # ASCII replaces: www.asciitables.com
51 | import urllib
52 | url = urllib.quote(url)
53 |
54 | response = requests.get(url)
55 |
56 | # Using .get() to avoid KeyErrors
57 | # Using .get('key', 'ifnotfound')
58 |
59 | # Converting the responses to the format we needed
60 |
61 | # Using multiple APIs: sometimes the parameters need adjustment
62 |
63 | # Thelma and Louise? Don't remove the 'The'
64 |
65 | # Loop through and test -- you can't fix every user error
66 |
67 | # Mash on your keyboard for user testing
68 |
69 | title = 'lsdfasdjflksjflksd'
70 |
71 | # Don't forget .json()
72 |
--------------------------------------------------------------------------------
/playtime/lesson01_pbj.py:
--------------------------------------------------------------------------------
1 | # Peanut Butter Jelly Time!
2 |
3 | # First Goal: Create a program that can tell you whether or not you can make a peanut butter and jelly sandwich
4 |
5 | # Second Goal: Create a program to tell you: if you can make a sandwich, how many you can make
6 |
7 | # Third Goal: Create a program to allow you to make open-face sandwiches if you have an odd number of slices of bread ( )
8 |
9 | # Fourth Goal: Create a program to tell you: if you're missing ingredients, which ones you need to be able to make your sandwiches
10 |
11 | # Fifth Goal: Create a program to tell you: if you have enough bread and peanut butter but no jelly, that you can make a peanut butter sandwich but you should take a hard, honest look at your life. Wow, your program is kinda judgy.
12 |
13 |
14 | # What are the step-by-steps to recreate this?
15 | # First, you need variables to store your information. Remember, variables are just containers for your information that you give a name.
16 |
17 | # You need three ingredients to make a PB&J, so you'll want three different variables:
18 | # How much bread do you have? (make this a number that reflects how many slices of bread you have)
19 | # How much peanut butter do you have? (make this a number that reflects how many sandwiches-worth of peanut butter you have)
20 | # How much jelly do you have? (make this a number that reflects how many sandwiches-worth of jelly you have)
21 |
22 | # For this exercise, we'll assume you have the requisite tools (plate, knife, etc)
23 |
24 | # Once you've defined your variables that tell you how much of each ingredient you have, use conditionals to test whether you have the right amount of ingredients
25 |
26 | # Based on the results of that conditional, display a message.
27 |
28 | # To satisfy the first goal:
29 | # If you have enough bread (2 slices), peanut butter (1), and jelly (1), print a message like "I can make a peanut butter and jelly sandwich";
30 | # If you don't; print a message like "Looks like I don't have a lunch today"
31 |
32 | # To satisfy the second goal:
33 | # Continue from the first goal, and add:
34 | # If you have enough bread (at least 2 slices), peanut butter (at least 1), and jelly (at least 1), print a message like "I can make this many sandwiches: " and then calculate the result.
35 | # If you don't; you can print the same message as before
36 | # To calculate which ingredient you have the least of, the min() function will be useful.
37 | # min() will calculate the smallest number of all of the numbers in the parentheses and tell you which it is
38 | # For example, min(4, 83, 6) will return 4
39 |
40 | # To satisfy the third goal:
41 | # Continue from the second goal, and add:
42 | # If you have an odd number of slices of bread* and odd amount of peanut butter and jelly, print a message like before, but mention that you can make an open-face sandwich, too.
43 | # If you don't have enough ingredients; still print the same message as before
44 | # * To calculate whether you have an odd number, see https://github.com/shannonturner/python-lessons/blob/master/section_01_(basics)/simple_math.py
45 |
46 | # To satisfy the fourth goal:
47 | # Continue from the third goal, but this time if you have enough bread and peanut butter but no jelly, print a message that tells you that you can make a peanut butter sandwich
48 | # Or if you have more peanut butter and bread than jelly, that you can make a certain number of peanut butter & jelly sandwiches and a certain number of peanut butter sandwiches
49 |
50 | # To satisfy the fifth goal:
51 | # Continue from the fourth goal, but this time if you don't have enough ingredients, print a message that tells you which ones you're missing.
52 |
--------------------------------------------------------------------------------
/playtime/lesson01_twitter.py:
--------------------------------------------------------------------------------
1 | # Difficulty Level: Beginner
2 | # Exercise: Tweet length calculator
3 |
4 | # Part one:
5 | # Create a variable called tweet and put some text in it
6 | # maybe something like "Hear Me Code class was so much fun today!"
7 |
8 | # Measure the length of that tweet.
9 |
10 | # Was that tweet more than 140 characters?
11 | # If so, tell the user it was too long!
12 | # Was that tweet 140 or fewer characters?
13 | # If so, tell the user how witty they are!
14 |
15 |
16 | # Part two:
17 | # Adjust the program to say how many characters you have remaining to use, or how many you need to trim by in order to meet the 140 character limit
18 |
19 |
20 | # Part three:
21 | # Twitter announced they are changing their character limit to 280, but they might change it again.
22 | # Can you make your code flexible enough so that you don't have to replace the character limit in multiple places in your code?
23 |
24 |
--------------------------------------------------------------------------------
/playtime/lesson02_99bottles.py:
--------------------------------------------------------------------------------
1 | # Difficulty Level: Beginner
2 |
3 | # Can you make Python print out the song for 99 bottles of beer on the wall?
4 |
5 | # Note: You can use range() in three different ways
6 |
7 | # First:
8 | # range(5) will give you a list containing [0, 1, 2, 3, 4]
9 | # In this case, range assumes you want to start counting at 0, and the parameter you give is the number to stop *just* short of.
10 |
11 | # Second:
12 | # range(5, 10) will give you a list containing [5, 6, 7, 8, 9]
13 | # In this case, the two parameters you give to range() are the number to start at and the number to stop *just* short of.
14 | # Helpful mnemonic: range(start, stop)
15 |
16 | # Third:
17 | # range(5, 15, 3) will give you a list containing [5, 8, 11, 14]
18 | # In this case, the three parameters you give to range() are the number to start at, the number to stop *just* short of, and the number to increment each time by.
19 | # Note that normally, the number to increment each time by is assumed to be 1. (In other words, you add 1 each time through.)
20 | # That's why it goes [0, 1, 2, 3, 4] unless you specify that third parameter, called the step.
21 | # Helpful mnemonic: range(start, stop, step)
22 |
23 | # Using range() and a loop, print out the song. Your output should look like this:
24 |
25 | # 99 bottles of beer on the wall, 99 bottles of beer ...
26 | # If one of those bottles should happen to fall, 98 bottles of beer on the wall
27 | # 98 bottles of beer on the wall, 98 bottles of beer ...
28 | # If one of those bottles should happen to fall, 97 bottles of beer on the wall
29 | # 97 bottles of beer on the wall, 97 bottles of beer ...
30 | # If one of those bottles should happen to fall, 96 bottles of beer on the wall
31 | # 96 bottles of beer on the wall, 96 bottles of beer ...
32 | # If one of those bottles should happen to fall, 95 bottles of beer on the wall
33 | # 95 bottles of beer on the wall, 95 bottles of beer ...
34 | # If one of those bottles should happen to fall, 94 bottles of beer on the wall
35 | # 94 bottles of beer on the wall, 94 bottles of beer ...
36 | # If one of those bottles should happen to fall, 93 bottles of beer on the wall
37 | # 93 bottles of beer on the wall, 93 bottles of beer ...
38 | # If one of those bottles should happen to fall, 92 bottles of beer on the wall
39 | # 92 bottles of beer on the wall, 92 bottles of beer ...
40 | # If one of those bottles should happen to fall, 91 bottles of beer on the wall
41 | # 91 bottles of beer on the wall, 91 bottles of beer ...
42 | # If one of those bottles should happen to fall, 90 bottles of beer on the wall
43 | # 90 bottles of beer on the wall, 90 bottles of beer ...
44 | # If one of those bottles should happen to fall, 89 bottles of beer on the wall
45 | # 89 bottles of beer on the wall, 89 bottles of beer ...
46 | # If one of those bottles should happen to fall, 88 bottles of beer on the wall
47 | # 88 bottles of beer on the wall, 88 bottles of beer ...
48 | # If one of those bottles should happen to fall, 87 bottles of beer on the wall
49 | # 87 bottles of beer on the wall, 87 bottles of beer ...
50 | # If one of those bottles should happen to fall, 86 bottles of beer on the wall
51 | # 86 bottles of beer on the wall, 86 bottles of beer ...
52 | # If one of those bottles should happen to fall, 85 bottles of beer on the wall
53 | # 85 bottles of beer on the wall, 85 bottles of beer ...
54 | # If one of those bottles should happen to fall, 84 bottles of beer on the wall
55 | # 84 bottles of beer on the wall, 84 bottles of beer ...
56 | # If one of those bottles should happen to fall, 83 bottles of beer on the wall
57 | # 83 bottles of beer on the wall, 83 bottles of beer ...
58 | # If one of those bottles should happen to fall, 82 bottles of beer on the wall
59 | # 82 bottles of beer on the wall, 82 bottles of beer ...
60 | # If one of those bottles should happen to fall, 81 bottles of beer on the wall
61 | # 81 bottles of beer on the wall, 81 bottles of beer ...
62 | # If one of those bottles should happen to fall, 80 bottles of beer on the wall
63 | # 80 bottles of beer on the wall, 80 bottles of beer ...
64 | # If one of those bottles should happen to fall, 79 bottles of beer on the wall
65 | # 79 bottles of beer on the wall, 79 bottles of beer ...
66 | # If one of those bottles should happen to fall, 78 bottles of beer on the wall
67 | # 78 bottles of beer on the wall, 78 bottles of beer ...
68 | # If one of those bottles should happen to fall, 77 bottles of beer on the wall
69 | # 77 bottles of beer on the wall, 77 bottles of beer ...
70 | # If one of those bottles should happen to fall, 76 bottles of beer on the wall
71 | # 76 bottles of beer on the wall, 76 bottles of beer ...
72 | # If one of those bottles should happen to fall, 75 bottles of beer on the wall
73 | # 75 bottles of beer on the wall, 75 bottles of beer ...
74 | # If one of those bottles should happen to fall, 74 bottles of beer on the wall
75 | # 74 bottles of beer on the wall, 74 bottles of beer ...
76 | # If one of those bottles should happen to fall, 73 bottles of beer on the wall
77 | # 73 bottles of beer on the wall, 73 bottles of beer ...
78 | # If one of those bottles should happen to fall, 72 bottles of beer on the wall
79 | # 72 bottles of beer on the wall, 72 bottles of beer ...
80 | # If one of those bottles should happen to fall, 71 bottles of beer on the wall
81 | # 71 bottles of beer on the wall, 71 bottles of beer ...
82 | # If one of those bottles should happen to fall, 70 bottles of beer on the wall
83 | # 70 bottles of beer on the wall, 70 bottles of beer ...
84 | # If one of those bottles should happen to fall, 69 bottles of beer on the wall
85 | # 69 bottles of beer on the wall, 69 bottles of beer ...
86 | # If one of those bottles should happen to fall, 68 bottles of beer on the wall
87 | # 68 bottles of beer on the wall, 68 bottles of beer ...
88 | # If one of those bottles should happen to fall, 67 bottles of beer on the wall
89 | # 67 bottles of beer on the wall, 67 bottles of beer ...
90 | # If one of those bottles should happen to fall, 66 bottles of beer on the wall
91 | # 66 bottles of beer on the wall, 66 bottles of beer ...
92 | # If one of those bottles should happen to fall, 65 bottles of beer on the wall
93 | # 65 bottles of beer on the wall, 65 bottles of beer ...
94 | # If one of those bottles should happen to fall, 64 bottles of beer on the wall
95 | # 64 bottles of beer on the wall, 64 bottles of beer ...
96 | # If one of those bottles should happen to fall, 63 bottles of beer on the wall
97 | # 63 bottles of beer on the wall, 63 bottles of beer ...
98 | # If one of those bottles should happen to fall, 62 bottles of beer on the wall
99 | # 62 bottles of beer on the wall, 62 bottles of beer ...
100 | # If one of those bottles should happen to fall, 61 bottles of beer on the wall
101 | # 61 bottles of beer on the wall, 61 bottles of beer ...
102 | # If one of those bottles should happen to fall, 60 bottles of beer on the wall
103 | # 60 bottles of beer on the wall, 60 bottles of beer ...
104 | # If one of those bottles should happen to fall, 59 bottles of beer on the wall
105 | # 59 bottles of beer on the wall, 59 bottles of beer ...
106 | # If one of those bottles should happen to fall, 58 bottles of beer on the wall
107 | # 58 bottles of beer on the wall, 58 bottles of beer ...
108 | # If one of those bottles should happen to fall, 57 bottles of beer on the wall
109 | # 57 bottles of beer on the wall, 57 bottles of beer ...
110 | # If one of those bottles should happen to fall, 56 bottles of beer on the wall
111 | # 56 bottles of beer on the wall, 56 bottles of beer ...
112 | # If one of those bottles should happen to fall, 55 bottles of beer on the wall
113 | # 55 bottles of beer on the wall, 55 bottles of beer ...
114 | # If one of those bottles should happen to fall, 54 bottles of beer on the wall
115 | # 54 bottles of beer on the wall, 54 bottles of beer ...
116 | # If one of those bottles should happen to fall, 53 bottles of beer on the wall
117 | # 53 bottles of beer on the wall, 53 bottles of beer ...
118 | # If one of those bottles should happen to fall, 52 bottles of beer on the wall
119 | # 52 bottles of beer on the wall, 52 bottles of beer ...
120 | # If one of those bottles should happen to fall, 51 bottles of beer on the wall
121 | # 51 bottles of beer on the wall, 51 bottles of beer ...
122 | # If one of those bottles should happen to fall, 50 bottles of beer on the wall
123 | # 50 bottles of beer on the wall, 50 bottles of beer ...
124 | # If one of those bottles should happen to fall, 49 bottles of beer on the wall
125 | # 49 bottles of beer on the wall, 49 bottles of beer ...
126 | # If one of those bottles should happen to fall, 48 bottles of beer on the wall
127 | # 48 bottles of beer on the wall, 48 bottles of beer ...
128 | # If one of those bottles should happen to fall, 47 bottles of beer on the wall
129 | # 47 bottles of beer on the wall, 47 bottles of beer ...
130 | # If one of those bottles should happen to fall, 46 bottles of beer on the wall
131 | # 46 bottles of beer on the wall, 46 bottles of beer ...
132 | # If one of those bottles should happen to fall, 45 bottles of beer on the wall
133 | # 45 bottles of beer on the wall, 45 bottles of beer ...
134 | # If one of those bottles should happen to fall, 44 bottles of beer on the wall
135 | # 44 bottles of beer on the wall, 44 bottles of beer ...
136 | # If one of those bottles should happen to fall, 43 bottles of beer on the wall
137 | # 43 bottles of beer on the wall, 43 bottles of beer ...
138 | # If one of those bottles should happen to fall, 42 bottles of beer on the wall
139 | # 42 bottles of beer on the wall, 42 bottles of beer ...
140 | # If one of those bottles should happen to fall, 41 bottles of beer on the wall
141 | # 41 bottles of beer on the wall, 41 bottles of beer ...
142 | # If one of those bottles should happen to fall, 40 bottles of beer on the wall
143 | # 40 bottles of beer on the wall, 40 bottles of beer ...
144 | # If one of those bottles should happen to fall, 39 bottles of beer on the wall
145 | # 39 bottles of beer on the wall, 39 bottles of beer ...
146 | # If one of those bottles should happen to fall, 38 bottles of beer on the wall
147 | # 38 bottles of beer on the wall, 38 bottles of beer ...
148 | # If one of those bottles should happen to fall, 37 bottles of beer on the wall
149 | # 37 bottles of beer on the wall, 37 bottles of beer ...
150 | # If one of those bottles should happen to fall, 36 bottles of beer on the wall
151 | # 36 bottles of beer on the wall, 36 bottles of beer ...
152 | # If one of those bottles should happen to fall, 35 bottles of beer on the wall
153 | # 35 bottles of beer on the wall, 35 bottles of beer ...
154 | # If one of those bottles should happen to fall, 34 bottles of beer on the wall
155 | # 34 bottles of beer on the wall, 34 bottles of beer ...
156 | # If one of those bottles should happen to fall, 33 bottles of beer on the wall
157 | # 33 bottles of beer on the wall, 33 bottles of beer ...
158 | # If one of those bottles should happen to fall, 32 bottles of beer on the wall
159 | # 32 bottles of beer on the wall, 32 bottles of beer ...
160 | # If one of those bottles should happen to fall, 31 bottles of beer on the wall
161 | # 31 bottles of beer on the wall, 31 bottles of beer ...
162 | # If one of those bottles should happen to fall, 30 bottles of beer on the wall
163 | # 30 bottles of beer on the wall, 30 bottles of beer ...
164 | # If one of those bottles should happen to fall, 29 bottles of beer on the wall
165 | # 29 bottles of beer on the wall, 29 bottles of beer ...
166 | # If one of those bottles should happen to fall, 28 bottles of beer on the wall
167 | # 28 bottles of beer on the wall, 28 bottles of beer ...
168 | # If one of those bottles should happen to fall, 27 bottles of beer on the wall
169 | # 27 bottles of beer on the wall, 27 bottles of beer ...
170 | # If one of those bottles should happen to fall, 26 bottles of beer on the wall
171 | # 26 bottles of beer on the wall, 26 bottles of beer ...
172 | # If one of those bottles should happen to fall, 25 bottles of beer on the wall
173 | # 25 bottles of beer on the wall, 25 bottles of beer ...
174 | # If one of those bottles should happen to fall, 24 bottles of beer on the wall
175 | # 24 bottles of beer on the wall, 24 bottles of beer ...
176 | # If one of those bottles should happen to fall, 23 bottles of beer on the wall
177 | # 23 bottles of beer on the wall, 23 bottles of beer ...
178 | # If one of those bottles should happen to fall, 22 bottles of beer on the wall
179 | # 22 bottles of beer on the wall, 22 bottles of beer ...
180 | # If one of those bottles should happen to fall, 21 bottles of beer on the wall
181 | # 21 bottles of beer on the wall, 21 bottles of beer ...
182 | # If one of those bottles should happen to fall, 20 bottles of beer on the wall
183 | # 20 bottles of beer on the wall, 20 bottles of beer ...
184 | # If one of those bottles should happen to fall, 19 bottles of beer on the wall
185 | # 19 bottles of beer on the wall, 19 bottles of beer ...
186 | # If one of those bottles should happen to fall, 18 bottles of beer on the wall
187 | # 18 bottles of beer on the wall, 18 bottles of beer ...
188 | # If one of those bottles should happen to fall, 17 bottles of beer on the wall
189 | # 17 bottles of beer on the wall, 17 bottles of beer ...
190 | # If one of those bottles should happen to fall, 16 bottles of beer on the wall
191 | # 16 bottles of beer on the wall, 16 bottles of beer ...
192 | # If one of those bottles should happen to fall, 15 bottles of beer on the wall
193 | # 15 bottles of beer on the wall, 15 bottles of beer ...
194 | # If one of those bottles should happen to fall, 14 bottles of beer on the wall
195 | # 14 bottles of beer on the wall, 14 bottles of beer ...
196 | # If one of those bottles should happen to fall, 13 bottles of beer on the wall
197 | # 13 bottles of beer on the wall, 13 bottles of beer ...
198 | # If one of those bottles should happen to fall, 12 bottles of beer on the wall
199 | # 12 bottles of beer on the wall, 12 bottles of beer ...
200 | # If one of those bottles should happen to fall, 11 bottles of beer on the wall
201 | # 11 bottles of beer on the wall, 11 bottles of beer ...
202 | # If one of those bottles should happen to fall, 10 bottles of beer on the wall
203 | # 10 bottles of beer on the wall, 10 bottles of beer ...
204 | # If one of those bottles should happen to fall, 9 bottles of beer on the wall
205 | # 9 bottles of beer on the wall, 9 bottles of beer ...
206 | # If one of those bottles should happen to fall, 8 bottles of beer on the wall
207 | # 8 bottles of beer on the wall, 8 bottles of beer ...
208 | # If one of those bottles should happen to fall, 7 bottles of beer on the wall
209 | # 7 bottles of beer on the wall, 7 bottles of beer ...
210 | # If one of those bottles should happen to fall, 6 bottles of beer on the wall
211 | # 6 bottles of beer on the wall, 6 bottles of beer ...
212 | # If one of those bottles should happen to fall, 5 bottles of beer on the wall
213 | # 5 bottles of beer on the wall, 5 bottles of beer ...
214 | # If one of those bottles should happen to fall, 4 bottles of beer on the wall
215 | # 4 bottles of beer on the wall, 4 bottles of beer ...
216 | # If one of those bottles should happen to fall, 3 bottles of beer on the wall
217 | # 3 bottles of beer on the wall, 3 bottles of beer ...
218 | # If one of those bottles should happen to fall, 2 bottles of beer on the wall
219 | # 2 bottles of beer on the wall, 2 bottles of beer ...
220 | # If one of those bottles should happen to fall, 1 bottles of beer on the wall
221 |
--------------------------------------------------------------------------------
/playtime/lesson02_fibonnaci.py:
--------------------------------------------------------------------------------
1 | # Fibonnaci Sequence
2 |
3 | # Difficulty: Advanced
4 |
5 | # NOTE: This is *not* a practical example of how you'll use coding in your day to day, BUT it does appear on a surprising number of in-person coding interviews. Why employers rely on something so impractical to gauge your coding ability is beyond me.
6 | # But since they're using it, maybe in this narrow sense it's a practical exercise to know how to do after all.
7 |
8 |
9 | # The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it.
10 |
11 | # Create a program that will generate the first 10 numbers in the Fibonacci Sequence.
12 |
13 | # When completed, your program should have the output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
14 |
--------------------------------------------------------------------------------
/playtime/lesson02_fizzbuzz.py:
--------------------------------------------------------------------------------
1 | # Fizzbuzz
2 |
3 | # Difficulty: Beginner
4 |
5 | # NOTE: This is *not* a practical example of how you'll use coding in your day to day, BUT it does appear on a surprising number of in-person coding interviews. Why employers rely on something so impractical to gauge your coding ability is beyond me.
6 | # But since they're using it, maybe in this narrow sense it's a practical exercise to know how to do after all.
7 |
8 |
9 | # Create a program that loops through the numbers 0-100.
10 | # When you land on a number evenly divisible by 3, have your program print "Fizz"
11 | # When you land on a number evenly divisible by 5, have your program print "Buzz"
12 | # When you land on a number evenly divisible by both 3 and 5, have your program print "Fizzbuzz"
13 | # When you land on any other number, print that number
14 |
15 | # Remember the % operator will tell you whether a number is evenly divisible by another number!
16 | # We use it most often to determine whether a number is even or odd, but it's also useful here.
17 |
18 | # When completed, your program output should look like:
19 | # 1
20 | # 2
21 | # Fizz
22 | # 4
23 | # Buzz
24 | # Fizz
25 | # 7
26 | # 8
27 | # Fizz
28 | # Buzz
29 | # 11
30 | # Fizz
31 | # 13
32 | # 14
33 | # Fizzbuzz
34 | # 16
35 | # 17
36 | # Fizz
37 | # 19
38 | # Buzz
39 | # Fizz
40 | # 22
41 | # 23
42 | # Fizz
43 | # Buzz
44 | # 26
45 | # Fizz
46 | # 28
47 | # 29
48 | # Fizzbuzz
49 | # 31
50 | # 32
51 | # Fizz
52 | # 34
53 | # Buzz
54 | # Fizz
55 | # 37
56 | # 38
57 | # Fizz
58 | # Buzz
59 | # 41
60 | # Fizz
61 | # 43
62 | # 44
63 | # Fizzbuzz
64 | # 46
65 | # 47
66 | # Fizz
67 | # 49
68 | # Buzz
69 | # Fizz
70 | # 52
71 | # 53
72 | # Fizz
73 | # Buzz
74 | # 56
75 | # Fizz
76 | # 58
77 | # 59
78 | # Fizzbuzz
79 | # 61
80 | # 62
81 | # Fizz
82 | # 64
83 | # Buzz
84 | # Fizz
85 | # 67
86 | # 68
87 | # Fizz
88 | # Buzz
89 | # 71
90 | # Fizz
91 | # 73
92 | # 74
93 | # Fizzbuzz
94 | # 76
95 | # 77
96 | # Fizz
97 | # 79
98 | # Buzz
99 | # Fizz
100 | # 82
101 | # 83
102 | # Fizz
103 | # Buzz
104 | # 86
105 | # Fizz
106 | # 88
107 | # 89
108 | # Fizzbuzz
109 | # 91
110 | # 92
111 | # Fizz
112 | # 94
113 | # Buzz
114 | # Fizz
115 | # 97
116 | # 98
117 | # Fizz
118 | # Buzz
119 |
--------------------------------------------------------------------------------
/playtime/lesson02_movies.py:
--------------------------------------------------------------------------------
1 | # Difficulty level: Advanced
2 |
3 | # Goal #1: Create a program that will print out a list of movie titles and a set of ratings defined below into a particular format.
4 |
5 | # First, choose any five movies you want.
6 |
7 | # Next, look each movie up manually to find out four pieces of information:
8 | # Their parental guidance rating (G, PG, PG-13, R)
9 | # Their Bechdel Test Rating (See http://shannonvturner.com/bechdel or http://bechdeltest.com/)
10 | # Their IMDB Rating from 0 - 10 (See http://imdb.com/)
11 | # Their genre according to IMDB
12 |
13 | # After a few more lessons, you'll be able to tell Python to go out and get that information for you, but for now you'll have to collect it on your own.
14 |
15 | # Now that you've written down each piece of information for all five of your movies, save them into variables.
16 |
17 | # You'll need a variable for movie_titles, a variable for parental_rating, a variable for bechdel_rating, a variable for imdb_rating, and a variable for genre.
18 |
19 | # Since you have five sets of facts about five movies, you'll want to use lists to hold these pieces of information.
20 |
21 | # Once all of your information is stored in lists, loop through those lists to print out information with each part separated by a comma, like this:
22 |
23 | # Example:
24 | # Jurassic Park, PG-13, 3, 8.0, Adventure / Sci-Fi
25 | # Back to the Future, PG, 1, 8.5, Adventure / Comedy / Sci-Fi
26 |
27 | # Note how each piece of information is separated by a comma. This is a specific file format called the "Comma Separated Value (CSV)" format
28 | # If you can make a CSV file, you can open it up in Excel or Numbers as a spreadsheet.
29 |
30 | # When you've printed out your information like the example above, copy/paste that into a file and save it as a .csv file.
31 | # Open that up in Excel, Numbers, or another spreadsheet program. How does it look?
32 | # To see an example of how it should look, check out: https://github.com/shannonturner/python-lessons/blob/master/section_05_(loops)/movies.csv
33 |
--------------------------------------------------------------------------------
/playtime/lesson02_pbj_while.py:
--------------------------------------------------------------------------------
1 | # Difficulty level: Beginner
2 |
3 | # Goal #1: Write a new version of the PB&J program that uses a while loop. Print "Making sandwich #" and the number of the sandwich until you are out of bread, peanut butter, or jelly.
4 |
5 | # Example:
6 | # bread = 4
7 | # peanut_butter = 3
8 | # jelly = 10
9 |
10 | # Output:
11 | # Making sandwich #1
12 | # Making sandwich #2
13 | # All done; only had enough bread for 2 sandwiches.
14 |
15 | # Goal #2: Modify that program to say how many sandwiches-worth of each ingredient remains.
16 |
17 | # Example 2:
18 | # bread = 10
19 | # peanut_butter = 10
20 | # jelly = 4
21 |
22 | # Output:
23 | # Making sandwich #1
24 | # I have enough bread for 4 more sandwiches, enough peanut butter for 9 more, and enough jelly for 3 more.
25 | # Making sandwich #2
26 | # I have enough bread for 3 more sandwiches, enough peanut butter for 8 more, and enough jelly for 2 more.
27 | # Making sandwich #3
28 | # I have enough bread for 2 more sandwiches, enough peanut butter for 7 more, and enough jelly for 1 more.
29 | # Making sandwich #4
30 | # All done; I ran out of jelly.
31 |
--------------------------------------------------------------------------------
/playtime/lesson02_states.py:
--------------------------------------------------------------------------------
1 | # Difficulty Level: Intermediate
2 |
3 | # Goal: Create a program that prints out an HTML drop down menu for all 50 states
4 |
5 | # Step 1: Define your list of states
6 | # These should all be strings, since they're names of places
7 | # Instead of having to type them all out, I really like liststates.com -- you can even customize the format it gives you the states in to make it super easy to copy/paste into your code here
8 |
9 | # Step 2: Create your loop
10 | # Essentially, you're telling Python: for each state in my list: print this HTML code
11 | # A good place to start is by printing the name of the state in the loop; after that you can add the HTML around it
12 |
13 | # Step 3: Add the HTML
14 | # A drop-down menu in HTML looks like this:
15 |
16 | #
19 |
20 | # At line 14, we create the drop-down menu
21 | # At line 15, we create one drop-down item. Each additional