├── .gitattributes ├── Charles-Severance.jpeg ├── Course-1- getting started with python.pdf ├── Course-1-Programming-for-Everybody-Getting-Started-with-Python ├── Codes │ ├── average_loop.py │ ├── break_loop.py │ ├── checking_grade.py │ ├── compute_gross_pay_py2.py │ ├── compute_gross_pay_py3.py │ ├── conditional_never_execute_p3.py │ ├── convert_input_p3.py │ ├── count_down.py │ ├── counting_loop.py │ ├── definite_loop.py │ ├── definite_loop_friends.py │ ├── filtering_boolean_loop.py │ ├── filtering_loop.py │ ├── for_loop_string.py │ ├── function.py │ ├── in_operator.py │ ├── indentation.py │ ├── indentation_example.py │ ├── infinite_loop.py │ ├── input.py │ ├── input_question_p2.py │ ├── input_question_p3.py │ ├── inside_string.py │ ├── invalid_loop.py │ ├── largest_number_loop.py │ ├── loop.py │ ├── looping_strings.py │ ├── multiple_parameters.py │ ├── new_line.py │ ├── overtime_pay_try_except.py │ ├── overtime_pay_try_except_function.py │ ├── parameters.py │ ├── parsing.py │ ├── parsing_2.py │ ├── return.py │ ├── slicing_string.py │ ├── smallest_number.py │ ├── string_library.py │ ├── sum_loop.py │ ├── test.py │ ├── try_except.py │ ├── try_except_loop_smallest_largest.py │ ├── try_except_loop_total_count_average.py │ └── while.py ├── Exercises │ ├── ex01 │ │ ├── first.py │ │ └── test.py │ ├── ex02 │ │ ├── ex02_02.py │ │ └── ex02_03.py │ ├── ex03 │ │ ├── ex03_01.py │ │ └── ex03_03.py │ ├── ex04 │ │ └── ex04_06.py │ └── ex05 │ │ ├── ex05_01.py │ │ └── ex05_02.py ├── Quizzes │ ├── Quiz Chapter 1.ipynb │ ├── Quiz Chapter 2.ipynb │ ├── Quiz Chapter 3.ipynb │ ├── Quiz Chapter 4.ipynb │ └── Quiz Chapter 5.ipynb └── README.md ├── Course-2- Python Data Structure.pdf ├── Course-2-python_data_structures ├── 10_most_common_words.py ├── 10_most_common_words_short.py ├── Exercises │ ├── ex06 │ │ └── ex06_05.py │ ├── ex07 │ │ ├── ex07_01.py │ │ ├── ex07_02.py │ │ ├── mbox-short.txt │ │ └── words.txt │ ├── ex08 │ │ ├── ex08_04.py │ │ ├── ex08_05.py │ │ ├── mbox-short.txt │ │ └── romeo.txt │ ├── ex09 │ │ ├── ex09_04.py │ │ └── mbox-short.txt │ └── ex10 │ │ ├── ex10_02.py │ │ └── mbox-short.txt ├── Quizzes │ ├── Chapter 10 Quiz.ipynb │ ├── Chapter 6 Quiz.ipynb │ ├── Chapter 7 Quiz.ipynb │ ├── Chapter 8 Quiz.ipynb │ └── Chapter 9 Quiz.ipynb ├── README.md ├── assignment10.2_open_tuples.py ├── assignment7.1_open_read.py ├── assignment7.2_open_seekaverage.py ├── assignment8.4_open_read_split.py ├── assignment8.5_open_read_split.py ├── assignment9.4_dictionaries_count.py ├── average_list.py ├── concat_lists.py ├── count_mbox.py ├── counting_lines.py ├── dictionaries.py ├── dictionaries_count_commonword.py ├── dictionaries_count_prog.py ├── find_day_mbox.py ├── list_methods.py ├── list_worked_exercise.py ├── lists_immutable.py ├── mbox-short.txt ├── newline_p3.py ├── open_function.py ├── range.py ├── romeo.txt ├── searching.py ├── searching_continue_strip.py ├── searching_in_strip.py ├── searching_strip.py ├── slice_lists.py ├── split.py ├── test.py ├── tuples.py ├── tuples_worked_exercise.py └── words.txt ├── Course-3 using Python to access web data.pdf ├── Course-3-python_access_web_data ├── README.md ├── test.py ├── w2_regular_expressions │ ├── Py4Inf-11-Regex-Guide.pdf │ ├── assignment_parse_extract_regex.py │ ├── mbox-short.txt │ ├── notes_regular_expressions.txt │ ├── regex.png │ ├── regex.py │ ├── regex_sum_42.txt │ └── regex_sum_443693.txt ├── w3_networks_sockets │ ├── http.txt │ ├── networked_programs.txt │ ├── socket1.py │ └── urllib1.py ├── w4_web_scraping │ ├── BeautifulSoup.py │ ├── BeautifulSoup.pyc │ ├── ScrappingHTMLDataWithBeautifull.py │ ├── assignment_scrap_href_url.py │ ├── assignment_scrap_span_tags.py │ ├── comments_443695.html │ ├── for_i_in_range.py │ ├── scrappin_a_tags.py │ └── webscraping.txt ├── w5_retrieve_parse_xml │ ├── assignment_parse_retrieve_count_xml.py │ ├── geoxml.py │ ├── web_services.txt │ ├── xml.txt │ ├── xml1.py │ └── xml2.py └── w6_json │ ├── access_api.txt │ ├── api.txt │ ├── assignment_extract_data_json.py │ ├── assignment_google_geojson.py │ ├── geojson.py │ ├── json.txt │ ├── json_notes.py │ └── twitter_api │ ├── get_profile_post.py │ ├── get_twitter_friends.py │ ├── hidden.py │ ├── hidden.pyc │ ├── oauth.py │ ├── oauth.pyc │ ├── twurl.py │ └── twurl.pyc ├── Course-4 Using Databases with Python.pdf ├── Course-4-python_databases ├── README.md ├── w1_oop │ ├── inheritance.py │ ├── inheritance.txt │ ├── object_lifecycle.py │ ├── object_lifecycle.txt │ ├── oop.py │ ├── oop.txt │ └── terminology.txt ├── w2_sql │ ├── assignment_count_email.py │ ├── assignment_intro.db │ ├── crud.txt │ ├── db_intro.txt │ ├── domain_db.sqlite │ ├── emaildb.py │ ├── emaildb.sqlite │ ├── mbox-short.txt │ ├── mbox.txt │ ├── sql1.db │ └── using_db.txt ├── w3_data_models │ ├── Library.xml │ ├── asssignment_musicdb.py │ ├── data_model_table.png │ ├── data_models.txt │ ├── join.txt │ ├── music_mgt.db │ ├── trackdb.sqlite │ ├── trackdb_assign.sqlite │ └── xml_extract_sql.py ├── w4_many_to_many │ ├── assignment_roster_role.py │ ├── exercise_db.png │ ├── exercise_db.sqlite │ ├── exercise_many2many.py │ ├── exercise_roster_m2m.py │ ├── many_to_many.png │ ├── many_to_many.txt │ ├── roster.sqlite │ ├── roster_data.json │ ├── roster_data_assignment.json │ └── trackdb_assign.sqlite └── w5_dbvisualisation │ ├── Capture.JPG │ ├── Geoload-py.png │ ├── geocoding.png │ ├── geocoding.txt │ ├── geodata │ ├── README.txt │ ├── geodata.sqlite │ ├── geodump.py │ ├── geoload.py │ ├── where.data │ ├── where.html │ └── where.js │ ├── gmane │ ├── Chart.bundle.js │ ├── README.txt │ ├── d3.layout.cloud.js │ ├── d3.v3.js │ ├── gbasic.py │ ├── gline.htm │ ├── gline.py │ ├── gline2.htm │ ├── gline3.htm │ ├── gmane.py │ ├── gmodel.py │ ├── gword.htm │ ├── gword.py │ ├── gyear.py │ └── mapping.sqlite │ ├── mailing_list.png │ ├── mailing_lists.txt │ ├── multistep_data_analysis.png │ ├── new-geodumpy.png │ ├── output.png │ ├── page_rank_web_search.txt │ ├── pagerank │ ├── BeautifulSoup.py │ ├── LICENSE │ ├── README.txt │ ├── d3.v2.js │ ├── force.css │ ├── force.html │ ├── force.js │ ├── spdump.py │ ├── spider.js │ ├── spider.py │ ├── spjson.py │ ├── sprank.py │ └── spreset.py │ └── web_crawling.png ├── Course-5 Capstone Retriveing Processing and Visulaisation data.pdf ├── Course-5- Capstone Retrieving Processing and Visualizing Data with Python ├── README.md ├── certificate.pdf ├── certificate_Python for Everybody.pdf ├── gmane output │ ├── GLineYearVisualization.jpg │ ├── GWordVisualization.jpg │ ├── Gbasic.jpg │ ├── IndexSQLite.jpg │ ├── TimeLineVisualization.jpg │ ├── index-sqlite.png │ ├── spliteBrowser-1.png │ └── week-6-1.jpg ├── gmane │ ├── .gitignore │ ├── README.txt │ ├── d3.layout.cloud.js │ ├── d3.v2.js │ ├── gbasic.py │ ├── gline.htm │ ├── gline.py │ ├── gmane.py │ ├── gmodel.py │ ├── gword.htm │ ├── gword.py │ ├── gyear.py │ └── mapping.sqlite ├── pageRank Output │ ├── force-2.png │ ├── froce.png │ ├── spdump-2.JPG │ ├── spdump-py.JPG │ └── spider_py.JPG └── pagerank │ ├── .gitignore │ ├── BeautifulSoup.py │ ├── BeautifulSoup.pyc │ ├── LICENSE │ ├── README.txt │ ├── d3.v2.js │ ├── force.css │ ├── force.html │ ├── force.js │ ├── spdump.py │ ├── spider.js │ ├── spider.py │ ├── spjson.py │ ├── sprank.py │ └── spreset.py ├── Honor Certificate.pdf ├── LICENSE ├── Python For Everybody Specialization.pdf ├── Python for Everybody.pdf ├── README.md ├── Slides And PDF ├── 00-Master.ppt ├── Anaconda_CheatSheet.pdf ├── Braile-Slide-Summaries-01-10.doc ├── Pythonlearn-01-Intro.pptx ├── Pythonlearn-01 │ ├── MOOCMap-highres.jpg │ ├── RaspberryPi.jpg │ ├── Raspi_Colour_R.png │ └── clown_car.jpg ├── Pythonlearn-02-Expressions.pptx ├── Pythonlearn-03-Conditional.pptx ├── Pythonlearn-04-Functions.pptx ├── Pythonlearn-05-Iterations.pptx ├── Pythonlearn-06-Strings.pptx ├── Pythonlearn-07-Files.pptx ├── Pythonlearn-08-Lists.pptx ├── Pythonlearn-09-Dictionaries.pptx ├── Pythonlearn-10-Tuples.pptx ├── Pythonlearn-11-Regex.pptx ├── Pythonlearn-12-HTML │ ├── Wikipedia_File_Utf8webgrowth_svg.png │ ├── ascii-cheat-sheet.png │ ├── bytes_decode.png │ ├── str_encode.png │ └── unicode-web-site.png ├── Pythonlearn-12-HTTP.pptx ├── Pythonlearn-13-WebServices.pptx ├── Pythonlearn-14-Objects.pptx ├── Pythonlearn-15-Databases.pptx ├── Pythonlearn-16-Data-Viz.pptx ├── intro-wrapup.pptx ├── program1.py ├── pythonlearn-11-Regex-handout.txt └── pythonlearn-15-Database-handout.txt └── michiganlogo.jpg /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Charles-Severance.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Charles-Severance.jpeg -------------------------------------------------------------------------------- /Course-1- getting started with python.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-1- getting started with python.pdf -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/average_loop.py: -------------------------------------------------------------------------------- 1 | count = 0 2 | sum = 0 3 | 4 | print('Before', count, sum) 5 | 6 | for value in [9, 41, 12, 3, 74, 15]: 7 | count += 1 8 | sum += value 9 | print(count, sum, value) 10 | print('After', count, sum, sum / count) 11 | 12 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/break_loop.py: -------------------------------------------------------------------------------- 1 | while True: 2 | line = input('> ') 3 | if line == 'done': 4 | break 5 | # Breaks out of loop if done 6 | # Otherwise, the loop continues and prints line. 7 | print(line) 8 | print('Done!') -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/checking_grade.py: -------------------------------------------------------------------------------- 1 | score = raw_input("Your score: ") 2 | 3 | try: 4 | score_int = float(score) 5 | # If try fails, except runs. 6 | except: 7 | score_int = -1 8 | 9 | if 0.90 <= score_int <= 1.00: 10 | print "A" 11 | elif 0.80 <= score_int < 0.90: 12 | print "B" 13 | elif 0.70 <= score_int < 0.80: 14 | print "C" 15 | elif 0.60 <= score_int < 0.70: 16 | print "D" 17 | elif 0.00 <= score_int < 0.60: 18 | print "F" 19 | else: 20 | print "Please enter a score between 0.00 and 1.00." -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/compute_gross_pay_py2.py: -------------------------------------------------------------------------------- 1 | hrs = raw_input("Enter Hours: ") 2 | hrs = float(hrs) 3 | 4 | hourly_rate = raw_input("Enter Hourly Rate: ") 5 | hourly_rate = float(hourly_rate) 6 | 7 | gross_pay = hourly_rate * hrs 8 | print "Gross pay:", gross_pay 9 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/compute_gross_pay_py3.py: -------------------------------------------------------------------------------- 1 | hrs = input('Enter Hours: ') 2 | hrs = float(hrs) 3 | 4 | hourly_rate = input('Enter Hourly Rate: ') 5 | hourly_rate = float(hourly_rate) 6 | 7 | gross_pay = hourly_rate * hrs 8 | print("Gross pay:", gross_pay) 9 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/conditional_never_execute_p3.py: -------------------------------------------------------------------------------- 1 | x = input('What number do you like?') 2 | x = int(x) 3 | # Convert the input into an integer for use with comparison operators 4 | 5 | if x < 2: 6 | print('Below 2') 7 | elif x < 20: 8 | print('Below 20') 9 | elif x < 10: 10 | print('Below 10') 11 | # This will never run. 12 | else: 13 | print('Above 20') 14 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/convert_input_p3.py: -------------------------------------------------------------------------------- 1 | inp = input('Europe floor? ') 2 | # input() runs first, then it stores the value into variable inp 3 | usf = int(inp) + 1 4 | print("US floor", usf) 5 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/count_down.py: -------------------------------------------------------------------------------- 1 | n = 6 2 | while n > 0: 3 | n -= 1 # augmented assignment & iteration variable 4 | print(n) 5 | print('Blastoff!') 6 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/counting_loop.py: -------------------------------------------------------------------------------- 1 | count = 0 2 | print('Before', count) 3 | 4 | for thing in [9, 41, 12, 3, 74, 15]: 5 | count += 1 6 | # zork = zork + 1 7 | print(count, thing) 8 | 9 | print('After', count) 10 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/definite_loop.py: -------------------------------------------------------------------------------- 1 | for i in [5, 4, 3, 2, 1]: 2 | print i 3 | # Prints 5, 4, 3, 2, 1 using an iteration variable in a finite loop 4 | 5 | print 'Blastoff!' 6 | 7 | 8 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/definite_loop_friends.py: -------------------------------------------------------------------------------- 1 | friends = ['Joseph', 'Glenn', 'Sally'] 2 | 3 | for friend in friends: 4 | print('Happy New Year', friend) 5 | 6 | print('Done!') 7 | 8 | # Iteration variable friend 9 | # Friend changes each time through the loop -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/filtering_boolean_loop.py: -------------------------------------------------------------------------------- 1 | found = False 2 | 3 | print('Before', found) 4 | 5 | for value in [9, 41, 12, 3, 74, 15]: 6 | if value == 3: 7 | found = True 8 | break 9 | print(found, value) 10 | 11 | print('After', found) -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/filtering_loop.py: -------------------------------------------------------------------------------- 1 | print('Before') 2 | 3 | for value in [9, 41, 12, 74, 15]: 4 | if value > 20: 5 | print('Large number', value) 6 | 7 | print('After') -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/for_loop_string.py: -------------------------------------------------------------------------------- 1 | word = 'banana' 2 | 3 | for letter in word: 4 | print(letter) 5 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/function.py: -------------------------------------------------------------------------------- 1 | # Function is some reusable code --> helps to reduce repeat codes 2 | # Takes arguments as input 3 | # Does computation then returns a result 4 | 5 | # def to define a function 6 | # function_name(argument) to invoke with argument 7 | 8 | def hello(): 9 | print('Hello!') 10 | name = input('Who are you? ') 11 | print('Welcome ' + name + ".") 12 | 13 | hello() 14 | # Invoking function 15 | 16 | # Built-in function 17 | big = max('1, 2, 3') 18 | print(big) 19 | # Input: string '1, 2, 3' 20 | # Function: max() --> takes input to produce output 21 | # Result: '3' 22 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/in_operator.py: -------------------------------------------------------------------------------- 1 | fruit = 'baanana' 2 | 3 | if 'n' in fruit: 4 | print('Found it!') 5 | 6 | if "m" in fruit: 7 | print('Found it!') 8 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/indentation.py: -------------------------------------------------------------------------------- 1 | # Increase indent for "if" or "for" statement 2 | 3 | # Maintain indent to indicate scope (which lines affected by if/for statement) 4 | 5 | # Reduce indent to indicate end of the block (if/for statement) 6 | 7 | # Blank lines ignored 8 | 9 | # Comments ignored 10 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/indentation_example.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | if x > 2: 3 | print 'Bigger than 2' 4 | print 'Still bigger' 5 | print 'Done with 2' 6 | 7 | for i in range(5): 8 | print i 9 | if i > 2: 10 | print 'Bigger than 2' 11 | print 'Done with i', i 12 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/infinite_loop.py: -------------------------------------------------------------------------------- 1 | n = 5 2 | while n > 0: 3 | print 'Lather' 4 | print 'Rinse' 5 | 6 | print 'Dry off!' 7 | 8 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/input.py: -------------------------------------------------------------------------------- 1 | # This program says hello and asks for my name. 2 | 3 | print ('Hello World!') 4 | print('What is your name?') # ask for their name 5 | myName = input() 6 | 7 | print('It is good to meet you, ' + myName) 8 | 9 | print('The length of your name is:') 10 | print(len(myName)) 11 | 12 | print('What is your age?') # ask for their age 13 | myAge = input() 14 | 15 | print('You will be ' + str(int(myAge) + 1) + ' in a year.') 16 | 17 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/input_question_p2.py: -------------------------------------------------------------------------------- 1 | nam = raw_input('Who are you?') 2 | print 'Welcome', nam 3 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/input_question_p3.py: -------------------------------------------------------------------------------- 1 | nam = input('Who are you?') 2 | print('Welcome', nam) 3 | 4 | name = raw_input('Enter your name') 5 | print "Hello " + name 6 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/inside_string.py: -------------------------------------------------------------------------------- 1 | fruit = 'banana' 2 | # Character starts from 0 3 | letter = fruit[1] 4 | print letter 5 | 6 | n = 3 7 | letter_2 = fruit[n - 1] 8 | print letter_2 9 | 10 | print len(fruit) 11 | # Counts the number of characters 12 | # 6 13 | # len() is an in-built function -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/invalid_loop.py: -------------------------------------------------------------------------------- 1 | n = 0 2 | while n > 0: 3 | print 'Zero trip loop' 4 | 5 | print 'Zero trip loop!' 6 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/largest_number_loop.py: -------------------------------------------------------------------------------- 1 | # Finding the largest value 2 | largest_so_far = -1 3 | print('Before', largest_so_far) 4 | 5 | for the_num in [9, 41, 12, 3, 74, 15]: 6 | if the_num > largest_so_far: 7 | largest_so_far = the_num 8 | print(largest_so_far, the_num) 9 | 10 | print('After', largest_so_far) -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/loop.py: -------------------------------------------------------------------------------- 1 | n = 5 2 | while n > 0: 3 | print n 4 | n -= 1 5 | # n = n - 1 6 | print 'Blastoff!' 7 | print n 8 | 9 | # Results 10 | # 5 --> print 5 then subtract 1 = 4 11 | # 4 --> print 4 then subtract 1 = 3 12 | # 3 13 | # 2 14 | # 1 --> print 1 then subtract 1 = 0 15 | # Blastoff 16 | # 0 --> result of last subtraction 17 | 18 | # Loops (repeated steps) have iteration variables, n 19 | # Iteration variable changes each time through a loop 20 | # Often go through a sequence of numbers 21 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/looping_strings.py: -------------------------------------------------------------------------------- 1 | fruit = 'banana' 2 | 3 | index = 0 4 | 5 | while index < len(fruit): 6 | letter = fruit[index] 7 | print(index, letter) 8 | index += 1 9 | 10 | print (" ") 11 | 12 | for letter in fruit: 13 | print(letter) 14 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/multiple_parameters.py: -------------------------------------------------------------------------------- 1 | def addtwo(a, b): 2 | added = a + b 3 | return added 4 | # a,b are parameters 5 | 6 | x = addtwo(3, 5) 7 | print(x) 8 | # 3,5 are arguments 9 | 10 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/new_line.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/new_line.py -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/overtime_pay_try_except.py: -------------------------------------------------------------------------------- 1 | # Employees get 1.5x the hourly rate for hours work above 40 hours. 2 | # Error message for non-number input. 3 | # One prompt then quit. No loop for this! 4 | 5 | # Concepts: if, elif, else, try, except, input and print 6 | 7 | 8 | hrs = raw_input("Enter Hours: ") 9 | hrs_int = float(hrs) 10 | 11 | hourly_rate = raw_input("Hourly Rate: ") 12 | hourly_rate_int = float(hourly_rate) 13 | overtime_multiplier = 1.5 14 | hourly_overtime = hourly_rate_int * overtime_multiplier 15 | 16 | if hrs_int <= 40: 17 | gross_pay = hrs_int * hourly_rate_int 18 | print gross_pay 19 | else: 20 | hrs_over = hrs_int - 40 21 | gross_pay_overtime = (40 * hourly_rate_int) + (hrs_over * hourly_overtime) 22 | print gross_pay_overtime 23 | 24 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/overtime_pay_try_except_function.py: -------------------------------------------------------------------------------- 1 | # Employees get 1.5x the hourly rate for hours work above 40 hours. 2 | # Error message for non-number input. 3 | # One prompt then quit. No loop for this! 4 | 5 | # Concepts: if, elif, else, try, except, input, print, and function 6 | 7 | 8 | hourly_rate = raw_input("Hourly rate: ") 9 | hourly_rate_int = float(hourly_rate) 10 | 11 | hours = raw_input("Number of hours: ") 12 | hours_int = float(hours) 13 | 14 | 15 | def computepay(h, r): 16 | overtime_r = r * 1.5 17 | 18 | if h < 40: 19 | return h * r 20 | else: 21 | return (40 * r) + (h - 40) * overtime_r 22 | 23 | p = computepay(hours_int, hourly_rate_int) 24 | print p 25 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/parameters.py: -------------------------------------------------------------------------------- 1 | def greet(lang): 2 | if lang == 'es': 3 | print 'Hola' 4 | elif lang == 'fr': 5 | print 'Bonjour' 6 | else: 7 | print 'Hello' 8 | 9 | greet('') -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/parsing.py: -------------------------------------------------------------------------------- 1 | # Want to extract domain hotmail.com 2 | data = 'From ritchie_ng@hotmail.com Tues May 31' 3 | at_position = data.find('@') 4 | print(at_position) 5 | 6 | space_position = data.find(' ', at_position) 7 | # Starting from at_position, where's the next space 8 | print(space_position) 9 | 10 | host = data[at_position + 1: space_position] 11 | print(host) -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/parsing_2.py: -------------------------------------------------------------------------------- 1 | text = "X-DSPAM-Confidence: 0.8475"; 2 | 3 | find_0 = text.find('0') 4 | find_5 = text.find('5') 5 | 6 | number_extraction = float(text[find_0: find_5 + 1]) 7 | print(number_extraction) -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/return.py: -------------------------------------------------------------------------------- 1 | # Return Values 2 | # Function can return a value 3 | # If you want to do something in the function, use return. 4 | 5 | def greet(lang): 6 | # lang is the parameter 7 | if lang == 'es': 8 | return "Hola" 9 | elif lang == 'fr': 10 | return "Bonjour" 11 | else: 12 | return "Hello" 13 | 14 | print(greet('fr'), "Monster") 15 | # fr is the argument 16 | print(greet('es'), "Monster") 17 | # es is the argument 18 | print(greet('en'), "Ritchie") 19 | # en is the argument 20 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/slicing_string.py: -------------------------------------------------------------------------------- 1 | s = 'Monty Python' 2 | print(s[0: 4]) 3 | # Up to but not including 4 | 5 | print(s[6: 7]) 6 | 7 | print(s[6: 20]) 8 | # Only prints 6 to 11 9 | 10 | print(s[:2]) 11 | # Up to but not including 12 | 13 | print(s[8:]) 14 | # From 8 until end 15 | 16 | print(s[:]) 17 | # Prints all 18 | 19 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/smallest_number.py: -------------------------------------------------------------------------------- 1 | smallest = None 2 | # Seen nothing so far, so will wait. 3 | 4 | print('Before', smallest) 5 | 6 | for value in [9, 41, 12, 3, 74, 15]: 7 | if smallest is None: 8 | smallest = value 9 | # This will happen the first time where smallest = 9 10 | # After the first time, it'll be permanently false 11 | elif value < smallest: 12 | smallest = value 13 | # This runs until the loop iterates through the array 14 | print(smallest, value) 15 | 16 | print('After', smallest) 17 | 18 | # Only use is when checking for None or False or True 19 | # Other times, use comparison operator == 20 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/string_library.py: -------------------------------------------------------------------------------- 1 | greet = ' Hello Bob ' 2 | 3 | zap = greet.lower() 4 | print(zap) 5 | # Converts to lowercase 6 | 7 | large = greet.upper() 8 | print(large) 9 | # Converts to uppercase 10 | 11 | what_type = type(greet) 12 | print(what_type) 13 | 14 | what_dir = greet.find('lo') 15 | print(what_dir) 16 | # Where is this in the string. 17 | 18 | replace_name = greet.replace('Bob', 'Jane') 19 | print(replace_name) 20 | # It doesn't change the value of greet 21 | 22 | replace_letter = greet.replace('o', 'X') 23 | print(replace_letter) 24 | 25 | strip_left = greet.lstrip() 26 | strip_right = greet.rstrip() 27 | strip_all = greet.strip() 28 | 29 | print(strip_all) 30 | print(strip_left) 31 | print(strip_right) 32 | 33 | start_with = greet.startswith('Hello') 34 | print(start_with) 35 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/sum_loop.py: -------------------------------------------------------------------------------- 1 | zork = 0 2 | print('Before', zork) 3 | 4 | for thing in [9, 41, 12, 3, 74, 15]: 5 | zork += thing 6 | # zork = zork + thing 7 | print(zork, thing) 8 | 9 | print('After', zork) 10 | 11 | 12 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/test.py: -------------------------------------------------------------------------------- 1 | x = 'From marquard@uct.ac.za' 2 | find_at = x.find('@') 3 | print(find_at) 4 | find_dot = x.find('.') 5 | print(find_dot) 6 | 7 | uct_cut = x[find_at + 1: find_dot] 8 | print(uct_cut) 9 | 10 | print(len('banana')*7) 11 | 12 | data = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008' 13 | pos = data.find('.') 14 | print(data[pos:pos+3]) -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/try_except.py: -------------------------------------------------------------------------------- 1 | # Try Fails 2 | first_string = input("What's your age? ") 3 | try: 4 | first_integer_string = int(first_string) 5 | # If try fails, except runs. 6 | # Keep as little code as possible so you know what's failing. 7 | except: 8 | first_integer_string = -1 9 | 10 | print('Your Age', first_integer_string) 11 | 12 | # Try Passes 13 | second_string = input("What's your age? ") 14 | try: 15 | second_integer_string = int(second_string) 16 | # If try fails, except runs. 17 | except: 18 | second_integer_string = -1 19 | 20 | print('Your Age, Second Try', second_integer_string) 21 | 22 | # Proper Error Log 23 | third_strong = input("What's your age? ") 24 | try: 25 | third_integer_string = int(third_strong) 26 | except: 27 | third_integer_string = -1 28 | 29 | if third_integer_string > 0: 30 | print('Your age:', third_integer_string) 31 | else: 32 | print('Error: not a number') 33 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/try_except_loop_smallest_largest.py: -------------------------------------------------------------------------------- 1 | largest = None 2 | smallest = None 3 | 4 | while True: 5 | num = raw_input('Enter a number: ') 6 | 7 | # Handle edge cases 8 | if num == "done": 9 | break 10 | if len(num) < 1: 11 | break 12 | # Allows user to press enter to complete 13 | 14 | # Work is done 15 | try: 16 | num_int = float(num) 17 | # If try fails, except runs 18 | except: 19 | print "Invalid input" 20 | continue 21 | # Jumps to the start of the loop without running any code below this line 22 | 23 | if smallest is None: 24 | smallest = num 25 | # This will be permanently false after the first iteration. 26 | elif num < smallest: 27 | smallest = num 28 | # Replaces the iteration variable with smaller input num 29 | 30 | if largest is None: 31 | largest = num 32 | # This will be permanently false after the first iteration. 33 | elif num > largest: 34 | largest = num 35 | # Replaces the iteration variable with larger input num 36 | 37 | print "Maximum is " + largest 38 | print "Minimum is " + smallest 39 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/try_except_loop_total_count_average.py: -------------------------------------------------------------------------------- 1 | count = 0 2 | total = 0 3 | 4 | while True: 5 | inp = raw_input('Enter a number: ') 6 | # Don't count here. Because you will count the last entry which is NaN 7 | 8 | # Handle the edge cases 9 | if inp == 'done': 10 | break 11 | # Breaks out of loop to print "Done!" 12 | if len(inp) < 1: 13 | break 14 | # Allows user to enter an empty line to print "Done!" 15 | 16 | # Do the work. 17 | try: 18 | num = float(inp) 19 | except: 20 | print "Invalid input" 21 | print " " 22 | continue 23 | # continue jumps up to loop without running the code below 24 | count += 1 25 | # count = count + 1 26 | total += num 27 | # total = total + num 28 | print num, total, count 29 | print " " 30 | 31 | print " " 32 | print "Results:" 33 | print "Count = ", count 34 | print "Total = ", total 35 | print "Average = ", total / count 36 | 37 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Codes/while.py: -------------------------------------------------------------------------------- 1 | while True: 2 | print('Who are you?') 3 | name = input() 4 | if name != 'Joe': 5 | continue 6 | print('Hello Joe. What is the password?') 7 | password = input() 8 | if password == 'swordfish': 9 | break 10 | print('Access granted.') -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Exercises/ex01/first.py: -------------------------------------------------------------------------------- 1 | print('hello world') -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Exercises/ex01/test.py: -------------------------------------------------------------------------------- 1 | print('test') 2 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Exercises/ex02/ex02_02.py: -------------------------------------------------------------------------------- 1 | # Welcoming the user 2 | name = input("Enter your name") 3 | print("Hello", name) -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Exercises/ex02/ex02_03.py: -------------------------------------------------------------------------------- 1 | # Ask user to input hours worked and rate 2 | hrs = input("Enter Hours:") 3 | rate = input("Enter Rate:") 4 | 5 | # Compute pay and print 6 | pay = float(hrs) * float(rate) 7 | print("Pay:", pay) -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Exercises/ex03/ex03_01.py: -------------------------------------------------------------------------------- 1 | # Asking input from user on hours and rate 2 | hrs = input("Enter Hours:") 3 | rate = input("Rate per Hour:") 4 | 5 | # Converting the input into float numbers 6 | h = float(hrs) 7 | r = float(rate) 8 | 9 | # Print gross pay using a multiplier for rates above 40 hours 10 | if h <= 40.0 : 11 | print(h * r) 12 | else : 13 | multiplier = 1.5 14 | print((40 * r) + ((h - 40) * r * multiplier)) -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Exercises/ex03/ex03_03.py: -------------------------------------------------------------------------------- 1 | score = input("Enter Score: ") 2 | try : 3 | sc = float(score) 4 | except : 5 | print("Error, please enter a number.") 6 | quit() 7 | 8 | if sc >= 1.0 : 9 | print("Please enter a number smaller than or equal to 1.0.") 10 | elif sc >= 0.9 : 11 | print("A") 12 | elif sc >= 0.8 : 13 | print("B") 14 | elif sc >= 0.7 : 15 | print("C") 16 | elif sc >= 0.6 : 17 | print("D") 18 | elif sc >= 0.0 : 19 | print("F") 20 | else : 21 | print("Please enter a number greater than or equal to 0.0.") -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Exercises/ex04/ex04_06.py: -------------------------------------------------------------------------------- 1 | # Defining the "computepay" function to return the gross pay according to schedule 2 | def computepay(h, r): 3 | if h <= 40.0 : 4 | return h * r 5 | else : 6 | m = 1.5 7 | return (40 * r) + ((h - 40) * r * m) 8 | 9 | 10 | # Asking input from user on hours and rate 11 | hrs = input("Enter Hours: ") 12 | rate = input("Rate per Hour: ") 13 | 14 | # Converting the input into float numbers 15 | h = float(hrs) 16 | r = float(rate) 17 | 18 | # Print gross pay using a multiplier for rates above 40 hours 19 | print(computepay(h, r)) -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Exercises/ex05/ex05_01.py: -------------------------------------------------------------------------------- 1 | # Initialising variables 2 | count = 0 3 | total = 0 4 | 5 | # Starting loop 6 | while True: 7 | # Ask for the user input 8 | sval = input('Enter a number: ') 9 | 10 | # If user types 'done' then exit 11 | if sval == 'done': 12 | break 13 | 14 | # Trying to convert user input to a value, but if it is not working, give an error message 15 | try: 16 | fval = float(sval) 17 | except: 18 | print('Invalid input') 19 | continue 20 | 21 | # Adjusting counters 22 | count = count + 1 23 | total = total + fval 24 | 25 | # Printing the total, the number of inputs and the average 26 | print(total, count, total/count) -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Exercises/ex05/ex05_02.py: -------------------------------------------------------------------------------- 1 | # Initialising variables 2 | smallest = None 3 | largest = None 4 | 5 | # Starting loop 6 | while True: 7 | # Ask for the user input 8 | sval = input('Enter a number: ') 9 | 10 | # If user types 'done' then exit 11 | if sval == 'done': break 12 | 13 | # Trying to convert user input to a value, but if it is not working, give an error message 14 | try: 15 | fval = float(sval) 16 | except: 17 | print('Invalid input') 18 | continue 19 | 20 | # Adjusting counters 21 | if ((smallest is None) or (fval < smallest)): 22 | smallest = int(fval) 23 | if ((largest is None) or (fval > largest)): 24 | largest = int(fval) 25 | 26 | # Printing the total, the number of inputs and the average 27 | print('Maximum is', largest) 28 | print('Minimum is', smallest) -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Quizzes/Quiz Chapter 1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1. When Python is running in the interactive mode and displaying the chevron prompt (>>>) - what question is Python asking you?\n", 8 | " - What would you like to do?\n", 9 | "2. What will the following program print out: >>> x = 15; >>> x = x + 5; >>> print x\n", 10 | " - 20\n", 11 | "3. Python scripts (files) have names that end with:\n", 12 | " - .py\n", 13 | "4. Which of these words are reserved words in Python ?\n", 14 | " - if\n", 15 | " - break\n", 16 | "5. What is the proper way to say “good-bye” to Python?\n", 17 | " - quit()\n", 18 | "6. Which of the parts of a computer actually executes the program instructions?\n", 19 | " - Central Processing Unit\n", 20 | "7. What is \"code\" in the context of this course?\n", 21 | " - A sequence of instructions in a programming language\n", 22 | "8. A USB memory stick is an example of which of the following components of computer architecture?\n", 23 | " - Secondary Memory\n", 24 | "9. What is the best way to think about a \"Syntax Error\" while programming?\n", 25 | " - The computer did not understand the statement that you entered\n", 26 | "10. Which of the following is not one of the programming patterns covered in Chapter 1?\n", 27 | " - Random steps" 28 | ] 29 | } 30 | ], 31 | "metadata": { 32 | "kernelspec": { 33 | "display_name": "Python 3", 34 | "language": "python", 35 | "name": "python3" 36 | }, 37 | "language_info": { 38 | "codemirror_mode": { 39 | "name": "ipython", 40 | "version": 3 41 | }, 42 | "file_extension": ".py", 43 | "mimetype": "text/x-python", 44 | "name": "python", 45 | "nbconvert_exporter": "python", 46 | "pygments_lexer": "ipython3", 47 | "version": "3.7.4" 48 | } 49 | }, 50 | "nbformat": 4, 51 | "nbformat_minor": 4 52 | } 53 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Quizzes/Quiz Chapter 2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1. In the following code, print 98.6. What is \"98.6\"?\n", 8 | " - A constant\n", 9 | "2. In the following code, x = 42. What is \"x\"?\n", 10 | " - A variable\n", 11 | "3. Which of the following variables is the \"most mnemonic\"?\n", 12 | " - hours\n", 13 | "4. Which of the following is not a Python reserved word?\n", 14 | " - speed\n", 15 | "5. Assume the variable x has been initialized to an integer value (e.g., x = 3). What does the following statement do? x = x + 2\n", 16 | " - Retrieve the current value for x, add two to it and put the sum back into x\n", 17 | "6. Which of the following elements of a mathematical expression in Python is evaluated first?\n", 18 | " -Parenthesis()\n", 19 | "7. What is the value of the following expression\n", 20 | " - 2\n", 21 | "8. What will be the value of x after the following statement executes: x = 1 + 2 * 3 - 8 / 4\n", 22 | " - 5\n", 23 | "9. What will be the value of x when the following statement is executed: x = int(98.6)\n", 24 | " - 98\n", 25 | "10. What does the Python raw_input() function do?\n", 26 | " - Pause the program and read data from the user" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 3", 33 | "language": "python", 34 | "name": "python3" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 3 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython3", 46 | "version": "3.7.4" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 4 51 | } 52 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Quizzes/Quiz Chapter 3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1. What do we do to a Python statement that is immediately after an if statement to indicate that the statement is to be executed only when the if statement is true?\n", 8 | " - Indent the line below the if statement\n", 9 | "2. Which of these operators is not a comparison / logical operator?\n", 10 | " - =\n", 11 | "3. What is true about the following code segment: if x == 5 : print 'Is 5' print 'Is Still 5' print 'Third 5'\n", 12 | " - Depending on the value of x, either all three of the print statements will execute or none of the statements will execute\n", 13 | "4. When you have multiple lines in an if block, how do you indicate the end of the if block?\n", 14 | " - You de-indent the next line past the if block to the same level of indent as the original if statement\n", 15 | "5. You look at the following text: if x == 6 : print 'Is 6' print 'Is Still 6' print 'Third 6'. It looks perfect but Python is giving you an 'Indentation Error' on the second print statement. What is the most likely reason?\n", 16 | " - You have mixed tabs and spaces in the file\n", 17 | "6. What is the Python reserved word that we use in two-way if tests to indicate the block of code that is to be executed if the logical test is false?\n", 18 | " - else\n", 19 | "7. What will the following code print out? x = 0 if x < 2 : print 'Small' elif x < 10 : print 'Medium' else : print 'LARGE' print 'All done'\n", 20 | " - Small All done\n", 21 | "8. For the following code, if x < 2: print 'Below 2' elif x >= 2: print 'Two or more' else: print 'Something else'. What value of 'x' will cause 'Something else' to print out?\n", 22 | " - This code will never print 'Something else' regardless of the value for 'x'\n", 23 | "9. In the following code (numbers added) - which will be the last line to execute successfully? (1) astr = 'Hello Bob'; (2) istr = int(astr); (3) print 'First', istr; (4) astr = '123'; (5) istr = int(astr); (6) print 'Second', istr\n", 24 | " - 1\n", 25 | "10. For the following code: astr = 'Hello Bob' istr = 0 try: istr = int(astr) except: istr = -1. What will the value be for istr after this code executes?\n", 26 | " - -1" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 3", 33 | "language": "python", 34 | "name": "python3" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 3 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython3", 46 | "version": "3.7.4" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 4 51 | } 52 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Quizzes/Quiz Chapter 4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1. Which Python keyword indicates the start of a function definition?\n", 8 | " - def\n", 9 | "2. In Python, how do you indicate the end of the block of code that makes up the function?\n", 10 | " - You de-indent a line of code to the same indent level as the def keyword\n", 11 | "3. In Python what is the raw_input() feature best described as?\n", 12 | " - A built-in function\n", 13 | "4. What does the following code print out? def thing(): print 'Hello'; print 'There'\n", 14 | " - There\n", 15 | "5. In the following Python code, which of the following is an \"argument\" to a function? x = 'banana'; y = max(x); print y\n", 16 | " - x\n", 17 | "6. What will the following Python code print out? def func(x) : print x; func(10); func(20)\n", 18 | " - 10, 20\n", 19 | "7. Which line of the following Python program is useless? def stuff(): print 'Hello' return print 'World' stuff()\n", 20 | " - print 'World'\n", 21 | "8. What will the following Python program print out? def greet(lang): if lang == 'es': return 'Hola' elif lang == 'fr': return 'Bonjour' else: return 'Hello' print greet('fr'),'Michael'\n", 22 | " - Bonjour Michael\n", 23 | "9. What does the following Python code print out? (Note that this is a bit of a trick question and the code has what many would consider to be a flaw/bug - so read carefully). def addtwo(a, b): added = a + b return a; x = addtwo(2, 7); print x\n", 24 | " - 2\n", 25 | "10. What is the most important benefit of writing your own functions?\n", 26 | " - Avoiding writing the same non-trivial code more than once in your program" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 3", 33 | "language": "python", 34 | "name": "python3" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 3 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython3", 46 | "version": "3.7.4" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 4 51 | } 52 | -------------------------------------------------------------------------------- /Course-1-Programming-for-Everybody-Getting-Started-with-Python/Quizzes/Quiz Chapter 5.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1. What is wrong with this Python loop: n = 5; while n > 0 : print n; print 'All done'\n", 8 | " - This loop will run forever.\n", 9 | "2. What does the break statement do?\n", 10 | " - Exits the currently executing loop.\n", 11 | "3. What does the continue statement do?\n", 12 | " - Jumps to the \"top\" of the loop and starts the next iteration.\n", 13 | "4. What does the following Python program print out? tot = 0; for i in [5, 4, 3, 2, 1] : tot = tot + 1; print tot\n", 14 | " - 5\n", 15 | "5. What is the iteration variable in the following Python code: friends = ['Joseph', 'Glenn', 'Sally']; for friend in friends : print 'Happy New Year:', friend; print 'Done!'\n", 16 | " - friend\n", 17 | "6. What is a good description of the following bit of Python code?; zork = 0; for thing in [9, 41, 12, 3, 74, 15] : zork = zork + thing; print 'After', zork\n", 18 | " - Sum all the elements of a list\n", 19 | "7. What will the following code print out? smallest_so_far = -1; for the_num in [9, 41, 12, 3, 74, 15] : if the_num < smallest_so_far : smallest_so_far = the_num; print smallest_so_far\n", 20 | " - -1\n", 21 | "8. What is a good statement to describe the is operator as used in the following if statement: if smallest is None : smallest = value\n", 22 | " - matches both type and value\n", 23 | "9. Which reserved word indicates the start of an \"indefinite\" loop in Python?\n", 24 | " - while\n", 25 | "10. How many times will the body of the following loop be executed? n = 0; while n > 0 : print 'Lather' print 'Rinse'; print 'Dry off!'\n", 26 | " - 0" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 3", 33 | "language": "python", 34 | "name": "python3" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 3 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython3", 46 | "version": "3.7.4" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 4 51 | } 52 | -------------------------------------------------------------------------------- /Course-2- Python Data Structure.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-2- Python Data Structure.pdf -------------------------------------------------------------------------------- /Course-2-python_data_structures/10_most_common_words.py: -------------------------------------------------------------------------------- 1 | # File handle to open the file 2 | fhand = open('romeo.txt') 3 | 4 | # Create dictionary 5 | counts = dict() 6 | 7 | # Populate dictionary 8 | for line in fhand: 9 | # Split the words for looping 10 | words = line.split() 11 | # Loop through words 12 | for word in words: 13 | # 1. If there is no count for the word, add 0 14 | # 2. If there is count for the word, add 1 15 | counts[word] = counts.get(word, 0) + 1 16 | 17 | # Create list 18 | lst = list() 19 | 20 | # Convert to list 21 | for key, val in counts.items(): 22 | lst.append((val, key)) 23 | # Create list for sorting based on value 24 | 25 | # Sort based on highest value 26 | lst.sort(reverse=True) 27 | print lst 28 | 29 | # Print 10 most common words based on key and value 30 | for val, key in lst[:10]: 31 | print key, val -------------------------------------------------------------------------------- /Course-2-python_data_structures/10_most_common_words_short.py: -------------------------------------------------------------------------------- 1 | c = {'a': 10, 'b': 1, 'c': 22} 2 | 3 | # [ ] list comprehension creates a dynamic list 4 | 5 | print sorted([(v, k) for (k, v) in c.items()]) 6 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/Exercises/ex06/ex06_05.py: -------------------------------------------------------------------------------- 1 | text = "X-DSPAM-Confidence: 0.8475"; 2 | 3 | # Finding the colon ':' sign in the text 4 | colon = text.find(':') 5 | # Taking the part of the text after the colon 6 | snumforslice = text[colon+1:] 7 | # Stripping the text taken 8 | snum = snumforslice.strip() 9 | # Printing the converted text to float 10 | print(float(snum)) -------------------------------------------------------------------------------- /Course-2-python_data_structures/Exercises/ex07/ex07_01.py: -------------------------------------------------------------------------------- 1 | # Use words.txt as the file name 2 | fname = input("Enter file name: ") 3 | fh = open(fname) 4 | ftext = fh.read() 5 | ftext = ftext.strip() 6 | print(ftext.upper()) -------------------------------------------------------------------------------- /Course-2-python_data_structures/Exercises/ex07/ex07_02.py: -------------------------------------------------------------------------------- 1 | # Use the file name mbox-short.txt as the file name 2 | fname = input("Enter file name: ") 3 | fh = open(fname) 4 | total = 0 5 | count = 0 6 | for line in fh: 7 | if not line.startswith("X-DSPAM-Confidence:") : 8 | continue 9 | else : 10 | snumt = line[20:] 11 | snum = snumt.strip() 12 | num = float(snum) 13 | total = total + num 14 | count = count + 1 15 | avg = total / count 16 | print("Average spam confidence:",avg) 17 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/Exercises/ex07/words.txt: -------------------------------------------------------------------------------- 1 | Writing programs or programming is a very creative 2 | and rewarding activity You can write programs for 3 | many reasons ranging from making your living to solving 4 | a difficult data analysis problem to having fun to helping 5 | someone else solve a problem This book assumes that 6 | {\em everyone} needs to know how to program and that once 7 | you know how to program, you will figure out what you want 8 | to do with your newfound skills 9 | 10 | We are surrounded in our daily lives with computers ranging 11 | from laptops to cell phones We can think of these computers 12 | as our personal assistants who can take care of many things 13 | on our behalf The hardware in our current-day computers 14 | is essentially built to continuously ask us the question 15 | What would you like me to do next 16 | 17 | Our computers are fast and have vasts amounts of memory and 18 | could be very helpful to us if we only knew the language to 19 | speak to explain to the computer what we would like it to 20 | do next If we knew this language we could tell the 21 | computer to do tasks on our behalf that were reptitive 22 | Interestingly, the kinds of things computers can do best 23 | are often the kinds of things that we humans find boring 24 | and mind-numbing 25 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/Exercises/ex08/ex08_04.py: -------------------------------------------------------------------------------- 1 | # Opening the file 2 | fname = input("Enter file name: ") 3 | fh = open(fname) 4 | 5 | # Defining the list to be built 6 | lst = list() 7 | 8 | # Reading the file line-by-line 9 | for line in fh: 10 | # For each line, splitting the line into words 11 | line.rstrip() 12 | ls = line.split() 13 | # For each word checking if already being on the list and appending to it if not 14 | for word in ls: 15 | if word not in lst: lst.append(word) 16 | 17 | # Sorting the list and printing 18 | lst.sort() 19 | print(lst) -------------------------------------------------------------------------------- /Course-2-python_data_structures/Exercises/ex08/ex08_05.py: -------------------------------------------------------------------------------- 1 | fname = input("Enter file name: ") 2 | if len(fname) < 1 : fname = "mbox-short.txt" 3 | 4 | fh = open(fname) 5 | count = 0 6 | 7 | # Reading file line-by-line 8 | for line in fh: 9 | line.rstrip() 10 | # Looking for lines starting with 'From' 11 | if line.startswith('From ') : 12 | # Splitting lines starting with 'From' 13 | ls = line.split() 14 | # Printing the second item (e-mail address) of the split 15 | print(ls[1]) 16 | count = count + 1 17 | 18 | # Printing the count of e-mail addresses 19 | print("There were", count, "lines in the file with From as the first word") -------------------------------------------------------------------------------- /Course-2-python_data_structures/Exercises/ex08/romeo.txt: -------------------------------------------------------------------------------- 1 | But soft what light through yonder window breaks 2 | It is the east and Juliet is the sun 3 | Arise fair sun and kill the envious moon 4 | Who is already sick and pale with grief 5 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/Exercises/ex09/ex09_04.py: -------------------------------------------------------------------------------- 1 | # Asking user to enter the source file 2 | fname = input("Enter file:") 3 | if len(fname) < 1 : fname = "mbox-short.txt" 4 | # Opening the source file 5 | fhandle = open(fname) 6 | 7 | # Creating a dictionary for the senders' e-mail addresses 8 | sender = dict() 9 | 10 | # Reading file line-by-line 11 | for line in fhandle: 12 | line.rstrip() 13 | # Looking for lines starting with 'From' 14 | if line.startswith('From ') : 15 | # Splitting lines starting with 'From' 16 | ls = line.split() 17 | # Adding the second item (e-mail address) of the split if not yet in the dictionary and counting 18 | sender[ls[1]] = sender.get(ls[1], 0) + 1 19 | 20 | # Selecting the most frequently occuring e-mail address and its count 21 | most_email = None 22 | most_count = None 23 | for email,count in sender.items(): 24 | if most_count is None or count > most_count : 25 | most_email = email 26 | most_count = count 27 | 28 | # Printing the most frequent e-mail address and its number of occurence 29 | print(most_email, most_count) -------------------------------------------------------------------------------- /Course-2-python_data_structures/Exercises/ex10/ex10_02.py: -------------------------------------------------------------------------------- 1 | # Asking user to enter the source file 2 | fname = input("Enter file:") 3 | if len(fname) < 1 : fname = "mbox-short.txt" 4 | # Opening the source file 5 | fhandle = open(fname) 6 | 7 | # Creating a dictionary for the hours 8 | hours = dict() 9 | 10 | # Reading file line-by-line 11 | for line in fhandle: 12 | line.rstrip() 13 | # Looking for lines starting with 'From' 14 | if line.startswith('From ') : 15 | # Splitting lines starting with 'From' 16 | ls = line.split() 17 | # Taking the split with the time 18 | time = ls[5] 19 | # Splitting the time 20 | tm = time.split(':') 21 | # Taking the split with the hour 22 | hour = tm[0] 23 | # Adding the hour in the dictionary and counting 24 | hours[hour] = hours.get(hour, 0) + 1 25 | 26 | # Printing the hours and their counts in ascending order by hours 27 | for k,v in sorted(hours.items()): 28 | print(k,v) -------------------------------------------------------------------------------- /Course-2-python_data_structures/Quizzes/Chapter 10 Quiz.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1. What is the difference between a Python tuple and Python list?\n", 8 | " - Lists are mutable and tuples are not mutable\n", 9 | "2. Which of the following methods work both in Python lists and Python tuples?\n", 10 | " - index()\n", 11 | "3. What will end up in the variable y after this code is executed? x , y = 3, 4\n", 12 | " - 4\n", 13 | "4. In the following Python code, what will end up in the variable y? x = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}; y = x.items()\n", 14 | " - A list of tuples\n", 15 | "5. Which of the following tuples is greater than x in the following Python sequence? x = (5, 1, 3); if ??? > x : ...\n", 16 | " - (6, 0, 0)\n", 17 | "6. What does the following Python code accomplish, assuming the c is a non-empty dictionary? tmp = list(); for k, v in c.items(): tmp.append( (v, k))\n", 18 | " - It creates a list of tuples where each tuple is a value, key pair\n", 19 | "7. If the variable data is a Python list, how do we sort it in reverse order?\n", 20 | " - data.sort(reverse=True)\n", 21 | "8. Using the following tuple, how would you print 'Wed'? days = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')\n", 22 | " - print days[2]\n", 23 | "9. In the following Python loop, why are there two iteration variables (k and v)? c = {'a':10, 'b':1, 'c':22}; for k, v in c.items() : ...\n", 24 | " - Because the items() method in dictionaries returns a list of tuples\n", 25 | "10. Given that Python lists and Python tuples are quite similar - when might you prefer to use a tuple over a list?\n", 26 | " - For a temporary variable that you will use and discard without modifying" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 3", 33 | "language": "python", 34 | "name": "python3" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 3 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython3", 46 | "version": "3.7.4" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 4 51 | } 52 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/Quizzes/Chapter 6 Quiz.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1. What does the following Python Program print out? str1 = \"Hello\"; str2 = 'there'; bob = str1 + str2; print bob\n", 8 | " - Hellothere\n", 9 | "2. What does the following Python program print out? x = '40'; y = int(x) + 2; print y\n", 10 | " - 42\n", 11 | "3. How would you use the index operator [] to print out the letter q from the following string? x = 'From marquard@uct.ac.za'\n", 12 | " - print x[8]\n", 13 | "4. How would you use string slicing [:] to print out 'uct' from the following string? x = 'From marquard@uct.ac.za'\n", 14 | " - print x[14:17]\n", 15 | "5. What is the iteration variable in the following Python code? for letter in 'banana' : print letter\n", 16 | " - letter\n", 17 | "6. What does the following Python code print out? print len('banana')*7\n", 18 | " - 42\n", 19 | "7. How would you print out the following variable in all upper case in Python? greet = 'Hello Bob'\n", 20 | " - print greet.upper()\n", 21 | "8. Which of the following is not a valid string method in Python?\n", 22 | " - twist()\n", 23 | "9. What will the following Python code print out? data = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'; pos = data.find('.'); print data[pos:pos+3]\n", 24 | " - .ma\n", 25 | "10. Which of the following string methods removes whitespace from both the beginning and end of a string?\n", 26 | " - strip()" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 3", 33 | "language": "python", 34 | "name": "python3" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 3 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython3", 46 | "version": "3.7.4" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 4 51 | } 52 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/Quizzes/Chapter 7 Quiz.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1. Given the architecture and terminology we introduced in Chapter 1, where are files stored?\n", 8 | " - Secondary memory\n", 9 | "2. What is stored in a \"file handle\" that is returned from a successful open() call?\n", 10 | " - The handle is a connection to the file's data\n", 11 | "3. What do we use the second parameter of the open() call to indicate?\n", 12 | " - Whether we want to read data from the file or write data to the file\n", 13 | "4. What Python function would you use if you wanted to prompt the user for a file name to open?\n", 14 | " - raw_input()\n", 15 | "5. What is the purpose of the newline character in text files?\n", 16 | " - It indicates the end of one line of text and the beginning of another line of text\n", 17 | "6. If we open a file as follows: xfile = open('mbox.txt'). What statement would we use to read the file one line at a time?\n", 18 | " - for line in xfile:\n", 19 | "7. What is the purpose of the following Python code? fhand = open('mbox.txt'); x = 0; for line in fhand: x = x + 1; print x\n", 20 | " - Count the lines in the file 'mbox.txt'\n", 21 | "8. If you write a Python program to read a text file and you see extra blank lines in the output that are not present in the file input as shown below, what Python string function will likely solve the problem?. From: stephen.marquard@uct.ac.za; From: louis@media.berkeley.edu; From: zqian@umich.edu; From: rjlowe@iupui.edu ...\n", 22 | " - strip()\n", 23 | "9. The following code sequence fails with a traceback when the user enters a file that does not exist. How would you avoid the traceback and make it so you could print out your own error message when a bad file name was entered? fname = raw_input('Enter the file name: '); fhand = open(fname)\n", 24 | " - try / except\n", 25 | "10. What does the following Python code do? fhand = open('mbox-short.txt'); inp = fhand.read()\n", 26 | " - Reads the entire file into the variable inp as a string" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 3", 33 | "language": "python", 34 | "name": "python3" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 3 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython3", 46 | "version": "3.7.4" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 4 51 | } 52 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/Quizzes/Chapter 8 Quiz.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1. How are \"collection\" variables different from normal variables?\n", 8 | " - Collection variables can store multiple values in a single variable\n", 9 | "2. What are the Python keywords used to construct a loop to iterate through a list?\n", 10 | " - for/in\n", 11 | "3. For the following list, how would you print out 'Sally'? friends = [ 'Joseph', 'Glenn', 'Sally']\n", 12 | " - print friends[2]\n", 13 | "4. fruit = 'Banana' fruit[0] = 'b'; print fruit\n", 14 | " - Nothing would print the program fails with a traceback\n", 15 | "5. Which of the following Python statements would print out the length of a list stored in the variable data?\n", 16 | " - print len(data)\n", 17 | "6. What type of data is produced when you call the range() function? x = range(5)\n", 18 | " - A list of integers\n", 19 | "7. What does the following Python code print out? a = [1, 2, 3]; b = [4, 5, 6]; c = a + b; print len(c)\n", 20 | " - 6\n", 21 | "8. Which of the following slicing operations will produce the list [12, 3]? t = [9, 41, 12, 3, 74, 15]\n", 22 | " - t[2:4]\n", 23 | "9. What list method adds a new item to the end of an existing list?\n", 24 | " - append()\n", 25 | "10. What will the following Python code print out? friends = [ 'Joseph', 'Glenn', 'Sally' ]; friends.sort(); print friends[0]\n", 26 | " - Glenn" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 3", 33 | "language": "python", 34 | "name": "python3" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 3 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython3", 46 | "version": "3.7.4" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 4 51 | } 52 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/Quizzes/Chapter 9 Quiz.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1. How are Python dictionaries different from Python lists?\n", 8 | " - Python lists are indexed using integers and dictionaries can use strings as indexes\n", 9 | "2. What is a term commonly used to describe the Python dictionary feature in other programming languages?\n", 10 | " - Associative arrays\n", 11 | "3. What would the following Python code print out? stuff = dict(); print stuff['candy']\n", 12 | " - The program would fail with a traceback\n", 13 | "4. What would the following Python code print out? stuff = dict(); print stuff.get('candy',-1)\n", 14 | " - -1\n", 15 | "5. (T/F) When you add items to a dictionary they remain in the order in which you added them.\n", 16 | " - False\n", 17 | "6. What is a common use of Python dictionaries in a program?\n", 18 | " - Building a histogram counting the occurrences of various strings in a file\n", 19 | "7. Which of the following lines of Python is equivalent to the following sequence of statements assuming that counts is a dictionary? if key in counts: counts[key] = counts[key] + 1 else: counts[key] = 1\n", 20 | " - counts[key] = counts.get(key,0) + 1\n", 21 | "8. In the following Python, what does the for loop iterate through? x = dict() ... for y in x : ...\n", 22 | " - It loops through the keys in the dictionary\n", 23 | "9. Which method in a dictionary object gives you a list of the values in the dictionary?\n", 24 | " - values()\n", 25 | "10. What is the purpose of the second parameter of the get() method for Python dictionaries?\n", 26 | " - To provide a default value if the key is not found" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 2", 33 | "language": "python", 34 | "name": "python2" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 2 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython2", 46 | "version": "2.7.11" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 0 51 | } 52 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/assignment10.2_open_tuples.py: -------------------------------------------------------------------------------- 1 | # 1. Use file handle to open mbox-short.txt 2 | # 2. Loop through lines to read 'From ' 3 | # 3. Split the line to form list of words 4 | # 4. Seek the time 5 | # 5. Split line using ':' 6 | # 6a. Create counts dictionary 7 | # 6b. Map key-value pairs to dictionary 8 | # 7. Convert counts dictionary to list of tuples 9 | # 8. Sort list of tuples based on ascending order 10 | # 9. Print sorted list with a loop 11 | 12 | # 1. Use file handle to open mbox-short.txt 13 | fhandle = open('mbox-short.txt') 14 | 15 | # 6a. Create counts dictionary 16 | counts = dict() 17 | 18 | # 2. Loop through lines to read 'From ' 19 | for line in fhandle: 20 | # 3. Split the line to form list of words 21 | words = line.split() 22 | 23 | # Guardian pattern for blank lines 24 | # Error code: list index out of range 25 | if len(words) < 1: 26 | continue 27 | 28 | # Exclude 'From:' 29 | if words[0] == 'From:': 30 | continue 31 | 32 | # Exclude everything but 'From' 33 | if words[0] != 'From': 34 | continue 35 | 36 | # 4. Seek the time 37 | word = words[5] 38 | 39 | # 5. Split line using ':' 40 | word = word.split(':') 41 | num = word[0] 42 | 43 | # 6b. Map key-value pairs to dictionary 44 | # i. If there is no count for the word, add 0 45 | # ii. If there is count for the word, add 1 46 | counts[num] = counts.get(num, 0) + 1 47 | 48 | # 7. Convert counts dictionary to list of tuples 49 | counts = counts.items() 50 | 51 | # 8. Sort list of tuples based on ascending order 52 | counts_sorted = sorted(counts) 53 | 54 | # 9. Print sorted list with a loop 55 | for k, v in counts_sorted: 56 | print k, v 57 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/assignment7.1_open_read.py: -------------------------------------------------------------------------------- 1 | # Write a program that prompts for a file name, then opens that file and reads through the file, 2 | # and print the contents of the file in upper case. Use the file words.txt to produce the output below. 3 | 4 | # Use words.txt as the file name 5 | fname = raw_input('Enter file name: ') 6 | 7 | # Neat Trick to load file 8 | if len(fname) == 0: 9 | fname = 'words.txt' 10 | 11 | # File handler allows iteration 12 | fhand = open(fname) 13 | 14 | for line in fhand: 15 | line = line.rstrip().upper() 16 | # rstrip then upper 17 | print (line) 18 | 19 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/assignment7.2_open_seekaverage.py: -------------------------------------------------------------------------------- 1 | fname = raw_input('Enter file name: ') 2 | fhandle = open(fname) 3 | 4 | x = 'X-DSPAM-Confidence:' 5 | y = len(x) 6 | count = 0 7 | total = 0 8 | 9 | # print y 10 | # This would show that the number of characters is 19 11 | 12 | # print x[18:] 13 | # This would show that character 19 is ':' 14 | 15 | for line in fhandle: 16 | if line.startswith('X-DSPAM-Confidence:'): 17 | line_number = line[19:] 18 | line_float = float(line_number) 19 | 20 | count += 1 21 | # Loop, iterates through all numbers to count number of numbers 22 | 23 | total += line_float 24 | # Loop, iterates through all numbers to count sum of numbers 25 | 26 | # print line_float 27 | # This shows that we have successfully extracted the floating numbers 28 | 29 | print 'Number of numbers:', count 30 | # This would show that the number of numbers is 27 31 | 32 | print 'Sum of numbers:', total 33 | # This would show that total is 20.2694 34 | 35 | print 'Average of numbers:', total / count 36 | 37 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/assignment8.4_open_read_split.py: -------------------------------------------------------------------------------- 1 | # fname = raw_input('Enter file name: ') 2 | fhandle = open('romeo.txt') 3 | words = [] 4 | 5 | for line in fhandle: 6 | line = line.rstrip().split() 7 | for word in line: 8 | if word not in words: 9 | words.append(word) 10 | 11 | # Use sorted when the sort() returns None 12 | print sorted(words) 13 | 14 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/assignment8.5_open_read_split.py: -------------------------------------------------------------------------------- 1 | fname = raw_input('Enter file name: ') 2 | fhandle = open(fname) 3 | count = 0 4 | 5 | for line in fhandle: 6 | line = line.rstrip() 7 | word = line.split() 8 | 9 | # Guardian pattern 10 | if len(word) < 1: 11 | continue 12 | 13 | # Requires a guardian pattern due to empty line 14 | if word[0] != 'From': 15 | continue 16 | else: 17 | # Counting number 18 | count += 1 19 | 20 | # Shows you have retrieved the emails 21 | print word[1] 22 | 23 | print "There were", count, "lines in the file with From as the first word" -------------------------------------------------------------------------------- /Course-2-python_data_structures/assignment9.4_dictionaries_count.py: -------------------------------------------------------------------------------- 1 | # 1. Get file name 2 | # 2. Open file 3 | # 3. Look for 'From' each line 4 | # 4. Get second word from each line 5 | # 5. Create dictionary 6 | # 6. Map address/count to dictionary 7 | # 7. Count most common using maximum loop 8 | 9 | # 1. Get file name 10 | # fname = raw_input('Enter file: ') 11 | 12 | # 2. Open file 13 | fhandle = open('mbox-short.txt') 14 | 15 | # 5. Create dictionary 16 | counts = dict() 17 | 18 | # 3. Look for 'From' each line 19 | for line in fhandle: 20 | words = line.split() 21 | 22 | # Guardian pattern for blank lines 23 | if len(words) < 1: 24 | continue 25 | 26 | # To ignore all sentences starting from "From:" 27 | if words[0] == 'From:': 28 | continue 29 | 30 | # To ignore all sentences not starting from "From " 31 | if words[0] != 'From': 32 | continue 33 | 34 | word = words[1] 35 | # 6. Map address/count to dictionary 36 | counts[word] = counts.get(word, 0) + 1 37 | # print words 38 | 39 | # 7. Count most common using maximum loop 40 | bigcount = None 41 | bigword = None 42 | 43 | for word, count in counts.items(): 44 | if bigcount is None or count > bigcount: 45 | bigword = word 46 | bigcount = count 47 | 48 | print bigword, bigcount 49 | # print counts 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/average_list.py: -------------------------------------------------------------------------------- 1 | # New way to do it with list compared to using single variables 2 | # Performance: this stores data then calculate and requires more memory 3 | 4 | numlist = list() 5 | 6 | while True: 7 | inp = raw_input('Enter a number: ') 8 | if inp == 'done': 9 | break 10 | value = float(inp) 11 | numlist.append(value) 12 | 13 | average = sum(numlist) / len(numlist) 14 | 15 | print 'Average:', average 16 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/concat_lists.py: -------------------------------------------------------------------------------- 1 | a = [1, 2, 3] 2 | b = [4, 5, 6] 3 | 4 | c = a + b 5 | print c -------------------------------------------------------------------------------- /Course-2-python_data_structures/count_mbox.py: -------------------------------------------------------------------------------- 1 | # fhand is the handle 2 | fhand = open('mbox.txt') 3 | count = 0 4 | 5 | # Python counts the number of lines using the loop 6 | for line in fhand: 7 | count += 1 8 | 9 | print('Line count: ', count) 10 | 11 | # Read it all and return to string 12 | inp = fhand.read() 13 | 14 | print(inp) 15 | print(len(inp)) 16 | print(inp[2: 4]) 17 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/counting_lines.py: -------------------------------------------------------------------------------- 1 | fname = input('Enter the file name: ') 2 | 3 | try: 4 | fhand = open(fname) 5 | 6 | except: 7 | print('File cannot be opened', fname) 8 | exit() 9 | 10 | count = 0 11 | for line in fhand: 12 | if line.startswith('Subject:'): 13 | count += 1 14 | print('There were', count, 'subject lines in', fname) -------------------------------------------------------------------------------- /Course-2-python_data_structures/dictionaries.py: -------------------------------------------------------------------------------- 1 | # Dictionaries store key-value pairs 2 | # They are like list() except that they use keys instead of numbers to look up values 3 | purse = dict() 4 | 5 | # keys: money and candy 6 | purse['money'] = 12 7 | purse['candy'] = 3 8 | 9 | print purse 10 | 11 | # Dictionary is good for adding the number of counts of candy 12 | purse['candy'] += 2 13 | 14 | print purse 15 | print purse['candy'] 16 | 17 | # Check if key in dictionary 18 | print 'candy' in purse 19 | # Returns true 20 | 21 | # You can create a dictionary using curly 22 | bag = {'chuck:': 1, 'ritchie': 0} 23 | print bag 24 | 25 | # Common pattern of counting number of names 26 | counts = dict() 27 | names = ['csev', 'qudas', 'john', 'john'] 28 | for name in names: 29 | if name not in counts: 30 | counts[name] = 1 31 | else: 32 | counts[name] += 1 33 | print counts 34 | 35 | # get built-in function - new idiom 36 | # first parameter: key name 37 | # second parameter: value to give back if key does not exist 38 | ## print counts.get(name, 0) 39 | 40 | # Simplified counting with get() 41 | 42 | for name in names: 43 | counts[name] = counts.get(name, 0) + 1 44 | # No entry: 0 45 | # Entry: +1 46 | print counts 47 | 48 | # This assigns key to the key 49 | for key in counts: 50 | print key, counts[key] 51 | 52 | # Convert dictionaries to list for keys 53 | print counts.keys() 54 | 55 | # Convert dictionaries to list for values 56 | print counts.values() 57 | 58 | # Convert dictionaries to list for tuples (key-value pairs) 59 | print counts.items() 60 | 61 | # Two iteration variables 62 | for a, b in counts.items(): 63 | print a, b 64 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/dictionaries_count_commonword.py: -------------------------------------------------------------------------------- 1 | name = raw_input('Enter file: ') 2 | handle = open(name, 'r') 3 | text = handle.read() 4 | words = text.split() 5 | 6 | # Full dictionary with key-value pairs of words and counts of each words 7 | counts = dict() 8 | for word in words: 9 | counts[word] = counts.get(word, 0) + 1 10 | 11 | # Find largest key-value pair 12 | 13 | bigcount = None 14 | bigword = None 15 | 16 | for word, count in counts.items(): 17 | if bigcount is None or count > bigcount: 18 | bigword = word 19 | bigcount = count 20 | 21 | print bigword, bigcount -------------------------------------------------------------------------------- /Course-2-python_data_structures/dictionaries_count_prog.py: -------------------------------------------------------------------------------- 1 | counts = dict() 2 | 3 | fname = raw_input('Key in your file name: ') 4 | fhandle = open('mbox-short.txt') 5 | 6 | for line in fhandle: 7 | line = line.split() 8 | for word in line: 9 | counts[word] = counts.get(word, 0) + 1 10 | 11 | print 'Counts', counts -------------------------------------------------------------------------------- /Course-2-python_data_structures/find_day_mbox.py: -------------------------------------------------------------------------------- 1 | fhand = open('mbox-short.txt') 2 | 3 | for line in fhand: 4 | line = line.rstrip() 5 | # Skipping lines without 'From ' 6 | if not line.startswith('From '): 7 | continue 8 | words = line.split() 9 | 10 | # Double split pattern 11 | email = words[1] 12 | pieces_email = email.split('@') 13 | print pieces_email[1] 14 | 15 | print words[2] 16 | 17 | # Format of line without split 18 | # From cwen@iupui.edu Thu Jan 3 16:34:40 2008 19 | # 1: email 20 | # 2: day 21 | # 3: month 22 | # 4: day of month 23 | 24 | 25 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/list_methods.py: -------------------------------------------------------------------------------- 1 | # Constructor, gives an empty list 2 | # Or x = [] 3 | x = list() 4 | 5 | print type(x) 6 | 7 | # This gives you the methods available 8 | print dir(x) 9 | 10 | # Appending 11 | x.append('book') 12 | x.append(99) 13 | print x 14 | 15 | 16 | # Is something in List? 17 | if 99 in x: 18 | print '99 in x' 19 | 20 | if 88 not in x: 21 | print '88 not in x' 22 | 23 | # Ordering List 24 | a = [3, 1, 2, 5, 4] 25 | 26 | print a.sort() 27 | 28 | print len(a) 29 | 30 | print max(a) 31 | 32 | print min(a) 33 | 34 | print sum(a) 35 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/list_worked_exercise.py: -------------------------------------------------------------------------------- 1 | fhand = open('mbox-short.txt') 2 | 3 | for line in fhand: 4 | line = line.rstrip() 5 | word = line.split() 6 | # Use a debug line to see what's happening before the line that has issue 7 | # print '++', line 8 | # print word 9 | # You will see that there's an issue with a blank line 10 | 11 | # Fix to the code below using a guardian pattern 12 | if word == []: 13 | continue 14 | 15 | # This code dies whenever there's a blank line 16 | if word[0] != 'From': 17 | continue 18 | 19 | print word[2] 20 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/lists_immutable.py: -------------------------------------------------------------------------------- 1 | fruit = 'Banana' 2 | fruit[0] = 'b' 3 | # This gives an error as you can't change a string 4 | 5 | new_fruit = fruit.upper() 6 | print new_fruit 7 | # You can create a new variable -------------------------------------------------------------------------------- /Course-2-python_data_structures/newline_p3.py: -------------------------------------------------------------------------------- 1 | stuff = 'Hello\nWorld!' 2 | print(stuff) 3 | 4 | print(len(stuff)) 5 | # \n character is counted as one character -------------------------------------------------------------------------------- /Course-2-python_data_structures/open_function.py: -------------------------------------------------------------------------------- 1 | handle = open(filename, mode) 2 | # 1. Name of file 3 | # 2. How to read: 'r' to read or 'w' to write 4 | 5 | # Handle connects the primary and secondary storage 6 | # Simple: name of file must be same folder as the python code 7 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/range.py: -------------------------------------------------------------------------------- 1 | # Range returns a list of numbers from 0 to n - 1, n is the parameter 2 | 3 | print range(4) 4 | 5 | friends = ['Ritchie', 'John', 'Sally'] 6 | length_friends = len(friends) 7 | 8 | print range(length_friends) 9 | 10 | # This is preferred 11 | for friend in friends: 12 | print 'Happy New Year!', friend 13 | 14 | # Equivalent Loop where you need value of i 15 | for i in range(length_friends): 16 | friend = friends[i] 17 | print 'Happy New Year!', friend 18 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/romeo.txt: -------------------------------------------------------------------------------- 1 | But soft what light through yonder window breaks 2 | It is the east and Juliet is the sun 3 | Arise fair sun and kill the envious moon 4 | Who is already sick and pale with grief 5 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/searching.py: -------------------------------------------------------------------------------- 1 | fhand = open('mbox.txt') 2 | for line in fhand: 3 | if line.startswith('From:'): 4 | print line 5 | 6 | # print adds a newline \n to each line therefore explaining the blank lines -------------------------------------------------------------------------------- /Course-2-python_data_structures/searching_continue_strip.py: -------------------------------------------------------------------------------- 1 | fhand = open('mbox.txt') 2 | 3 | for line in fhand: 4 | line = line.rstrip() 5 | # Skip all other lines 6 | if not line.startswith('From:'): 7 | continue 8 | # Process the lines we want 9 | print line 10 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/searching_in_strip.py: -------------------------------------------------------------------------------- 1 | fhand = open('mbox.txt') 2 | 3 | for line in fhand: 4 | line = line.rstrip() 5 | # Skip all other lines 6 | if not '@uct.ac.za' in line: 7 | continue 8 | # Process the lines we want 9 | print line 10 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/searching_strip.py: -------------------------------------------------------------------------------- 1 | fhand = open('mbox.txt') 2 | 3 | for line in fhand: 4 | line = line.rstrip() 5 | if line.startswith('From:'): 6 | print line 7 | 8 | # This strips the line of a new blank line by removing all characters after the end of the string 9 | # This means any whitespace characters are removed like \n in each space -------------------------------------------------------------------------------- /Course-2-python_data_structures/slice_lists.py: -------------------------------------------------------------------------------- 1 | a = [1, 2, 3, 4, 5] 2 | 3 | print len(a) 4 | 5 | print a[0: 2] 6 | 7 | print a[:5] 8 | print a[0:] 9 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/split.py: -------------------------------------------------------------------------------- 1 | abc = 'With three words' 2 | 3 | # Finds the spaces and split them into a list of strings 4 | stuff = abc.split() 5 | print stuff 6 | print len(stuff) 7 | print stuff[0] 8 | 9 | # Can access through a loop 10 | for w in stuff: 11 | print w 12 | 13 | # Many spaces = 1 space 14 | line = 'A lot of spaces' 15 | line_split = line.split() 16 | print line_split 17 | 18 | # Split based on other characters; not spaces 19 | line_semicolon = 'first; second; third' 20 | line_semicolon_split = line_semicolon.split('; ') 21 | print line_semicolon_split 22 | 23 | 24 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/test.py: -------------------------------------------------------------------------------- 1 | stuff = dict() 2 | stuff['money'] = 1 3 | stuff['apples'] = 0 4 | 5 | stuff['google'] = 2 6 | print stuff -------------------------------------------------------------------------------- /Course-2-python_data_structures/tuples.py: -------------------------------------------------------------------------------- 1 | # Tuples are immutable lists with key-value pair 2 | # You can do what you do with lists 3 | # Loop through them 4 | # max(tuple) 5 | # min(tuple) 6 | # CANNOT 7 | # sort() 8 | # append() 9 | 10 | 11 | # Mutable List 12 | x = [1, 2] 13 | x[0] = 2 14 | print x 15 | 16 | print " " 17 | 18 | # Immutable list or tuple, z 19 | z = (5, 4, 3) 20 | # z[0] = 1 21 | # This does not work as a tuple is immutable 22 | print z[0] 23 | 24 | print " " 25 | 26 | # Less in-built functions compared to lists 27 | print dir(z) 28 | 29 | print " " 30 | 31 | # When to use tuples? 32 | # 1. Temporary variables 33 | # 2. Immutable variables 34 | # 3. Saving processing time 35 | 36 | # Tupple assignments 37 | (a, b) = (99, 98) 38 | print (a, b) 39 | 40 | print " " 41 | 42 | # Converting dictionaries to list of tuples to loop through list 43 | d = dict() 44 | d['BMW'] = 10 45 | d['VW'] = 5 46 | for (k, v) in d.items(): 47 | print k, v 48 | 49 | print " " 50 | 51 | t = d.items() 52 | print t 53 | 54 | # Tuples are comparable 55 | 56 | # (0, 1, 20) < (5, 1, 2) 57 | # TRUE 58 | # Compares the first only 59 | # If equal, goes to the next value to compare 60 | 61 | print " " 62 | 63 | # Sorted: takes an unsorted list as input and returns a sorted list 64 | f = sorted(d.items()) 65 | 66 | for k, v in f: 67 | print k, v 68 | 69 | print " " 70 | # Sort by values instead of key 71 | c = {'a': 10, 'b': 1, 'c': 22} 72 | tmp = list() 73 | 74 | for k, v in c.items(): 75 | tmp.append((v, k)) 76 | print tmp 77 | 78 | tmp = sorted(tmp, reverse=True) 79 | print tmp 80 | 81 | -------------------------------------------------------------------------------- /Course-2-python_data_structures/tuples_worked_exercise.py: -------------------------------------------------------------------------------- 1 | # File handle to open the file 2 | fhand = open('romeo.txt') 3 | 4 | # Create dictionary 5 | counts = dict() 6 | 7 | # Populate dictionary 8 | for line in fhand: 9 | # Split the words for looping 10 | words = line.split() 11 | # Loop through words 12 | for word in words: 13 | # 1. If there is no count for the word, add 0 14 | # 2. If there is count for the word, add 1 15 | counts[word] = counts.get(word, 0) + 1 16 | 17 | # Create list 18 | lst = list() 19 | 20 | # Convert to list 21 | for key, val in counts.items(): 22 | lst.append((val, key)) 23 | # Create list for sorting based on value 24 | 25 | # Sort based on highest value 26 | lst.sort(reverse=True) 27 | 28 | # Print 10 most common words based on key and value 29 | for val, key in lst[:10]: 30 | print key, val -------------------------------------------------------------------------------- /Course-2-python_data_structures/words.txt: -------------------------------------------------------------------------------- 1 | Writing programs or programming is a very creative 2 | and rewarding activity You can write programs for 3 | many reasons ranging from making your living to solving 4 | a difficult data analysis problem to having fun to helping 5 | someone else solve a problem This book assumes that 6 | {\em everyone} needs to know how to program and that once 7 | you know how to program, you will figure out what you want 8 | to do with your newfound skills 9 | 10 | We are surrounded in our daily lives with computers ranging 11 | from laptops to cell phones We can think of these computers 12 | as our personal assistants who can take care of many things 13 | on our behalf The hardware in our current-day computers 14 | is essentially built to continuously ask us the question 15 | What would you like me to do next 16 | 17 | Our computers are fast and have vasts amounts of memory and 18 | could be very helpful to us if we only knew the language to 19 | speak to explain to the computer what we would like it to 20 | do next If we knew this language we could tell the 21 | computer to do tasks on our behalf that were reptitive 22 | Interestingly, the kinds of things computers can do best 23 | are often the kinds of things that we humans find boring 24 | and mind-numbing 25 | -------------------------------------------------------------------------------- /Course-3 using Python to access web data.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-3 using Python to access web data.pdf -------------------------------------------------------------------------------- /Course-3-python_access_web_data/README.md: -------------------------------------------------------------------------------- 1 | # Python For Everybody-Coursera 2 | This course is part of the Python for Everybody Specialization 3 | 4 | ## Python Access Wen Data 5 | 6 | 7 | ### About this Course 8 | This course will show how one can treat the Internet as a source of data. We will scrape, parse, and read web data as well as access data using web APIs. We will work with HTML, XML, and JSON data formats in Python. This course will cover Chapters 11-13 of the textbook “Python for Everybody”. To succeed in this course, you should be familiar with the material covered in Chapters 1-10 of the textbook and the first two courses in this specialization. These topics include variables and expressions, conditional execution (loops, branching, and try/except), functions, Python data structures (strings, lists, dictionaries, and tuples), and manipulating files. This course covers Python 3. 9 | 10 | ## Course Syllabus 11 | * **WEEK-1 : Getting Started** 12 | > In this section you will install Python and a text editor. In previous classes in the specialization this was an optional assignment, but in this class it is the first requirement to get started. From this point forward we will stop using the browser-based Python grading environment because the browser-based Python environment (Skulpt) is not capable of running the more complex programs we will be developing in this class. 13 | 14 | * **WEEK-2 : Regular Expressions (Chapter 11)** 15 | > Regular expressions are a very specialized language that allow us to succinctly search strings and extract data from strings. Regular expressions are a language unto themselves. It is not essential to know how to use regular expressions, but they can be quite useful and powerful. 16 | 17 | * **WEEK-3 : Networks and Sockets (Chapter 12)** 18 | > In this section we learn about the protocols that web browsers use to retrieve documents and web applications use to interact with Application Program Interfaces (APIs). 19 | 20 | * **WEEK-4 : Programs that Surf the Web (Chapter 12)** 21 | > In this section we learn to use Python to retrieve data from web sites and APIs over the Internet. 22 | 23 | * **WEEK-5 : Web Services and XML (Chapter 13)** 24 | > In this section, we learn how to retrieve and parse XML (eXtensible Markup Language) data. 25 | 26 | * **WEEK-6 : JSON and the REST Architecture (Chapter 13)** 27 | > In this module, we work with Application Program Interfaces / Web Services using the JavaScript Object Notation (JSON) data format. 28 | 29 | ## Reference 30 | > [Coursera](https://www.coursera.org/learn/python-network-data?specialization=python) 31 | 32 | > [Achievement Certificate](https://github.com/Ashleshk/Python-For-Everybody-Coursera/blob/master/Course-3%20using%20Python%20to%20access%20web%20data.pdf) 33 | -------------------------------------------------------------------------------- /Course-3-python_access_web_data/test.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-3-python_access_web_data/test.py -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w2_regular_expressions/Py4Inf-11-Regex-Guide.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-3-python_access_web_data/w2_regular_expressions/Py4Inf-11-Regex-Guide.pdf -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w2_regular_expressions/assignment_parse_extract_regex.py: -------------------------------------------------------------------------------- 1 | # 1. Import regex 2 | # 2. Read file 3 | # 3. Create 4 | # 4. Look for integers re.findall('[0-9]+', line) 5 | # 5. Convert strings to integers 6 | # 6. Sum integers 7 | 8 | # 1. Import regex 9 | import re 10 | 11 | # 2. Read file 12 | fhandle = open('regex_sum_443693.txt') 13 | 14 | # 3. Create list 15 | numlist = list() 16 | 17 | # 4. Look for integers re.findall('[0-9]+', line) 18 | for line in fhandle: 19 | line = line.rstrip() 20 | # Create lists of numbers 21 | num = re.findall('[0-9]+', line) 22 | 23 | # print num 24 | # confirm that numbers are collated 25 | 26 | # print num 27 | # shows max 3 in a list 28 | 29 | # Skip blank lists 30 | if len(num) < 1: 31 | continue 32 | 33 | elif len(num) == 1: 34 | # 5. Convert strings to integers 35 | num1 = int(num[0]) 36 | numlist.append(num1) 37 | elif len(num) == 2: 38 | num1 = int(num[0]) 39 | num2 = int(num[1]) 40 | numlist.append(num1) 41 | numlist.append(num2) 42 | else: 43 | num1 = int(num[0]) 44 | num2 = int(num[1]) 45 | num3 = int(num[2]) 46 | numlist.append(num1) 47 | numlist.append(num2) 48 | numlist.append(num3) 49 | 50 | # 6. Sum integers in a list 51 | sum_num_int = sum(numlist) 52 | print(len(numlist)) 53 | print(sum_num_int) -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w2_regular_expressions/notes_regular_expressions.txt: -------------------------------------------------------------------------------- 1 | Regular Expression: regex or regexp 2 | - specialised in string matching 3 | - clever wild card expressions for matching and parsing strings 4 | 5 | 6 | Import regex 7 | import re 8 | re.search() 9 | re.findall() -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w2_regular_expressions/regex.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-3-python_access_web_data/w2_regular_expressions/regex.png -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w2_regular_expressions/regex.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | fhand = open('mbox-short.txt') 4 | 5 | # 1. search() 6 | for line in fhand: 7 | line = line.rstrip() 8 | # Find 'From:' at the beginning of line 9 | # ^: beginning of 10 | # True/false if you find or not 11 | if re.search('^From:', line): 12 | print line 13 | 14 | # 1. search() 15 | for line in fhand: 16 | line = line.rstrip() 17 | # * 0 or more digits, any number of times 18 | # . matches any character 19 | if re.search('^X.*:', line): 20 | print line 21 | 22 | # 1. search() 23 | for line in fhand: 24 | line = line.rstrip() 25 | # \S --> non-blank character 26 | # + --> one or more times 27 | # This basically searches for X- with non-blank characters up to : 28 | if re.search('^X-\S+:', line): 29 | print line 30 | 31 | 32 | # 2. findall() 33 | x = 'my 2 favourite numbers are 19 and 24' 34 | # findall: pull out to fill in list 35 | # [0-9] digits and 1 or more digit 36 | # search condition in x 37 | y = re.findall('[0-9]+', x) 38 | print y 39 | 40 | # Find all one or more vowels 41 | y = re.findall('[AEIOU]+', x) 42 | print y 43 | 44 | # 3. Greedy matching expanding out to the max : 45 | x = 'From: Using the : character' 46 | y = re.findall('^F.+:', x) 47 | print y 48 | 49 | # 4. Non-greedy matching to the nearest : 50 | x = 'From: Using the : character' 51 | y = re.findall('^F.+?:', x) 52 | print y 53 | 54 | # \S+ at least one non blank before and after @ 55 | # Greedy 56 | # ( ) gives you what you're looking for 57 | # \S --> non-blank character 58 | line = 'From stephen@u.nus.edu do not' 59 | a = re.findall('^From (\S+@\S+)', line) 60 | print a 61 | 62 | # Extracting only the domain 63 | line = 'From stephen@u.nus.edu do not' 64 | # find @ in line 65 | # ( ) gives you what you're looking for 66 | # [^ ] non blank 67 | # * 0 or more 68 | b = re.findall('@([^ ]*)', line) 69 | print b 70 | 71 | # ^From --> starts From 72 | # .* --> any character before @ 73 | # ( ) --> what you will get 74 | # [^ ]* --> non-blank characters as many as them 75 | c = re.findall('^From .*@([^ ]*)', line) 76 | print c 77 | 78 | # SPAM CONFIDENCE Example 79 | fhand = open('mbox-short.txt') 80 | numlist = list() 81 | for line in fhand: 82 | line = line.rstrip() 83 | stuff = re.findall('^X-DSPAM-Confidence: ([0-9.]+)', line) 84 | # skip those you don't find 85 | print stuff 86 | if len(stuff) != 1: 87 | continue 88 | num = float(stuff[0]) 89 | numlist.append(num) 90 | print 'Maximium:', max(numlist) -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w3_networks_sockets/http.txt: -------------------------------------------------------------------------------- 1 | HyperText Transport Protocol (HTTP) 2 | Set of rules to allow web browsers to retrieve web documents from 3 | servers around the world 4 | 5 | Internet Engineering Task Force (IETF) 6 | 7 | Uniform Resource Locator (URL) 8 | Protocol: http 9 | Host: www.ritchieng.com 10 | Document: /page1.html 11 | 12 | Request Response Cycle 13 | Click --> event in browser 14 | Browser makes connection to server 15 | Makes application GET request 16 | Server respond 17 | 18 | 1. GET 19 | 2. RETRIEVE 20 | 3. DISPLAY 21 | 22 | telnet command 23 | telnet www.dr-chuck.com 80 24 | GET http://www.dr-chuck.com/page1.htm HTTP/1.0 25 | PRESS ENTER TWICE 26 | RETRIEVES 27 | DISPLAY 28 | 29 | Summary 30 | Open a socket 31 | Send a GET 32 | Send a new line 33 | GET back the document 34 | Display the document 35 | DO IT OVER AND OVE -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w3_networks_sockets/networked_programs.txt: -------------------------------------------------------------------------------- 1 | Transport Control Protocol (TCP) 2 | Built on top of Internet Protocol (IP) 3 | 4 | 5 | TCP Connections or Sockets 6 | Internet or network socket is an endpoint of a bidirectional 7 | inter-process communication flow across an IP-based computer network, 8 | such as the Internet. 9 | 10 | Like a phone call between two applications 11 | 12 | fhandle --> port to look out to exchange data through a socket 13 | 14 | TCP Port Numbers 15 | Port is an application-specific or process-specific 16 | communications endpoint 17 | 18 | Allows multiple networked applications to coexist on same server 19 | 20 | List of well-known TCP port numbers 21 | HTTP 80 22 | HTTPS 443 23 | SSH 24 | IMAP 25 | POP 26 | DNS 27 | SMTP 25 28 | FTP 29 | 30 | Similar to extensions in phone numbers 31 | 32 | Example: 33 | 1. Domain name: www.ritchieng.com 34 | 2. IP Address: 199.10.0.10 35 | 2a. Email: port 25 36 | 2b. Login: port 23 37 | 2c. Web server: port 80 38 | 2d. Personal mail: port 109 39 | -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w3_networks_sockets/socket1.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | # CREATE ENDPOINT 4 | # socket - library 5 | # socket - method within library 6 | # (socket.AF_INET, socket.SOCK_STREAM) --> make internet socket 7 | mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 8 | 9 | # PUSH ENDPOINT THROUGH WEB 10 | # Establish connection between me and host with port 80 11 | # Want the other end to be www.py4inf.com 12 | # Most like the open() call to read a file 13 | mysock.connect(('www.pythonlearn.com', 80)) 14 | # www.py4inf.com --> host 15 | # 80 --> port 16 | 17 | # SEND REQUEST 18 | mysock.send('GET http://data.pr4e.org/intro-short.txt HTTP/1.0\n\n') 19 | 20 | # LOOP TO READ 21 | while True: 22 | # Call to receive 23 | data = mysock.recv(512) 24 | # When last data, break the loop 25 | if len(data) < 1: 26 | break 27 | # Get data 28 | print(data) 29 | # Close the socket 30 | mysock.close() 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w3_networks_sockets/urllib1.py: -------------------------------------------------------------------------------- 1 | # Using urllib which is easier than socket 2 | # socket is like a phonecall where you determine the connection 3 | 4 | import urllib 5 | fhand = urllib.urlopen('http://www.py4inf.com/code/romeo.txt') 6 | print(type(fhand)) 7 | # for line in fhand: 8 | # print line.strip() 9 | 10 | # You can count the words! 11 | 12 | counts = dict() 13 | for line in fhand: 14 | print(line.strip()) 15 | words = line.split() 16 | for word in words: 17 | counts[word] = counts.get(word, 0) + 1 18 | print(counts) -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w4_web_scraping/BeautifulSoup.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-3-python_access_web_data/w4_web_scraping/BeautifulSoup.pyc -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w4_web_scraping/ScrappingHTMLDataWithBeautifull.py: -------------------------------------------------------------------------------- 1 | import urllib.request, urllib.parse, urllib.error 2 | from bs4 import BeautifulSoup 3 | import ssl 4 | 5 | #Get SSL Certification Error handeling 6 | ctx = ssl.create_default_context() 7 | ctx.check_hostname = False 8 | ctx.verify_mode = ssl.CERT_NONE 9 | 10 | #Data Collect From the Website 11 | link = input('Enter - ') 12 | html = urllib.request.urlopen(link, context = ctx).read() 13 | soup = BeautifulSoup(html, 'html.parser') 14 | 15 | 16 | #Data Scraping 17 | tags = soup('span') 18 | sm = 0 19 | cn = 0 20 | for tag in tags: 21 | cn += 1 22 | sm += int(tag.contents[0]) 23 | 24 | print('Count %d' % cn) 25 | print('Sum %d' % sm) -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w4_web_scraping/assignment_scrap_href_url.py: -------------------------------------------------------------------------------- 1 | import urllib.request, urllib.parse, urllib.error 2 | from bs4 import BeautifulSoup 3 | import ssl 4 | 5 | #SSL Certification Error Handle 6 | ctx = ssl.create_default_context() 7 | ctx.check_hostname = False 8 | ctx.verify_mode = ssl.CERT_NONE 9 | 10 | #Data Collection 11 | link = input('Enter URL: ') 12 | cont = int(input('Enter count: ')) 13 | line = int(input('Enter position: ')) 14 | 15 | 16 | 17 | print('Retrieving: %s' % link) 18 | for i in range(0, cont): 19 | html = urllib.request.urlopen(link, context=ctx).read() 20 | soup = BeautifulSoup(html, 'html.parser') 21 | 22 | tags = soup('a') 23 | cn = 0 24 | ps = 0 25 | for tag in tags: 26 | ps += 1 27 | if ps == line: 28 | print('Retrieving: %s' % str(tag.get('href', None))) 29 | link = str(tag.get('href', None)) 30 | ps = 0 31 | break -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w4_web_scraping/assignment_scrap_span_tags.py: -------------------------------------------------------------------------------- 1 | # 1. Import urllib and Beautiful Soup 2 | # 2. Request raw URL input 3 | # 3. Read HTML with urllib's urlopen() method 4 | # 4. Parse HTML with BeautifulSoup's BeautifulSoup() method 5 | # 5. Retrieve list of span tags 6 | # 6. Loop through to get numbers 7 | 8 | # 1. Import urllib and Beautiful Soup 9 | import urllib 10 | from BeautifulSoup import * 11 | 12 | # 2. Request raw URL input 13 | url = raw_input('URL to scrap: ') 14 | 15 | # 3. Read HTML with urllib's urlopen() method 16 | html = urllib.urlopen(url).read() 17 | 18 | # 4. Parse HTML with BeautifulSoup's BeautifulSoup() method 19 | soup = BeautifulSoup(html) 20 | 21 | # 5. Retrieve list of span tags 22 | tags = soup('span') 23 | 24 | total = 0 25 | # 6. Loop through to get numbers 26 | for tag in tags: 27 | # print 'TAG:', tag 28 | # print 'URL:', tag.get('href', None) 29 | # print 'Contents:', tag.contents[0] 30 | # print 'Attrs:', tag.attrs 31 | num = tag.contents[0] 32 | # Retrieve number with positions 0 onwards 33 | num_int = int(num[0:]) 34 | total += num_int 35 | 36 | print(total) 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w4_web_scraping/for_i_in_range.py: -------------------------------------------------------------------------------- 1 | l = 5 2 | 3 | for i in range(l): 4 | print "test" -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w4_web_scraping/scrappin_a_tags.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | from BeautifulSoup import * 3 | 4 | url =raw_input('Enter - ') 5 | 6 | html = urllib.urlopen(url).read() 7 | soup = BeautifulSoup(html) 8 | 9 | # Retrieve a list of anchor tags 10 | # Each tag is like a dictionary of HTML attributes 11 | 12 | # Just want 13 | tags = soup('a') 14 | 15 | # Look at all the 'a' anchor tags 16 | # Give values of href 17 | for tag in tags: 18 | print(tag.get('href', None)) 19 | -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w4_web_scraping/webscraping.txt: -------------------------------------------------------------------------------- 1 | Web Scraping 2 | Program/script that pretends to be a browser 3 | retrieves web pages 4 | looks 5 | extracts 6 | looks at more 7 | Search Engines do this 8 | Web Crawling 9 | 10 | 11 | Browser sends GET 12 | Server responds with HTML 13 | Request Response Cycle 14 | Click --> event in browser 15 | Browser makes connection to server 16 | Makes application GET request 17 | Server respond 18 | 19 | -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w5_retrieve_parse_xml/assignment_parse_retrieve_count_xml.py: -------------------------------------------------------------------------------- 1 | import urllib.request, urllib.parse, urllib.error 2 | import xml.etree.ElementTree as ET 3 | 4 | link = input('Enter location: ') 5 | html = urllib.request.urlopen(link).read().decode() 6 | print('Retrieving', link) 7 | print('Retrieved', len(html), 'characters') 8 | 9 | 10 | #data calculation 11 | cn = 0 12 | sm = 0 13 | data = ET.fromstring(html) 14 | tags = data.findall('comments/comment') 15 | 16 | for tag in tags: 17 | cn += 1 18 | sm += int(tag.find('count').text) 19 | 20 | print('Count:', cn) 21 | print('Sum:', sm) -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w5_retrieve_parse_xml/geoxml.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | import xml.etree.ElementTree as ET 3 | 4 | serviceurl = 'http://maps.googleapis.com/maps/api/geocode/xml?' 5 | 6 | while True: 7 | address = raw_input('Enter location: ') 8 | if len(address) < 1 : break 9 | 10 | url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address}) 11 | print 'Retrieving', url 12 | uh = urllib.urlopen(url) 13 | data = uh.read() 14 | print 'Retrieved',len(data),'characters' 15 | print data 16 | tree = ET.fromstring(data) 17 | 18 | 19 | results = tree.findall('result') 20 | lat = results[0].find('geometry').find('location').find('lat').text 21 | lng = results[0].find('geometry').find('location').find('lng').text 22 | location = results[0].find('formatted_address').text 23 | 24 | print 'lat',lat,'lng',lng 25 | print location 26 | -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w5_retrieve_parse_xml/web_services.txt: -------------------------------------------------------------------------------- 1 | Web Services 2 | 3 | Sending data across the web 4 | Dictionary -->(serialize) Wire Protocol (XML and JSON) -->(de-serialize) Java Hashmap 5 | Serialize: 6 | taking internal structure and creating wire format (XML/JSON) - 7 | convert to common format 8 | De-serialize: 9 | taking wire format & creating internal structures in diff languages 10 | allows creating different applications that work in different languages 11 | convert from common format 12 | 13 | JSON: Javascript Object Notation 14 | XML: eXtensible Markup Language 15 | 16 | -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w5_retrieve_parse_xml/xml.txt: -------------------------------------------------------------------------------- 1 | XML (eXtensible Markup Language) 2 | View as a tree of nodes (elements and nodes) 3 | 4 | Elements 5 | Simple element: just tags including text 6 | Complex element: include other tags 7 | 8 | Tags 9 | Start/end tag 10 | Attributes 11 | Text Content 12 | Self closing tag 13 | Your tags are customised based on what is most useful 14 | 15 | 16 | White space do not matter 17 | 18 | Tree of nodes 19 | 20 | 21 | text 22 | 23 | /a/b/c 24 | /a/d/ 25 | 26 | 27 | VALIDATION 28 | XML Document & XML Schema Contract --> valid/invalid 29 | 30 | XML Schema Contract 31 | 32 | 33 | 44 | 45 | Types 46 | xs:"string" 47 | xs:"date" 48 | xs:"dateTime" 49 | xs:"decimal" 50 | xs:"integer" 51 | 52 | Time representation ISO 8601 Date/time format 53 | YYYY-MM-DDTHH:MM:SSZ 54 | T 55 | Z: time zone 56 | 57 | minOccurs = 0 (optional) 58 | minOccurs = 1 (compulsory) 59 | maxOccurs="unbounded" (as many as possible) 60 | base="xs:string" --> x number of options -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w5_retrieve_parse_xml/xml1.py: -------------------------------------------------------------------------------- 1 | # Naming code 2 | import xml.etree.ElementTree as ET 3 | 4 | # ''' represents whole string 5 | data = ''' 6 | 7 | Chuck 8 | 9 | +1 734 303 4456 10 | 11 | 12 | ''' 13 | 14 | # Parse string: de-serialization 15 | tree = ET.fromstring(data) 16 | 17 | # Find tag 'name' --> text 18 | print 'Name:',tree.find('name').text 19 | 20 | # Find tag 'email' --> get 'hide' attribute 21 | print 'Attr:',tree.find('email').get('hide') 22 | 23 | print 'Phone:', tree.find('phone').text 24 | print 'Phone type:', tree.find('phone').get('type') -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w5_retrieve_parse_xml/xml2.py: -------------------------------------------------------------------------------- 1 | import xml.etree.ElementTree as ET 2 | 3 | input = ''' 4 | 5 | 6 | 7 | 001 8 | Chuck 9 | 10 | 11 | 009 12 | Brent 13 | 14 | 15 | ''' 16 | 17 | # Producing structure of grabbing the tree of nodes 18 | stuff = ET.fromstring(input) 19 | 20 | # Grab all the users/user nodes 21 | lst = stuff.findall('users/user') 22 | print 'User count:', len(lst) 23 | 24 | # Loop 25 | for item in lst: 26 | print 'Name', item.find('name').text 27 | print 'Id', item.find('id').text 28 | print 'Attribute', item.get("x") 29 | 30 | -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w6_json/access_api.txt: -------------------------------------------------------------------------------- 1 | SOAP (Simple Object Access Protocol) 2 | - Remote programs/code which we use over the network 3 | 4 | REST (Representational State Transfer) 5 | Remote resources we create, read, update and delete remotely 6 | -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w6_json/api.txt: -------------------------------------------------------------------------------- 1 | Rate limiting 2 | You can make a certain number of calls per day 3 | 4 | If you want to exceed rate 5 | Start/stop for n-days to cover everything 6 | 7 | Twitter 8 | Create app 9 | Get hidden.py details from the app page 10 | You access with signature (OAuth) 11 | 12 | Service Oriented Architecture 13 | Allows an application to be broken into parts and distributed across 14 | networks 15 | 16 | Application Programming Interface (API) 17 | Contract for interaction 18 | 19 | Web Services 20 | Provide infrastructure for applications cooperating (an API) 21 | over a network 22 | Two styles of web services 23 | SOAP (Simple Object Access Protocol) - Rarely used 24 | REST (Representational State Transfer) 25 | 26 | Serialization Formats 27 | XML and JSON -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w6_json/assignment_extract_data_json.py: -------------------------------------------------------------------------------- 1 | import urllib.request, urllib.parse, urllib.error 2 | import json 3 | 4 | #Data collection 5 | link = input('Enter location: ') 6 | print('Retrieving', link) 7 | 8 | html = urllib.request.urlopen(link).read().decode() 9 | print('Retrieved', len(html), 'characters') 10 | 11 | try: 12 | js = json.loads(html) 13 | except: 14 | js = None 15 | 16 | cn = 0 17 | sm = 0 18 | for item in js['comments']: 19 | cn += 1 20 | sm += int(item['count']) 21 | 22 | print('Count:', cn) 23 | print('Sum:', sm) -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w6_json/assignment_google_geojson.py: -------------------------------------------------------------------------------- 1 | import urllib.request, urllib.parse, urllib.error 2 | import json 3 | 4 | #Api 5 | api = 'http://py4e-data.dr-chuck.net/json?' 6 | 7 | #Input data 8 | link = input('Enter location: ') 9 | link = api + urllib.parse.urlencode({'address':link}) 10 | print('Retrieving', link) 11 | 12 | html = urllib.request.urlopen(link).read().decode() 13 | print('Retrieved', len(html), 'characters') 14 | 15 | try: 16 | js = json.loads(html) 17 | except: 18 | js = None 19 | 20 | placeId = js['results'][0]['place_id'] 21 | print('Place id', placeId) -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w6_json/geojson.py: -------------------------------------------------------------------------------- 1 | # Imports 2 | import urllib 3 | import json 4 | 5 | # Access Service API 6 | serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?' 7 | 8 | # Loop 9 | while True: 10 | # Prompt location 11 | address = raw_input('Enter location: ') 12 | # If input = 0, end loop 13 | if len(address) < 1 : break 14 | 15 | # Encodes using urlencode using a dictionary 16 | url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address}) 17 | 18 | # Print URL 19 | print 'Retrieving', url 20 | 21 | # Open and read URL 22 | uh = urllib.urlopen(url) 23 | data = uh.read() 24 | 25 | # Print URL characters 26 | print 'Retrieved',len(data),'characters' 27 | 28 | # Try and except for bad data handling 29 | # json.loads loads data 30 | try: js = json.loads(str(data)) 31 | except: js = None 32 | if 'status' not in js or js['status'] != 'OK': 33 | print '==== Failure To Retrieve ====' 34 | print data 35 | continue 36 | 37 | # Using json library, dump string using .dumps 38 | # Parse dictionary js 39 | # indent = 4: print it out nicely with indent of 4 40 | print json.dumps(js, indent=4) 41 | 42 | # js is a dictionary 43 | # ["results"] is a list of dictionaries 44 | # ["results"][0] accesses first object 45 | # Access geometry dictionary 46 | # Access location dictionary 47 | # Pull out lat 48 | lat = js["results"][0]["place_id"]["location"]["lat"] 49 | lng = js["results"][0]["geometry"]["location"]["lng"] 50 | print 'lat',lat,'lng',lng 51 | 52 | # Access formatted_address 53 | # Tip, slowly do print [] until you reach where you want 54 | location = js['results'][0]['formatted_address'] 55 | print location 56 | -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w6_json/json.txt: -------------------------------------------------------------------------------- 1 | JSON 2 | Doesn't do well with infinite nested nodes 3 | Key-value pairs separated by : 4 | 5 | List: array 6 | Dictionary: object -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w6_json/json_notes.py: -------------------------------------------------------------------------------- 1 | # Import json 2 | import json 3 | 4 | # Create Data: Dictionary 5 | # If dictionary, data parsed will be dictionary 6 | data = '''{ 7 | "name" : "John", 8 | "phone" : { 9 | "type" : "international", 10 | "mobile" : "999" 11 | }, 12 | "email" : { 13 | "hide" : "yes" 14 | } 15 | }''' 16 | 17 | # Deserialization from string to internal structure (where you get back a dictionary) 18 | info = json.loads(data) 19 | 20 | # Access native dictionary 21 | print 'Name:', info["name"] 22 | print 'Hide:', info["email"]["hide"] 23 | 24 | 25 | # Create data list 26 | # If list, data parsed will be list 27 | input2 = '''[ 28 | { 29 | "id" : "001", 30 | "x" : "2", 31 | "name" : "Chuck" 32 | }, 33 | { 34 | "id" : "009", 35 | "x" : "7", 36 | "name" : "Chuck" 37 | } 38 | ]''' 39 | 40 | info2 = json.loads(input2) 41 | print 'User count:', len(info2) 42 | for item in info2: 43 | print 'Name', item['name'] 44 | print 'Id', item['id'] 45 | print 'Attribute', item['x'] -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w6_json/twitter_api/get_profile_post.py: -------------------------------------------------------------------------------- 1 | # ACCESS POST DATA 2 | # RUN THIS 3 | import urllib 4 | from twurl import augment 5 | 6 | print '* Calling Twitter...' 7 | url = augment('https://api.twitter.com/1.1/statuses/user_timeline.json', 8 | {'screen_name': 'drchuck', 'count': '2'} ) 9 | print url 10 | connection = urllib.urlopen(url) 11 | data = connection.read() 12 | print data 13 | headers = connection.info().dict 14 | print headers 15 | -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w6_json/twitter_api/get_twitter_friends.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | import twurl 3 | import json 4 | 5 | # Base URL: get from API Documentation 6 | TWITTER_URL = 'https://api.twitter.com/1.1/friends/list.json' 7 | 8 | while True: 9 | print '' 10 | acct = raw_input('Enter Twitter Account:') 11 | if ( len(acct) < 1 ) : break 12 | # Count: # of friends 13 | url = twurl.augment(TWITTER_URL, 14 | {'screen_name': acct, 'count': '5'} ) 15 | print 'Retrieving', url 16 | # Open URL 17 | connection = urllib.urlopen(url) 18 | # Open body, JSON 19 | data = connection.read() 20 | # Open headers, dictionary 21 | headers = connection.info().dict 22 | # Remaining limit for the day by accessing the dictionary 23 | print 'Remaining', headers['x-rate-limit-remaining'] 24 | # Deserialize the json --> turn to list (native Python) 25 | js = json.loads(data) 26 | # json.dumps for pretty printing of 'js' using 'indent=4' 27 | # Easier readability of JSON data 28 | print json.dumps(js, indent=4) 29 | 30 | # Loop through list of users 31 | for u in js['users']: 32 | print u['screen_name'] 33 | s = u['status']['text'] 34 | print ' ',s[:50] 35 | -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w6_json/twitter_api/hidden.py: -------------------------------------------------------------------------------- 1 | # Keep this file separate 2 | # https://apps.twitter.com/ 3 | 4 | def oauth() : 5 | return { "consumer_key" : "h7Lu...Ng", 6 | "consumer_secret" : "dNKenAC3New...mmn7Q", 7 | "token_key" : "10185562-eibxCp9n2...P4GEQQOSGI", 8 | "token_secret" : "H0ycCFemmC4wyf1...qoIpBo" } 9 | -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w6_json/twitter_api/hidden.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-3-python_access_web_data/w6_json/twitter_api/hidden.pyc -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w6_json/twitter_api/oauth.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-3-python_access_web_data/w6_json/twitter_api/oauth.pyc -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w6_json/twitter_api/twurl.py: -------------------------------------------------------------------------------- 1 | # File for twtest.py to import to get post data 2 | 3 | import urllib 4 | import oauth 5 | # Fill in the application's information in hidden 6 | import hidden 7 | 8 | # How to do OAuth signing 9 | def augment(url, parameters): 10 | secrets = hidden.oauth() 11 | consumer = oauth.OAuthConsumer(secrets['consumer_key'], secrets['consumer_secret']) 12 | token = oauth.OAuthToken(secrets['token_key'],secrets['token_secret']) 13 | 14 | oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, 15 | token=token, http_method='GET', http_url=url, parameters=parameters) 16 | oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), consumer, token) 17 | return oauth_request.to_url() 18 | 19 | 20 | def test_me(): 21 | print '* Calling Twitter...' 22 | # Count: # of post to load 23 | url = augment('https://api.twitter.com/1.1/statuses/user_timeline.json', 24 | {'screen_name': 'drchuck', 'count': '2'} ) 25 | print url 26 | connection = urllib.urlopen(url) 27 | 28 | # Gets the body 29 | # JSON 30 | data = connection.read() 31 | print data 32 | 33 | # Gets dictionary of headers 34 | # JSON 35 | headers = connection.info().dict 36 | print headers 37 | -------------------------------------------------------------------------------- /Course-3-python_access_web_data/w6_json/twitter_api/twurl.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-3-python_access_web_data/w6_json/twitter_api/twurl.pyc -------------------------------------------------------------------------------- /Course-4 Using Databases with Python.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4 Using Databases with Python.pdf -------------------------------------------------------------------------------- /Course-4-python_databases/README.md: -------------------------------------------------------------------------------- 1 | # Python For Everybody-Coursera 2 | This course is part of the Python for Everybody Specialization 3 | 4 | ## Using Databases with Python 5 | 6 | 7 | ### About this Course 8 | This course will introduce students to the basics of the Structured Query Language (SQL) as well as basic database design for storing data as part of a multi-step data gathering, analysis, and processing effort. The course will use SQLite3 as its database. We will also build web crawlers and multi-step data gathering and visualization processes. We will use the D3.js library to do basic data visualization. This course will cover Chapters 14-15 of the book “Python for Everybody”. To succeed in this course, you should be familiar with the material covered in Chapters 1-13 of the textbook and the first three courses in this specialization. This course covers Python 3. 9 | 10 | **WHAT YOU WILL LEARN** 11 | * Use the Create, Read, Update, and Delete operations to manage databases 12 | * Explain the basics of Object Oriented Python 13 | * Understand how data is stored across multiple tables in a database 14 | * Utilize the Google Maps API to visualize data 15 | 16 | ## Course Syllabus 17 | **WEEK-1 : Object Oriented Python** 18 | > To start this class out we cover the basics of Object Oriented Python. We won't be writing our own objects, but since many of the things we use like BeautifulSoup, strings, dictionaries, database connections all use Object Oriented (OO) patterns we should at least understand some of its patterns and terminology. 19 | 20 | **WEEK-2 : Basic Structured Query Language** 21 | > We learn the four core CRUD operations (Create, Read, Update, and Delete) to manage data stored in a database. 22 | 23 | **WEEK-3 : Data Models and Relational SQL** 24 | > In this section we learn about how data is stored across multiple tables in a database and how rows are linked (i.e., we establish relationships) in the database. 25 | 26 | **WEEK-4 : Many-to-Many Relationships in SQL** 27 | > In this section we explore how to model situations like students enrolling in courses where each course has many students and each student is enrolled in many courses. 28 | 29 | **WEEK-5 : Databases and Visualization** 30 | > In this section, we put it all together, retrieve and process some data and then use the Google Maps API to visualize our data. 31 | 32 | ## Reference 33 | > [Coursera](https://www.coursera.org/learn/python-databases?specialization=python) 34 | 35 | > [Achievement Certificate](https://github.com/Ashleshk/Python-For-Everybody-Coursera/blob/master/Course-4%20Using%20Databases%20with%20Python.pdf) 36 | -------------------------------------------------------------------------------- /Course-4-python_databases/w1_oop/inheritance.py: -------------------------------------------------------------------------------- 1 | class PartyAnimal: 2 | # Two INSTANCES/VARIABLES here 3 | x = 0 4 | name = '' 5 | 6 | # CONSTRUCTOR 7 | # Double underscores, methods when something happens 8 | # When constructor runs, this happens 9 | # constructor 2 params 10 | # self: alias of instance we're in 11 | # nam: name param 12 | def __init__(self, nam): 13 | self.name = nam 14 | print self.name, 'constructed' 15 | 16 | # Self becomes an alias for s or j 17 | def party(self): 18 | self.x += 1 19 | print self.name, 'party count', self.x 20 | 21 | # FootballFan is a class that extends PartyAnimal 22 | # All the capabilities of PartyAnimal and more 23 | class FootballFan(PartyAnimal): 24 | # Add new variable/ 25 | points = 0 26 | 27 | # Add one new method 28 | def touchdown(self): 29 | self.points += 7 30 | self.party() 31 | print self.name, 'points', self.points 32 | 33 | # Create PartyAnimal object/instance 34 | s = PartyAnimal('Sally') 35 | # short-form is s.party() 36 | PartyAnimal.party(s) 37 | 38 | # Create FootballFan object/instance 39 | j = FootballFan('Jim') 40 | # short-form is j.party() 41 | FootballFan.party(j) 42 | 43 | # short-form is j.touchdown() 44 | FootballFan.touchdown(j) 45 | 46 | # 3 OBJECTS/INSTANCES in total 47 | # x, name and points 48 | -------------------------------------------------------------------------------- /Course-4-python_databases/w1_oop/inheritance.txt: -------------------------------------------------------------------------------- 1 | OOP: INHERITANCE 2 | Reuse existing class and inherit (borrow) all capabilities 3 | of an existing class & add more to make a new class 4 | 5 | "Extending a class" 6 | 7 | Write once and use many times 8 | 9 | The new class (child) has the capabilities + more of the old 10 | class (parent) 11 | 12 | -------------------------------------------------------------------------------- /Course-4-python_databases/w1_oop/object_lifecycle.py: -------------------------------------------------------------------------------- 1 | # ONE INSTANCE EXAMPLE ALONG WITH CONSTRUCTOR 2 | class PartyAnimal: 3 | # One instance here 4 | x = 0 5 | 6 | # Double underscores, methods when something happens 7 | # When constructor runs, this happens 8 | def __init__(self): 9 | print "I am constructed" 10 | 11 | def party(self): 12 | self.x = self.x + 1 13 | print 'So far', self.x 14 | 15 | def __del__(self): 16 | print 'I am destructed', self.x 17 | 18 | an = PartyAnimal() 19 | 20 | an.party() 21 | an.party() 22 | an.party() 23 | 24 | # TWO INSTANCE VARIABLE 25 | class PartyAnimal2: 26 | # 1st instance variable 27 | x = 0 28 | # 2nd instance variable 29 | name = '' 30 | 31 | # constructor 2 params 32 | # self: alias of instance we're in 33 | # nam: name param 34 | def __init__(self, nam): 35 | self.name = nam 36 | print self.name, 'constructed' 37 | 38 | # Self becomes an alias for s or j 39 | def party(self): 40 | self.x = self.x + 1 41 | print self.name, 'party count', self.x 42 | 43 | s = PartyAnimal2('Sally') 44 | # Short-form of PartyAnimal2.party(s) 45 | s.party() 46 | 47 | j = PartyAnimal2('Jim') 48 | # Short-form of PartyAnimal2.party(j) 49 | j.party() 50 | s.party() -------------------------------------------------------------------------------- /Course-4-python_databases/w1_oop/object_lifecycle.txt: -------------------------------------------------------------------------------- 1 | Object Life Cycle 2 | Objects are created, used and discarded 3 | Special blocks of code (methods) that get called 4 | During creation (constructor) - common 5 | During destruction (destructor) - rare 6 | 7 | Constructor 8 | Set up some instance variables to have proper 9 | initial values when the object is created 10 | 11 | Constructor in a class is a special block of statements 12 | called when an object is created 13 | 14 | Many Instances 15 | We can create many objects (using class as template for object) 16 | 17 | We can store each distinct object in its own variable 18 | 19 | Each instance have its own copy of the instance variables -------------------------------------------------------------------------------- /Course-4-python_databases/w1_oop/oop.py: -------------------------------------------------------------------------------- 1 | # PartyAnimal: class for making PartyAnimal objects 2 | class PartyAnimal: 3 | # x = 0: each PartyAnimal object has data 4 | x = 0 5 | 6 | # def: each PartyAnimal object has code 7 | # every method must have >= 1 parameters 8 | def party(self): 9 | self.x = self.x + 1 10 | print 'So far', self.x 11 | 12 | # Create PartyAnimal object/instance 13 | an = PartyAnimal() 14 | # an would have 15 | # data: x = 0 16 | # code: party() 17 | print type(an) 18 | print dir(an) 19 | 20 | # Call party() method 21 | # Short-form of PartyAnimal.party(an) 22 | an.party() 23 | # self is an alias of an 24 | # data: x = 0 + 1 = 1 25 | # code: party() 26 | an.party() 27 | # data: x = 1 + 1 = 2 28 | # code: party() 29 | an.party() 30 | # data: x = 2 + 1 = 3 31 | # code: party() 32 | 33 | a = list() 34 | # list() is built-in class 35 | 36 | type(a) 37 | 38 | print dir(a) 39 | # methods: operations that the object can perform 40 | # append() 41 | # pop() -------------------------------------------------------------------------------- /Course-4-python_databases/w1_oop/oop.txt: -------------------------------------------------------------------------------- 1 | Program is made up of many coordinating objects 2 | Each object is a "little island" 3 | Cooperatively working with one another 4 | Objects make use of one another's capabilities 5 | 6 | Object 7 | self-contained code and data 8 | String objects 9 | Integer objects 10 | Dictionary objects 11 | List objects 12 | 13 | Object-approach 14 | Break problem into smaller parts 15 | Hide complex parts 16 | Represent easier framework 17 | You care about the interactions 18 | 19 | INPUT --> (COORDINATING OBJECTS) --> OUTPUT 20 | bits of code/data 21 | 22 | OOP 23 | Structured approach to code reuse 24 | 25 | Can group data and functionality together 26 | and create many independent instances 27 | of a class 28 | 29 | Fields = Attributes = Class Variables -------------------------------------------------------------------------------- /Course-4-python_databases/w1_oop/terminology.txt: -------------------------------------------------------------------------------- 1 | Class: template 2 | Dog 3 | Method/message: defined capability of a class 4 | Call a method to the class 5 | Activating the code 6 | Bark() 7 | Field/attribute: bit of data in class 8 | Variable 9 | Object/instance: particular instance of a class 10 | 11 | -------------------------------------------------------------------------------- /Course-4-python_databases/w2_sql/assignment_count_email.py: -------------------------------------------------------------------------------- 1 | # 1. Import sqlite3 2 | # 2. Call .connect() method to create connection object 3 | # 3. Create cursor object 4 | # 4. Delete table if it exists 5 | # 5. Create table with domains and counts as attributes 6 | # 6. Request file name 7 | # 7. Create file handle 8 | # 8. Loop through file to retrieve domain of emails 9 | # 9a. Retrieve data 10 | # 9b. Call fetchone() method to query db 11 | # 10. if/else statement similar to get() 12 | # 11. Commit changes with commit() 13 | # 12. Print counts 14 | 15 | # 1. Import sqlite3 16 | import sqlite3 17 | 18 | # 2. Call .connect() method to create connection object 19 | connect_db = sqlite3.connect('domain_db.sqlite') 20 | 21 | # 3. Create cursor object 22 | cursor_db = connect_db.cursor() 23 | 24 | # 4. Delete table if it exists 25 | cursor_db.execute(''' 26 | DROP TABLE IF EXISTS Counts''') 27 | 28 | # 5. Create table with emails and counts as attributes 29 | cursor_db.execute(''' 30 | CREATE TABLE Counts( 31 | org TEXT, 32 | count INTEGER)''') 33 | 34 | # 6. Request file name 35 | fname = input('File name: ') 36 | 37 | # 7. Create file handle 38 | fhandle = open(fname) 39 | 40 | # 8. Loop through file to retrieve domain of emails 41 | for line in fhandle: 42 | if not line.startswith('From: '): 43 | continue 44 | line = line.split() 45 | email = line[1] 46 | email = email.split('@') 47 | org = email[1] 48 | 49 | # Using cursor as iterator 50 | # 9a. Retrieve data 51 | cursor_db.execute('SELECT count FROM Counts WHERE org = ? ', (org,)) 52 | 53 | # 9b. Call fetchone() method to query db 54 | row = cursor_db.fetchone() 55 | 56 | # 10. if/else statement similar to get() 57 | if row is None: 58 | cursor_db.execute('''INSERT INTO Counts (org, count) 59 | VALUES (?, 1)''', (org, )) 60 | else: 61 | cursor_db.execute('''UPDATE Counts 62 | SET count = count + 1 63 | WHERE org = ?''', (org, )) 64 | 65 | # 11. Commit changes with commit() 66 | connect_db.commit() 67 | 68 | # 12. Print counts 69 | sqlstr = '''SELECT org, count FROM Counts ORDER BY count DESC''' 70 | 71 | for row in cursor_db.execute(sqlstr): 72 | print (row[0], row[1]) 73 | 74 | # 13. Close cursor (communication) 75 | cursor_db.close() -------------------------------------------------------------------------------- /Course-4-python_databases/w2_sql/assignment_intro.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w2_sql/assignment_intro.db -------------------------------------------------------------------------------- /Course-4-python_databases/w2_sql/crud.txt: -------------------------------------------------------------------------------- 1 | # CREATE TABLE 2 | 3 | CREATE TABLE Users( 4 | name VARCHAR(128), 5 | email VARCHAR(128) 6 | ) 7 | 8 | # DELETE TABLE 9 | DROP TABLE IF EXISTS Users 10 | 11 | # INSERT 12 | INSERT INTO Users (name, email) VALUES ('Kristin', 'kf@me.com') 13 | 14 | # UPDATE 15 | UPDATE Users SET name='Charles' WHERE email='fred@live.com' 16 | 17 | # Retrieve 18 | # * all rows 19 | SELECT * FROM Users WHERE email='fred@live.com' 20 | SELECT * FROM Users ORDER BY email 21 | SELECT * FROM Users ORDER BY name 22 | 23 | # DELETE 24 | DELETE FROM Users WHERE email='sally@me.com' 25 | 26 | # Delete everything 27 | DELETE FROM Users 28 | 29 | # Count number of rows or users 30 | SELECT COUNT(*) FROM Users 31 | 32 | # 33 | SELECT hex(name || age) AS X FROM Ages ORDER BY X 34 | -------------------------------------------------------------------------------- /Course-4-python_databases/w2_sql/db_intro.txt: -------------------------------------------------------------------------------- 1 | INTRODUCTION TO DATABASES 2 | 3 | Relational Databases 4 | Storing rows and columns in tables 5 | Multiple tables 6 | Relationship between tables 7 | 8 | Database: contains many tables 9 | 10 | Relation (table): contains tuples and attributes (variables/fields) 11 | 12 | Tuple (row): a set of fields that generally represents an "object" like 13 | person or a music track 14 | 15 | Attribute (column/field): one of possibly many elements of data 16 | corresponding to the object represented in the row 17 | 18 | Structured Query Language (SQL): language to issue commands to the databases 19 | Create data 20 | Retrieve data 21 | Update data 22 | Delete data 23 | CRUD! 24 | 25 | Abstraction of complexity of SQL 26 | We communicate with SQL instead of the database directly 27 | 28 | Python cleans data 29 | SQL CRUD data 30 | -------------------------------------------------------------------------------- /Course-4-python_databases/w2_sql/domain_db.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w2_sql/domain_db.sqlite -------------------------------------------------------------------------------- /Course-4-python_databases/w2_sql/emaildb.py: -------------------------------------------------------------------------------- 1 | # Importing library to talk to SQL db 2 | import sqlite3 3 | 4 | # Use db, store db in file emaildb.sqlite 5 | # SQLite Browser to view this 6 | # Create connection object 7 | conn = sqlite3.connect('emaildb.sqlite') 8 | # .cursor() so you can communicate 9 | cur = conn.cursor() 10 | 11 | # Call execute() method 12 | # The DROP TABLE statement is used to delete a table. 13 | cur.execute(''' 14 | DROP TABLE IF EXISTS Counts''') 15 | 16 | 17 | # Call execute() methods 18 | # CREATE TABLE 19 | cur.execute(''' 20 | CREATE TABLE Counts (email TEXT, count INTEGER)''') 21 | 22 | fname = raw_input('Enter file name: ') 23 | if ( len(fname) < 1 ) : fname = 'mbox-short.txt' 24 | fh = open(fname) 25 | for line in fh: 26 | # Looping through and retrieving email 27 | if not line.startswith('From: '): 28 | continue 29 | pieces = line.split() 30 | email = pieces[1] 31 | print email 32 | 33 | 34 | # first param: ? is a placeholder to be filled in 35 | # second param: one tuple --> first thing in tuple to substitute question mark 36 | cur.execute('SELECT count FROM Counts WHERE email = ? ', (email, )) 37 | 38 | # This method retrieves the next row of a query result set and 39 | # returns a single sequence, or None if no more rows are available. 40 | row = cur.fetchone() 41 | if row is None: 42 | cur.execute('''INSERT INTO Counts (email, count) 43 | VALUES (?, 1)''', (email, )) 44 | else: 45 | cur.execute('UPDATE Counts SET count=count+1 WHERE email = ?', 46 | (email, )) 47 | 48 | # This statement commits outstanding changes to disk each 49 | # time through the loop - the program can be made faster 50 | # by moving the commit so it runs only after the loop completes 51 | conn.commit() 52 | 53 | # https://www.sqlite.org/lang_select.html 54 | # DESC so large number on the top 55 | # LIMIT 10: only give 10 out of n records --> top 10 email senders 56 | sqlstr = 'SELECT email, count FROM Counts ORDER BY count DESC LIMIT 10' 57 | 58 | print "Counts:" 59 | for row in cur.execute(sqlstr): 60 | print str(row[0]), row[1] 61 | 62 | cur.close() 63 | 64 | -------------------------------------------------------------------------------- /Course-4-python_databases/w2_sql/emaildb.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w2_sql/emaildb.sqlite -------------------------------------------------------------------------------- /Course-4-python_databases/w2_sql/sql1.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w2_sql/sql1.db -------------------------------------------------------------------------------- /Course-4-python_databases/w2_sql/using_db.txt: -------------------------------------------------------------------------------- 1 | USING DATABASES 2 | 3 | Roles in Large Projects 4 | Application developer: builds logic, looks, and monitors 5 | Database Administrator: design, implementation etc. 6 | 7 | Data Analysis Structure 8 | 1. Read & clean input data (Python) 9 | 2. CUD data (SQL) 10 | 3. Read data (SQL) 11 | 4. Output files (R/Excel/D3.js) (Python) 12 | 13 | Database Model or Database Schema 14 | Structure or format of a database 15 | Application of a data model in conjunction with a database management system 16 | 17 | Common Database Systems 18 | Oracle: large, commercial, enterprise-scale, very very tweakable 19 | MySql (open-source): simpler but fast and scalable - commercial open-source 20 | Use for online website 21 | SqlServer: from Microsoft (Access) 22 | Postgress (open-source): imitation of Oracle 23 | 24 | SQLite: Embedded database (built in Python using import) 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Course-4-python_databases/w3_data_models/data_model_table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w3_data_models/data_model_table.png -------------------------------------------------------------------------------- /Course-4-python_databases/w3_data_models/data_models.txt: -------------------------------------------------------------------------------- 1 | Schema (contract) 2 | Many tables 3 | Some columns related to other tables 4 | 5 | Building Data Model 6 | Don't put same string twice (use relationship) 7 | 8 | For each piece of info 9 | Is the column an object or attribute of another object? 10 | 11 | Once we define objects, we need to define the relationships 12 | between objects 13 | 14 | Data Modelling of Music Management 15 | Track 16 | Time 17 | Rating 18 | Count 19 | Album 20 | Artist 21 | Genre 22 | 23 | Start with most essential function to determine which 24 | tables to build 25 | Music track management --> start with Tracks 26 | 27 | Move to which are attributes 28 | Time --> part of Tracks 29 | Rating --> part of Tracks 30 | Count --> part of Tracks 31 | 32 | 33 | Move to which are attributes of objects 34 | Album TABLE --> Tracks belong to Album 35 | Artist TABLE --> Album belong to Artist 36 | Genre Table --> Tracks belong to Genre 37 | 38 | Primary key: id (one key for every row to point to other tables) 39 | Logical key: might use this in a WHERE or ORDER BY clause 40 | Foreign key 41 | 42 | WORK FROM OUTWARD IN --> LEAVES TO BRANCH 43 | 1. Artist 44 | 2. Album 45 | 3. Genre 46 | 4. Tracks 47 | -------------------------------------------------------------------------------- /Course-4-python_databases/w3_data_models/join.txt: -------------------------------------------------------------------------------- 1 | JOIN operation 2 | links across several tables as part of a select operation 3 | 4 | tell JOIN to use the keys that make the connection 5 | between tables using an ON clause 6 | 7 | # JOIN Album & Artist 8 | SELECT Album.title, Artist.name 9 | FROM Album 10 | JOIN Artist 11 | ON Album.artist_id = Artist.id 12 | # SELECT Album.title, Artist.name --> what we want to see 13 | # Artist.name FROM Album JOIN Artist --> join 2 tables (title and name columns) 14 | # ON Album.artist_id = Artist.id --> how tables are linked 15 | # starting arrow --> Album.artist_id (foreign key) 16 | # ending arrow --> Artist.id (primary key) 17 | 18 | # Show source of connection 19 | SELECT Album.title, Album.artist_id, Artist.id, Artist.name 20 | FROM Album 21 | JOIN Artist 22 | ON Album.artist_id = Artist.id 23 | 24 | # JOIN Track & Genre 25 | # Canonical syntax 26 | SELECT Track.title, Genre.name 27 | FROM Track 28 | JOIN Genre 29 | ON Track.genre_id = Genre.id 30 | 31 | # If no ON clause, there will be combinations 32 | SELECT Track.title, Track.genre_id, Genre.name, Genre.id 33 | FROM Track 34 | JOIN Genre 35 | ON Track.genre_id = Genre.id 36 | 37 | # JOIN --> all possible matches amongst keys 38 | # ON --> pick keys match (foreign and primary key) 39 | 40 | # More complex JOINs 41 | SELECT Track.title, Artist.name, Album.title, Genre.name 42 | FROM Track 43 | JOIN Artist JOIN Album JOIN Genre 44 | ON Track.genre_id = Genre.id and Track.album_id = Album.id and Album.artist_id = Artist.id -------------------------------------------------------------------------------- /Course-4-python_databases/w3_data_models/music_mgt.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w3_data_models/music_mgt.db -------------------------------------------------------------------------------- /Course-4-python_databases/w3_data_models/trackdb.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w3_data_models/trackdb.sqlite -------------------------------------------------------------------------------- /Course-4-python_databases/w3_data_models/trackdb_assign.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w3_data_models/trackdb_assign.sqlite -------------------------------------------------------------------------------- /Course-4-python_databases/w4_many_to_many/exercise_db.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w4_many_to_many/exercise_db.png -------------------------------------------------------------------------------- /Course-4-python_databases/w4_many_to_many/exercise_db.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w4_many_to_many/exercise_db.sqlite -------------------------------------------------------------------------------- /Course-4-python_databases/w4_many_to_many/exercise_many2many.py: -------------------------------------------------------------------------------- 1 | # 1. Import ElementTree and SQLite libraries 2 | import xml.etree.ElementTree as ET 3 | import sqlite3 4 | 5 | # 2. Call .connect() method to create connection object 6 | connect_db = sqlite3.connect('exercise_db.sqlite') 7 | 8 | # 3. Create cursor object 9 | cursor_db = connect_db.cursor() 10 | 11 | # 4. Create Tables 12 | # User 13 | # Course 14 | # Member 15 | 16 | # Delete table if it exists 17 | cursor_db.execute('DROP TABLE IF EXISTS User') 18 | 19 | # Create table 20 | cursor_db.execute(''' 21 | CREATE TABLE IF NOT EXISTS User ( 22 | id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, 23 | name TEXT, 24 | email TEXT 25 | ) 26 | ''') 27 | 28 | # Delete table if it exists 29 | cursor_db.execute('DROP TABLE IF EXISTS Course') 30 | 31 | # Create table 32 | cursor_db.execute(''' 33 | CREATE TABLE IF NOT EXISTS Course ( 34 | id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, 35 | title TEXT 36 | ) 37 | ''') 38 | 39 | # Delete table if it exists 40 | cursor_db.execute('DROP TABLE IF EXISTS Member') 41 | 42 | # Create CONNECTOR (Junction) table 43 | cursor_db.execute(''' 44 | CREATE TABLE IF NOT EXISTS Member ( 45 | user_id INTEGER, 46 | course_id INTEGER, 47 | role INTEGER, 48 | PRIMARY KEY(user_id, course_id) 49 | ) 50 | ''') 51 | 52 | # Insert User and Course Data 53 | # INSERT INTO User (name, email) VALUES ('Jane', 'jane@me.com'); 54 | # INSERT INTO User (name, email) VALUES ('Ed', 'ed@me.com'); 55 | # INSERT INTO User (name, email) VALUES ('Sue', 'sue@me.com'); 56 | # INSERT INTO Course (title) VALUES ('Python'); 57 | # INSERT INTO Course (title) VALUES ('SQL'); 58 | # INSERT INTO Course (title) VALUES ('PHP'); 59 | 60 | # Insert Member Data 61 | # INSERT INTO Member (user_id, course_id, role) VALUES (1, 1, 1); 62 | # INSERT INTO Member (user_id, course_id, role) VALUES (2, 1, 0); 63 | # INSERT INTO Member (user_id, course_id, role) VALUES (3, 1, 0); 64 | # 65 | # INSERT INTO Member (user_id, course_id, role) VALUES (1, 2, 0); 66 | # INSERT INTO Member (user_id, course_id, role) VALUES (2, 2, 1); 67 | # 68 | # INSERT INTO Member (user_id, course_id, role) VALUES (2, 3, 1); 69 | # INSERT INTO Member (user_id, course_id, role) VALUES (3, 3, 0); 70 | 71 | 72 | # Select, Join, On and Order By 73 | # SELECT User.name, Member.role, Course.title 74 | # FROM User 75 | # JOIN Member JOIN Course 76 | # ON Member.user_id = User.id AND Member.course_id = Course.id 77 | # ORDER BY Course.title, Member.role DESC, User.name 78 | 79 | # Course.title first --> Member.role first --> User.name last -------------------------------------------------------------------------------- /Course-4-python_databases/w4_many_to_many/exercise_roster_m2m.py: -------------------------------------------------------------------------------- 1 | # 1. Import JSON and SQLite libraries 2 | import json 3 | import sqlite3 4 | 5 | # 2. Call .connect() method to create connection object 6 | connect_db = sqlite3.connect('rosterdb.sqlite') 7 | 8 | # 3. Create cursor object to send commands 9 | cursor_db = connect_db.cursor() 10 | 11 | # 4. Create tables 12 | # Using multiple SQL commands using .executescript() 13 | # Connector table: Member 14 | # Member: 2 foreign keys, 1 composite primary key (concatenated) 15 | 16 | cursor_db.executescript(''' 17 | DROP TABLE IF EXISTS User; 18 | DROP TABLE IF EXISTS Member; 19 | DROP TABLE IF EXISTS Course; 20 | 21 | CREATE TABLE User ( 22 | id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, 23 | name TEXT UNIQUE 24 | ); 25 | 26 | CREATE TABLE Course ( 27 | id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, 28 | title TEXT UNIQUE 29 | ); 30 | 31 | CREATE TABLE Member ( 32 | user_id INTEGER, 33 | course_id INTEGER, 34 | PRIMARY KEY (user_id, course_id) 35 | ) 36 | ''') 37 | 38 | # 5 Request file name 39 | fname = raw_input('File name: ') 40 | # 5a. Error handling 41 | if len(fname) < 1: 42 | fname = 'roster_data.json' 43 | 44 | # JSON Data example 45 | # [ 46 | # [ 47 | # "Charley", 48 | # "si110", 49 | # 1 50 | # ], 51 | # [ 52 | # "Mea", 53 | # "si110", 54 | # 0 55 | # ], 56 | 57 | # 6. Open and load json 58 | str_data = open(fname).read() 59 | json_data = json.loads(str_data) 60 | 61 | for entry in json_data: 62 | name = entry[0] 63 | title = entry[1] 64 | 65 | print name, title 66 | 67 | # User: Insert, Retrieve and Query 68 | # Insert Data 69 | cursor_db.execute(''' 70 | INSERT OR IGNORE INTO User (name) 71 | VALUES (?)''', (name, )) 72 | 73 | # Retrieve Data 74 | cursor_db.execute(''' 75 | SELECT id 76 | FROM User 77 | WHERE name = ?''', (name, )) 78 | 79 | # Call fetchone() method to query db 80 | user_id = cursor_db.fetchone()[0] 81 | 82 | # Course: Insert, Retrieve and Query 83 | # Insert Data 84 | cursor_db.execute(''' 85 | INSERT OR IGNORE INTO Course (title) 86 | VALUES (?)''', (title,)) 87 | 88 | # Retrieve Data 89 | cursor_db.execute(''' 90 | SELECT id 91 | FROM Course 92 | WHERE title = ?''', (title,)) 93 | 94 | # Call fetchone() method to query db 95 | course_id = cursor_db.fetchone()[0] 96 | 97 | # Member: Insert 98 | cursor_db.execute(''' 99 | INSERT OR REPLACE INTO Member (user_id, course_id) 100 | VALUES (?, ?)''', (user_id, course_id)) 101 | 102 | # Commit changes 103 | connect_db.commit() 104 | -------------------------------------------------------------------------------- /Course-4-python_databases/w4_many_to_many/many_to_many.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w4_many_to_many/many_to_many.png -------------------------------------------------------------------------------- /Course-4-python_databases/w4_many_to_many/many_to_many.txt: -------------------------------------------------------------------------------- 1 | One to Many or Many to One 2 | 1 Artist TO MANY Tracks 3 | 1 Genre TO MANY Tracks 4 | 5 | Many to Many 6 | Courses and Users 7 | Decompose to M-O and O-M relationships 8 | 9 | -------------------------------------------------------------------------------- /Course-4-python_databases/w4_many_to_many/roster.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w4_many_to_many/roster.sqlite -------------------------------------------------------------------------------- /Course-4-python_databases/w4_many_to_many/trackdb_assign.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w4_many_to_many/trackdb_assign.sqlite -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/Capture.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w5_dbvisualisation/Capture.JPG -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/Geoload-py.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w5_dbvisualisation/Geoload-py.png -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/geocoding.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w5_dbvisualisation/geocoding.png -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/geocoding.txt: -------------------------------------------------------------------------------- 1 | Data Mining Technologies 2 | Hadoop (Apache) 3 | Spark (Apache) 4 | Redshift (AWS) 5 | Pentaho 6 | 7 | GeoData 8 | Makes a Google Map from user data 9 | 10 | 11 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/geodata/geodata.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w5_dbvisualisation/geodata/geodata.sqlite -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/geodata/geodump.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | import json 3 | import codecs 4 | 5 | conn = sqlite3.connect('geodata.sqlite') 6 | cur = conn.cursor() 7 | 8 | cur.execute('SELECT * FROM Locations') 9 | fhand = codecs.open('where.js', 'w', "utf-8") 10 | fhand.write("myData = [\n") 11 | count = 0 12 | for row in cur: 13 | # [0] address 14 | # [1] actual data 15 | data = str(row[1]) 16 | try: 17 | js = json.loads(str(data)) 18 | except: 19 | continue 20 | 21 | if not('status' in js and js['status'] == 'OK'): 22 | continue 23 | 24 | lat = js["results"][0]["geometry"]["location"]["lat"] 25 | lng = js["results"][0]["geometry"]["location"]["lng"] 26 | if lat == 0 or lng == 0: 27 | continue 28 | where = js['results'][0]['formatted_address'] 29 | where = where.replace("'", "") 30 | try: 31 | print where, lat, lng 32 | 33 | count = count + 1 34 | if count > 1 : fhand.write(",\n") 35 | output = "["+str(lat)+","+str(lng)+", '"+where+"']" 36 | fhand.write(output) 37 | except: 38 | continue 39 | 40 | fhand.write("\n];\n") 41 | cur.close() 42 | fhand.close() 43 | print count, "records written to where.js" 44 | print "Open where.html to view the data in a browser" 45 | 46 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/geodata/geoload.py: -------------------------------------------------------------------------------- 1 | # 1. Imports 2 | import urllib 3 | import sqlite3 4 | import json 5 | import time 6 | import ssl 7 | 8 | # 2. API's URL to connect 9 | serviceurl = "http://maps.googleapis.com/maps/api/geocode/json?" 10 | 11 | # Deal with SSL certificate anomalies Python > 2.7 12 | # scontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1) 13 | scontext = None 14 | 15 | # 2. Call .connect() method to create connection object 16 | conn = sqlite3.connect('geodata.sqlite') 17 | 18 | # 3. Create cursor object 19 | cur = conn.cursor() 20 | 21 | # 4. Create table 22 | cur.execute(''' 23 | CREATE TABLE IF NOT EXISTS Locations ( 24 | address TEXT, 25 | geodata TEXT 26 | ) 27 | ''') 28 | 29 | # Open input data 30 | fh = open("where.data") 31 | count = 0 32 | 33 | # Loop through data 34 | for line in fh: 35 | if count > 200: 36 | break 37 | address = line.strip() 38 | print '' 39 | # Buffer to force it to what we want 40 | # Retrieve data 41 | cur.execute(''' 42 | SELECT geodata 43 | FROM Locations 44 | WHERE address= ?''', (buffer(address), )) 45 | 46 | try: 47 | # Query data 48 | # fetchone() grabs a row 49 | # [0] grabs first column 50 | data = cur.fetchone()[0] 51 | print "Found in database ",address 52 | continue 53 | except: 54 | pass 55 | 56 | print 'Resolving', address 57 | # Create URL 58 | url = serviceurl + urllib.urlencode({"sensor":"false", "address": address}) 59 | print 'Retrieving', url 60 | 61 | # Open URL 62 | uh = urllib.urlopen(url, context=scontext) 63 | 64 | # Read URL 65 | data = uh.read() 66 | print 'Retrieved',len(data),'characters',data[:20].replace('\n',' ') 67 | count = count + 1 68 | try: 69 | js = json.loads(str(data)) 70 | # print js # We print in case unicode causes an error 71 | except: 72 | # If bad JSON 73 | continue 74 | 75 | if 'status' not in js or (js['status'] != 'OK' and js['status'] != 'ZERO_RESULTS'): 76 | print '==== Failure To Retrieve ====' 77 | print data 78 | break 79 | 80 | # SQL Command 81 | cur.execute('''INSERT INTO Locations (address, geodata) 82 | VALUES ( ?, ? )''', (buffer(address), buffer(data))) 83 | 84 | # Write to disk to ensure we've data if it blows up halfway 85 | conn.commit() 86 | time.sleep(1) 87 | 88 | print "Run geodump.py to read the data from the database so you can visualize it on a map." 89 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/geodata/where.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | A Map of Information 6 | 7 | 8 | 10 | 11 | 12 | 13 | 14 | 43 | 44 | 45 |
46 |

About this Map

47 |
51 | 52 | 53 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/gmane/gbasic.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | import time 3 | import urllib 4 | import zlib 5 | 6 | howmany = int(raw_input("How many to dump? ")) 7 | 8 | conn = sqlite3.connect('index.sqlite') 9 | conn.text_factory = str 10 | cur = conn.cursor() 11 | 12 | cur.execute('''SELECT Messages.id, sender FROM Messages 13 | JOIN Senders ON Messages.sender_id = Senders.id''') 14 | 15 | sendcounts = dict() 16 | sendorgs = dict() 17 | for message in cur : 18 | sender = message[1] 19 | sendcounts[sender] = sendcounts.get(sender,0) + 1 20 | pieces = sender.split("@") 21 | if len(pieces) != 2 : continue 22 | dns = pieces[1] 23 | sendorgs[dns] = sendorgs.get(dns,0) + 1 24 | 25 | print '' 26 | print 'Top',howmany,'Email list participants' 27 | 28 | x = sorted(sendcounts, key=sendcounts.get, reverse=True) 29 | for k in x[:howmany]: 30 | print k, sendcounts[k] 31 | if sendcounts[k] < 10 : break 32 | 33 | print '' 34 | print 'Top',howmany,'Email list organizations' 35 | 36 | x = sorted(sendorgs, key=sendorgs.get, reverse=True) 37 | for k in x[:howmany]: 38 | print k, sendorgs[k] 39 | if sendorgs[k] < 10 : break 40 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/gmane/gline.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 19 | 20 | 21 |
22 | 23 | 24 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/gmane/gline.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | import time 3 | import urllib 4 | import zlib 5 | 6 | conn = sqlite3.connect('index.sqlite') 7 | conn.text_factory = str 8 | cur = conn.cursor() 9 | 10 | # Determine the top ten organizations 11 | cur.execute('''SELECT Messages.id, sender FROM Messages 12 | JOIN Senders ON Messages.sender_id = Senders.id''') 13 | 14 | sendorgs = dict() 15 | for message_row in cur : 16 | sender = message_row[1] 17 | pieces = sender.split("@") 18 | if len(pieces) != 2 : continue 19 | dns = pieces[1] 20 | sendorgs[dns] = sendorgs.get(dns,0) + 1 21 | 22 | # pick the top schools 23 | orgs = sorted(sendorgs, key=sendorgs.get, reverse=True) 24 | orgs = orgs[:10] 25 | print "Top 10 Organizations" 26 | print orgs 27 | # orgs = ['total'] + orgs 28 | 29 | # Read through the messages 30 | counts = dict() 31 | months = list() 32 | 33 | cur.execute('''SELECT Messages.id, sender, sent_at FROM Messages 34 | JOIN Senders ON Messages.sender_id = Senders.id''') 35 | 36 | for message_row in cur : 37 | sender = message_row[1] 38 | pieces = sender.split("@") 39 | if len(pieces) != 2 : continue 40 | dns = pieces[1] 41 | if dns not in orgs : continue 42 | month = message_row[2][:7] 43 | if month not in months : months.append(month) 44 | key = (month, dns) 45 | counts[key] = counts.get(key,0) + 1 46 | tkey = (month, 'total') 47 | counts[tkey] = counts.get(tkey,0) + 1 48 | 49 | months.sort() 50 | print counts 51 | print months 52 | 53 | fhand = open('gline.js','w') 54 | fhand.write("gline = [ ['Month'") 55 | for org in orgs: 56 | fhand.write(",'"+org+"'") 57 | fhand.write("]") 58 | 59 | # for month in months[1:-1]: 60 | for month in months: 61 | fhand.write(",\n['"+month+"'") 62 | for org in orgs: 63 | key = (month, org) 64 | val = counts.get(key,0) 65 | fhand.write(","+str(val)) 66 | fhand.write("]"); 67 | 68 | fhand.write("\n];\n") 69 | 70 | print "Data written to gline.js" 71 | print "Open gline.htm in a browser to view" 72 | 73 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/gmane/gline2.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 21 | 22 | 23 |
24 | 25 | 26 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/gmane/gword.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 37 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/gmane/gword.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | import time 3 | import urllib 4 | import zlib 5 | import string 6 | 7 | conn = sqlite3.connect('index.sqlite') 8 | conn.text_factory = str 9 | cur = conn.cursor() 10 | 11 | cur.execute('''SELECT subject_id,subject FROM Messages 12 | JOIN Subjects ON Messages.subject_id = Subjects.id''') 13 | 14 | counts = dict() 15 | for message_row in cur : 16 | text = message_row[1] 17 | text = text.translate(None, string.punctuation) 18 | text = text.translate(None, '1234567890') 19 | text = text.strip() 20 | text = text.lower() 21 | words = text.split() 22 | for word in words: 23 | if len(word) < 4 : continue 24 | counts[word] = counts.get(word,0) + 1 25 | 26 | # Find the top 100 words 27 | words = sorted(counts, key=counts.get, reverse=True) 28 | highest = None 29 | lowest = None 30 | for w in words[:100]: 31 | if highest is None or highest < counts[w] : 32 | highest = counts[w] 33 | if lowest is None or lowest > counts[w] : 34 | lowest = counts[w] 35 | print 'Range of counts:',highest,lowest 36 | 37 | # Spread the font sizes across 20-100 based on the count 38 | bigsize = 80 39 | smallsize = 20 40 | 41 | fhand = open('gword.js','w') 42 | fhand.write("gword = [") 43 | first = True 44 | for k in words[:100]: 45 | if not first : fhand.write( ",\n") 46 | first = False 47 | size = counts[k] 48 | size = (size - lowest) / float(highest - lowest) 49 | size = int((size * bigsize) + smallsize) 50 | fhand.write("{text: '"+k+"', size: "+str(size)+"}") 51 | fhand.write( "\n];\n") 52 | 53 | print "Output written to gword.js" 54 | print "Open gword.htm in a browser to view" 55 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/gmane/gyear.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | import time 3 | import urllib 4 | import zlib 5 | 6 | conn = sqlite3.connect('index.sqlite') 7 | conn.text_factory = str 8 | cur = conn.cursor() 9 | 10 | # Determine the top ten organizations 11 | cur.execute('''SELECT Messages.id, sender FROM Messages 12 | JOIN Senders ON Messages.sender_id = Senders.id''') 13 | 14 | sendorgs = dict() 15 | for message_row in cur : 16 | sender = message_row[1] 17 | pieces = sender.split("@") 18 | if len(pieces) != 2 : continue 19 | dns = pieces[1] 20 | sendorgs[dns] = sendorgs.get(dns,0) + 1 21 | 22 | # pick the top schools 23 | orgs = sorted(sendorgs, key=sendorgs.get, reverse=True) 24 | orgs = orgs[:10] 25 | print "Top 10 Organizations" 26 | print orgs 27 | # orgs = ['total'] + orgs 28 | 29 | # Read through the messages 30 | counts = dict() 31 | years = list() 32 | 33 | cur.execute('''SELECT Messages.id, sender, sent_at FROM Messages 34 | JOIN Senders ON Messages.sender_id = Senders.id''') 35 | 36 | for message_row in cur : 37 | sender = message_row[1] 38 | pieces = sender.split("@") 39 | if len(pieces) != 2 : continue 40 | dns = pieces[1] 41 | if dns not in orgs : continue 42 | year = message_row[2][:4] 43 | if year not in years : years.append(year) 44 | key = (year, dns) 45 | counts[key] = counts.get(key,0) + 1 46 | tkey = (year, 'total') 47 | counts[tkey] = counts.get(tkey,0) + 1 48 | 49 | years.sort() 50 | print counts 51 | print years 52 | 53 | fhand = open('gline.js','w') 54 | fhand.write("gline = [ ['Year'") 55 | for org in orgs: 56 | fhand.write(",'"+org+"'") 57 | fhand.write("]") 58 | 59 | # for year in years[1:-1]: 60 | for year in years: 61 | fhand.write(",\n['"+year+"'") 62 | for org in orgs: 63 | key = (year, org) 64 | val = counts.get(key,0) 65 | fhand.write(","+str(val)) 66 | fhand.write("]"); 67 | 68 | fhand.write("\n];\n") 69 | 70 | print "Data written to gline.js" 71 | print "Open gline.htm in a browser to view" 72 | 73 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/gmane/mapping.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w5_dbvisualisation/gmane/mapping.sqlite -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/mailing_list.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w5_dbvisualisation/mailing_list.png -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/mailing_lists.txt: -------------------------------------------------------------------------------- 1 | Mailing Lists 2 | 1. Crawl archive of mailing list 3 | 2. Analyse and clean-up 4 | 3. Visualise data 5 | 6 | 7 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/multistep_data_analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w5_dbvisualisation/multistep_data_analysis.png -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/new-geodumpy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w5_dbvisualisation/new-geodumpy.png -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w5_dbvisualisation/output.png -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/page_rank_web_search.txt: -------------------------------------------------------------------------------- 1 | Page Rank 2 | 1. Write a simple web page crawler 3 | 2. Compute simple version of Google's Page Rank algorithm 4 | 3. Visualize resulting network 5 | 6 | Search Engine Architecture 7 | 1. Web Crawling 8 | - Browses the WWW in a methodical and automated manner 9 | - Create copy of pages to be indexed for fast searching 10 | a. Create list of websites to crawl 11 | b. Retrieve page 12 | c. Look through for links 13 | d. Add links to list 14 | e. Repeat 15 | 2. Index Building 16 | - Collects, parses and stores data 17 | - Facilitate fast & accurate data retrieval 18 | 19 | 3. Searching 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/pagerank/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012, Michael Bostock 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * The name Michael Bostock may not be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL MICHAEL BOSTOCK BE LIABLE FOR ANY DIRECT, 21 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 24 | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 25 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 26 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/pagerank/force.css: -------------------------------------------------------------------------------- 1 | circle.node { 2 | stroke: #fff; 3 | stroke-width: 1.5px; 4 | } 5 | 6 | line.link { 7 | stroke: #999; 8 | stroke-opacity: .6; 9 | } 10 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/pagerank/force.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Force-Directed Layout 5 | 6 | 7 | 8 | 9 | 10 | 13 |
14 | 15 |

If you don't see a chart above, check the JavaScript console. You may 16 | need to use a different browser.

17 | 18 | 19 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/pagerank/force.js: -------------------------------------------------------------------------------- 1 | var width = 600, 2 | height = 600; 3 | 4 | var color = d3.scale.category20(); 5 | 6 | var dist = (width + height) / 4; 7 | 8 | var force = d3.layout.force() 9 | .charge(-120) 10 | .linkDistance(dist) 11 | .size([width, height]); 12 | 13 | function getrank(rval) { 14 | return (rval/2.0) + 3; 15 | } 16 | 17 | function getcolor(rval) { 18 | return color(rval); 19 | } 20 | 21 | var svg = d3.select("#chart").append("svg") 22 | .attr("width", width) 23 | .attr("height", height); 24 | 25 | function loadData(json) { 26 | force 27 | .nodes(json.nodes) 28 | .links(json.links); 29 | 30 | var k = Math.sqrt(json.nodes.length / (width * height)); 31 | 32 | force 33 | .charge(-10 / k) 34 | .gravity(100 * k) 35 | .start(); 36 | 37 | var link = svg.selectAll("line.link") 38 | .data(json.links) 39 | .enter().append("line") 40 | .attr("class", "link") 41 | .style("stroke-width", function(d) { return Math.sqrt(d.value); }); 42 | 43 | var node = svg.selectAll("circle.node") 44 | .data(json.nodes) 45 | .enter().append("circle") 46 | .attr("class", "node") 47 | .attr("r", function(d) { return getrank(d.rank); } ) 48 | .style("fill", function(d) { return getcolor(d.rank); }) 49 | .on("dblclick",function(d) { 50 | if ( confirm('Do you want to open '+d.url) ) 51 | window.open(d.url,'_new',''); 52 | d3.event.stopPropagation(); 53 | }) 54 | .call(force.drag); 55 | 56 | node.append("title") 57 | .text(function(d) { return d.url; }); 58 | 59 | force.on("tick", function() { 60 | link.attr("x1", function(d) { return d.source.x; }) 61 | .attr("y1", function(d) { return d.source.y; }) 62 | .attr("x2", function(d) { return d.target.x; }) 63 | .attr("y2", function(d) { return d.target.y; }); 64 | 65 | node.attr("cx", function(d) { return d.x; }) 66 | .attr("cy", function(d) { return d.y; }); 67 | }); 68 | 69 | } 70 | loadData(spiderJson); 71 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/pagerank/spdump.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | conn = sqlite3.connect('spider.sqlite') 4 | cur = conn.cursor() 5 | 6 | cur.execute('''SELECT COUNT(from_id) AS inbound, old_rank, new_rank, id, url 7 | FROM Pages JOIN Links ON Pages.id = Links.to_id 8 | WHERE html IS NOT NULL 9 | GROUP BY id ORDER BY inbound DESC''') 10 | 11 | count = 0 12 | for row in cur : 13 | if count < 50 : print row 14 | count = count + 1 15 | print count, 'rows.' 16 | cur.close() 17 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/pagerank/spjson.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | conn = sqlite3.connect('spider.sqlite') 4 | cur = conn.cursor() 5 | 6 | print "Creating JSON output on spider.js..." 7 | howmany = int(raw_input("How many nodes? ")) 8 | 9 | cur.execute('''SELECT COUNT(from_id) AS inbound, old_rank, new_rank, id, url 10 | FROM Pages JOIN Links ON Pages.id = Links.to_id 11 | WHERE html IS NOT NULL AND ERROR IS NULL 12 | GROUP BY id ORDER BY id,inbound''') 13 | 14 | fhand = open('spider.js','w') 15 | nodes = list() 16 | maxrank = None 17 | minrank = None 18 | for row in cur : 19 | nodes.append(row) 20 | rank = row[2] 21 | if maxrank < rank or maxrank is None : maxrank = rank 22 | if minrank > rank or minrank is None : minrank = rank 23 | if len(nodes) > howmany : break 24 | 25 | if maxrank == minrank or maxrank is None or minrank is None: 26 | print "Error - please run sprank.py to compute page rank" 27 | quit() 28 | 29 | fhand.write('spiderJson = {"nodes":[\n') 30 | count = 0 31 | map = dict() 32 | ranks = dict() 33 | for row in nodes : 34 | if count > 0 : fhand.write(',\n') 35 | # print row 36 | rank = row[2] 37 | rank = 19 * ( (rank - minrank) / (maxrank - minrank) ) 38 | fhand.write('{'+'"weight":'+str(row[0])+',"rank":'+str(rank)+',') 39 | fhand.write(' "id":'+str(row[3])+', "url":"'+row[4]+'"}') 40 | map[row[3]] = count 41 | ranks[row[3]] = rank 42 | count = count + 1 43 | fhand.write('],\n') 44 | 45 | cur.execute('''SELECT DISTINCT from_id, to_id FROM Links''') 46 | fhand.write('"links":[\n') 47 | 48 | count = 0 49 | for row in cur : 50 | # print row 51 | if row[0] not in map or row[1] not in map : continue 52 | if count > 0 : fhand.write(',\n') 53 | rank = ranks[row[0]] 54 | srank = 19 * ( (rank - minrank) / (maxrank - minrank) ) 55 | fhand.write('{"source":'+str(map[row[0]])+',"target":'+str(map[row[1]])+',"value":3}') 56 | count = count + 1 57 | fhand.write(']};') 58 | fhand.close() 59 | cur.close() 60 | 61 | print "Open force.html in a browser to view the visualization" 62 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/pagerank/spreset.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | conn = sqlite3.connect('spider.sqlite') 4 | cur = conn.cursor() 5 | 6 | cur.execute('''UPDATE Pages SET new_rank=1.0, old_rank=0.0''') 7 | conn.commit() 8 | 9 | cur.close() 10 | 11 | print "All pages set to a rank of 1.0" 12 | -------------------------------------------------------------------------------- /Course-4-python_databases/w5_dbvisualisation/web_crawling.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-4-python_databases/w5_dbvisualisation/web_crawling.png -------------------------------------------------------------------------------- /Course-5 Capstone Retriveing Processing and Visulaisation data.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5 Capstone Retriveing Processing and Visulaisation data.pdf -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/certificate.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/certificate.pdf -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/certificate_Python for Everybody.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/certificate_Python for Everybody.pdf -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/GLineYearVisualization.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/GLineYearVisualization.jpg -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/GWordVisualization.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/GWordVisualization.jpg -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/Gbasic.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/Gbasic.jpg -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/IndexSQLite.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/IndexSQLite.jpg -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/TimeLineVisualization.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/TimeLineVisualization.jpg -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/index-sqlite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/index-sqlite.png -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/spliteBrowser-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/spliteBrowser-1.png -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/week-6-1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane output/week-6-1.jpg -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane/.gitignore: -------------------------------------------------------------------------------- 1 | content.sqlite 2 | index.sqlite -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane/gbasic.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | import time 3 | import zlib 4 | 5 | howmany = int(input("How many to dump? ")) 6 | 7 | conn = sqlite3.connect('index.sqlite') 8 | cur = conn.cursor() 9 | 10 | cur.execute('SELECT id, sender FROM Senders') 11 | senders = dict() 12 | for message_row in cur : 13 | senders[message_row[0]] = message_row[1] 14 | 15 | cur.execute('SELECT id, subject FROM Subjects') 16 | subjects = dict() 17 | for message_row in cur : 18 | subjects[message_row[0]] = message_row[1] 19 | 20 | # cur.execute('SELECT id, guid,sender_id,subject_id,headers,body FROM Messages') 21 | cur.execute('SELECT id, guid,sender_id,subject_id,sent_at FROM Messages') 22 | messages = dict() 23 | for message_row in cur : 24 | messages[message_row[0]] = (message_row[1],message_row[2],message_row[3],message_row[4]) 25 | 26 | print("Loaded messages=",len(messages),"subjects=",len(subjects),"senders=",len(senders)) 27 | 28 | sendcounts = dict() 29 | sendorgs = dict() 30 | for (message_id, message) in list(messages.items()): 31 | sender = message[1] 32 | sendcounts[sender] = sendcounts.get(sender,0) + 1 33 | pieces = senders[sender].split("@") 34 | if len(pieces) != 2 : continue 35 | dns = pieces[1] 36 | sendorgs[dns] = sendorgs.get(dns,0) + 1 37 | 38 | print('') 39 | print('Top',howmany,'Email list participants') 40 | 41 | x = sorted(sendcounts, key=sendcounts.get, reverse=True) 42 | for k in x[:howmany]: 43 | print(senders[k], sendcounts[k]) 44 | if sendcounts[k] < 10 : break 45 | 46 | print('') 47 | print('Top',howmany,'Email list organizations') 48 | 49 | x = sorted(sendorgs, key=sendorgs.get, reverse=True) 50 | for k in x[:howmany]: 51 | print(k, sendorgs[k]) 52 | if sendorgs[k] < 10 : break 53 | -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane/gline.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 20 | 21 | 22 |
23 | 24 | 25 | -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane/gline.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | import time 3 | import zlib 4 | 5 | conn = sqlite3.connect('index.sqlite') 6 | cur = conn.cursor() 7 | 8 | cur.execute('SELECT id, sender FROM Senders') 9 | senders = dict() 10 | for message_row in cur : 11 | senders[message_row[0]] = message_row[1] 12 | 13 | cur.execute('SELECT id, guid,sender_id,subject_id,sent_at FROM Messages') 14 | messages = dict() 15 | for message_row in cur : 16 | messages[message_row[0]] = (message_row[1],message_row[2],message_row[3],message_row[4]) 17 | 18 | print("Loaded messages=",len(messages),"senders=",len(senders)) 19 | 20 | sendorgs = dict() 21 | for (message_id, message) in list(messages.items()): 22 | sender = message[1] 23 | pieces = senders[sender].split("@") 24 | if len(pieces) != 2 : continue 25 | dns = pieces[1] 26 | sendorgs[dns] = sendorgs.get(dns,0) + 1 27 | 28 | # pick the top schools 29 | orgs = sorted(sendorgs, key=sendorgs.get, reverse=True) 30 | orgs = orgs[:10] 31 | print("Top 10 Organizations") 32 | print(orgs) 33 | 34 | counts = dict() 35 | months = list() 36 | # cur.execute('SELECT id, guid,sender_id,subject_id,sent_at FROM Messages') 37 | for (message_id, message) in list(messages.items()): 38 | sender = message[1] 39 | pieces = senders[sender].split("@") 40 | if len(pieces) != 2 : continue 41 | dns = pieces[1] 42 | if dns not in orgs : continue 43 | month = message[3][:7] 44 | if month not in months : months.append(month) 45 | key = (month, dns) 46 | counts[key] = counts.get(key,0) + 1 47 | 48 | months.sort() 49 | # print counts 50 | # print months 51 | 52 | fhand = open('gline.js','w') 53 | fhand.write("gline = [ ['Month'") 54 | for org in orgs: 55 | fhand.write(",'"+org+"'") 56 | fhand.write("]") 57 | 58 | for month in months: 59 | fhand.write(",\n['"+month+"'") 60 | for org in orgs: 61 | key = (month, org) 62 | val = counts.get(key,0) 63 | fhand.write(","+str(val)) 64 | fhand.write("]"); 65 | 66 | fhand.write("\n];\n") 67 | fhand.close() 68 | 69 | print("Output written to gline.js") 70 | print("Open gline.htm to visualize the data") 71 | -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane/gword.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 37 | -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane/gword.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | import time 3 | import zlib 4 | import string 5 | 6 | conn = sqlite3.connect('index.sqlite') 7 | cur = conn.cursor() 8 | 9 | cur.execute('SELECT id, subject FROM Subjects') 10 | subjects = dict() 11 | for message_row in cur : 12 | subjects[message_row[0]] = message_row[1] 13 | 14 | # cur.execute('SELECT id, guid,sender_id,subject_id,headers,body FROM Messages') 15 | cur.execute('SELECT subject_id FROM Messages') 16 | counts = dict() 17 | for message_row in cur : 18 | text = subjects[message_row[0]] 19 | text = text.translate(str.maketrans('','',string.punctuation)) 20 | text = text.translate(str.maketrans('','','1234567890')) 21 | text = text.strip() 22 | text = text.lower() 23 | words = text.split() 24 | for word in words: 25 | if len(word) < 4 : continue 26 | counts[word] = counts.get(word,0) + 1 27 | 28 | x = sorted(counts, key=counts.get, reverse=True) 29 | highest = None 30 | lowest = None 31 | for k in x[:100]: 32 | if highest is None or highest < counts[k] : 33 | highest = counts[k] 34 | if lowest is None or lowest > counts[k] : 35 | lowest = counts[k] 36 | print('Range of counts:',highest,lowest) 37 | 38 | # Spread the font sizes across 20-100 based on the count 39 | bigsize = 80 40 | smallsize = 20 41 | 42 | fhand = open('gword.js','w') 43 | fhand.write("gword = [") 44 | first = True 45 | for k in x[:100]: 46 | if not first : fhand.write( ",\n") 47 | first = False 48 | size = counts[k] 49 | size = (size - lowest) / float(highest - lowest) 50 | size = int((size * bigsize) + smallsize) 51 | fhand.write("{text: '"+k+"', size: "+str(size)+"}") 52 | fhand.write( "\n];\n") 53 | fhand.close() 54 | 55 | print("Output written to gword.js") 56 | print("Open gword.htm in a browser to see the vizualization") 57 | -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane/gyear.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | import time 3 | import urllib.request, urllib.parse, urllib.error 4 | import zlib 5 | 6 | conn = sqlite3.connect('index.sqlite') 7 | cur = conn.cursor() 8 | 9 | cur.execute('SELECT id, sender FROM Senders') 10 | senders = dict() 11 | for message_row in cur : 12 | senders[message_row[0]] = message_row[1] 13 | 14 | cur.execute('SELECT id, guid,sender_id,subject_id,sent_at FROM Messages') 15 | messages = dict() 16 | for message_row in cur : 17 | messages[message_row[0]] = (message_row[1],message_row[2],message_row[3],message_row[4]) 18 | 19 | print("Loaded messages=",len(messages),"senders=",len(senders)) 20 | 21 | sendorgs = dict() 22 | for (message_id, message) in list(messages.items()): 23 | sender = message[1] 24 | pieces = senders[sender].split("@") 25 | if len(pieces) != 2 : continue 26 | dns = pieces[1] 27 | sendorgs[dns] = sendorgs.get(dns,0) + 1 28 | 29 | # pick the top schools 30 | orgs = sorted(sendorgs, key=sendorgs.get, reverse=True) 31 | orgs = orgs[:10] 32 | print("Top 10 Organizations") 33 | print(orgs) 34 | # orgs = ['total'] + orgs 35 | 36 | counts = dict() 37 | months = list() 38 | # cur.execute('SELECT id, guid,sender_id,subject_id,sent_at FROM Messages') 39 | for (message_id, message) in list(messages.items()): 40 | sender = message[1] 41 | pieces = senders[sender].split("@") 42 | if len(pieces) != 2 : continue 43 | dns = pieces[1] 44 | if dns not in orgs : continue 45 | month = message[3][:4] 46 | if month not in months : months.append(month) 47 | key = (month, dns) 48 | counts[key] = counts.get(key,0) + 1 49 | tkey = (month, 'total') 50 | counts[tkey] = counts.get(tkey,0) + 1 51 | 52 | months.sort() 53 | # print counts 54 | # print months 55 | 56 | fhand = open('gline.js','w') 57 | fhand.write("gline = [ ['Year'") 58 | for org in orgs: 59 | fhand.write(",'"+org+"'") 60 | fhand.write("]") 61 | 62 | for month in months[1:-1]: 63 | fhand.write(",\n['"+month+"'") 64 | for org in orgs: 65 | key = (month, org) 66 | val = counts.get(key,0) 67 | fhand.write(","+str(val)) 68 | fhand.write("]"); 69 | 70 | fhand.write("\n];\n") 71 | fhand.close() 72 | 73 | print("Output written to gline.js") 74 | print("Open gline.htm to visualize the data") 75 | 76 | -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane/mapping.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/gmane/mapping.sqlite -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pageRank Output/force-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pageRank Output/force-2.png -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pageRank Output/froce.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pageRank Output/froce.png -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pageRank Output/spdump-2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pageRank Output/spdump-2.JPG -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pageRank Output/spdump-py.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pageRank Output/spdump-py.JPG -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pageRank Output/spider_py.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pageRank Output/spider_py.JPG -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pagerank/.gitignore: -------------------------------------------------------------------------------- 1 | content.sqlite 2 | index.sqlite 3 | spider.sqlite -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pagerank/BeautifulSoup.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pagerank/BeautifulSoup.pyc -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pagerank/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012, Michael Bostock 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * The name Michael Bostock may not be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL MICHAEL BOSTOCK BE LIABLE FOR ANY DIRECT, 21 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 24 | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 25 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 26 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pagerank/force.css: -------------------------------------------------------------------------------- 1 | circle.node { 2 | stroke: #fff; 3 | stroke-width: 1.5px; 4 | } 5 | 6 | line.link { 7 | stroke: #999; 8 | stroke-opacity: .6; 9 | } 10 | -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pagerank/force.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Force-Directed Layout 5 | 6 | 7 | 8 | 9 | 10 | 13 |
14 | 15 |

If you don't see a chart above, check the JavaScript console. You may 16 | need to use a different browser.

17 | 18 | 19 | -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pagerank/force.js: -------------------------------------------------------------------------------- 1 | var width = 600, 2 | height = 600; 3 | 4 | var color = d3.scale.category20(); 5 | 6 | var dist = (width + height) / 4; 7 | 8 | var force = d3.layout.force() 9 | .charge(-120) 10 | .linkDistance(dist) 11 | .size([width, height]); 12 | 13 | function getrank(rval) { 14 | return (rval/2.0) + 3; 15 | } 16 | 17 | function getcolor(rval) { 18 | return color(rval); 19 | } 20 | 21 | var svg = d3.select("#chart").append("svg") 22 | .attr("width", width) 23 | .attr("height", height); 24 | 25 | function loadData(json) { 26 | force 27 | .nodes(json.nodes) 28 | .links(json.links); 29 | 30 | var k = Math.sqrt(json.nodes.length / (width * height)); 31 | 32 | force 33 | .charge(-10 / k) 34 | .gravity(100 * k) 35 | .start(); 36 | 37 | var link = svg.selectAll("line.link") 38 | .data(json.links) 39 | .enter().append("line") 40 | .attr("class", "link") 41 | .style("stroke-width", function(d) { return Math.sqrt(d.value); }); 42 | 43 | var node = svg.selectAll("circle.node") 44 | .data(json.nodes) 45 | .enter().append("circle") 46 | .attr("class", "node") 47 | .attr("r", function(d) { return getrank(d.rank); } ) 48 | .style("fill", function(d) { return getcolor(d.rank); }) 49 | .on("dblclick",function(d) { 50 | if ( confirm('Do you want to open '+d.url) ) 51 | window.open(d.url,'_new',''); 52 | d3.event.stopPropagation(); 53 | }) 54 | .call(force.drag); 55 | 56 | node.append("title") 57 | .text(function(d) { return d.url; }); 58 | 59 | force.on("tick", function() { 60 | link.attr("x1", function(d) { return d.source.x; }) 61 | .attr("y1", function(d) { return d.source.y; }) 62 | .attr("x2", function(d) { return d.target.x; }) 63 | .attr("y2", function(d) { return d.target.y; }); 64 | 65 | node.attr("cx", function(d) { return d.x; }) 66 | .attr("cy", function(d) { return d.y; }); 67 | }); 68 | 69 | } 70 | loadData(spiderJson); 71 | -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pagerank/spdump.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | conn = sqlite3.connect('spider.sqlite') 4 | cur = conn.cursor() 5 | 6 | cur.execute('''SELECT COUNT(from_id) AS inbound, old_rank, new_rank, id, url 7 | FROM Pages JOIN Links ON Pages.id = Links.to_id 8 | WHERE html IS NOT NULL 9 | GROUP BY id ORDER BY inbound DESC''') 10 | 11 | count = 0 12 | for row in cur : 13 | if count < 50 : print(row) 14 | count = count + 1 15 | print(count, 'rows.') 16 | cur.close() 17 | -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pagerank/spjson.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | conn = sqlite3.connect('spider.sqlite') 4 | cur = conn.cursor() 5 | 6 | print("Creating JSON output on spider.js...") 7 | howmany = int(input("How many nodes? ")) 8 | 9 | cur.execute('''SELECT COUNT(from_id) AS inbound, old_rank, new_rank, id, url 10 | FROM Pages JOIN Links ON Pages.id = Links.to_id 11 | WHERE html IS NOT NULL AND ERROR IS NULL 12 | GROUP BY id ORDER BY id,inbound''') 13 | 14 | fhand = open('spider.js','w') 15 | nodes = list() 16 | maxrank = None 17 | minrank = None 18 | for row in cur : 19 | nodes.append(row) 20 | rank = row[2] 21 | if maxrank is None or maxrank < rank: maxrank = rank 22 | if minrank is None or minrank > rank : minrank = rank 23 | if len(nodes) > howmany : break 24 | 25 | if maxrank == minrank or maxrank is None or minrank is None: 26 | print("Error - please run sprank.py to compute page rank") 27 | quit() 28 | 29 | fhand.write('spiderJson = {"nodes":[\n') 30 | count = 0 31 | map = dict() 32 | ranks = dict() 33 | for row in nodes : 34 | if count > 0 : fhand.write(',\n') 35 | # print row 36 | rank = row[2] 37 | rank = 19 * ( (rank - minrank) / (maxrank - minrank) ) 38 | fhand.write('{'+'"weight":'+str(row[0])+',"rank":'+str(rank)+',') 39 | fhand.write(' "id":'+str(row[3])+', "url":"'+row[4]+'"}') 40 | map[row[3]] = count 41 | ranks[row[3]] = rank 42 | count = count + 1 43 | fhand.write('],\n') 44 | 45 | cur.execute('''SELECT DISTINCT from_id, to_id FROM Links''') 46 | fhand.write('"links":[\n') 47 | 48 | count = 0 49 | for row in cur : 50 | # print row 51 | if row[0] not in map or row[1] not in map : continue 52 | if count > 0 : fhand.write(',\n') 53 | rank = ranks[row[0]] 54 | srank = 19 * ( (rank - minrank) / (maxrank - minrank) ) 55 | fhand.write('{"source":'+str(map[row[0]])+',"target":'+str(map[row[1]])+',"value":3}') 56 | count = count + 1 57 | fhand.write(']};') 58 | fhand.close() 59 | cur.close() 60 | 61 | print("Open force.html in a browser to view the visualization") 62 | -------------------------------------------------------------------------------- /Course-5- Capstone Retrieving Processing and Visualizing Data with Python/pagerank/spreset.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | conn = sqlite3.connect('spider.sqlite') 4 | cur = conn.cursor() 5 | 6 | cur.execute('''UPDATE Pages SET new_rank=1.0, old_rank=0.0''') 7 | conn.commit() 8 | 9 | cur.close() 10 | 11 | print("All pages set to a rank of 1.0") 12 | -------------------------------------------------------------------------------- /Honor Certificate.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Honor Certificate.pdf -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Ashlesh Khajbage 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Python For Everybody Specialization.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Python For Everybody Specialization.pdf -------------------------------------------------------------------------------- /Python for Everybody.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Python for Everybody.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python For Everybody-Coursera 2 | This repository Consists of material And Certificates Earned From Online Learning Course on Coursera from University of Michigan 3 | 4 | [![forthebadge](https://forthebadge.com/images/badges/built-by-developers.svg)](https://forthebadge.com) [![forthebadge](https://forthebadge.com/images/badges/made-with-python.svg)](https://forthebadge.com) [![forthebadge](https://forthebadge.com/images/badges/built-with-love.svg)](https://forthebadge.com) 5 | 6 | ## About this Specialization 7 | This Specialization builds on the success of the Python for Everybody course and will introduce fundamental programming concepts including data structures, networked application program interfaces, and databases, using the Python programming language. In the Capstone Project, you’ll use the technologies learned throughout the Specialization to design and create your own applications for data retrieval, processing, and visualization. 8 | 9 | ## Courses In Specilisation 10 | 1. Programming for EveryBody -Getting Started with Python 11 | 2. Python Data Structures 12 | 3. Python Access Wen Data 13 | 4. Python Databases 14 | 5. Capstone : Retrieving, Processing, and Visualizing Data with Python 15 | 16 | ## Offered by 17 | ![University of Michigan logo](https://github.com/Ashleshk/Python-For-Everybody-Coursera/blob/master/michiganlogo.jpg) 18 | 19 | University of Michigan 20 | The mission of the University of Michigan is to serve the people of Michigan and the world through preeminence in creating, communicating, preserving and applying knowledge, art, and academic values, and in developing leaders and citizens who will challenge the present and enrich the future. 21 | 22 | ## Instructor 23 | ![Charles Russell Severance](https://github.com/Ashleshk/Python-For-Everybody-Coursera/blob/master/Charles-Severance.jpeg) 24 | 25 | **TOP INSTRUCTOR** 26 | Charles Russell Severance 27 | Clinical Professor, 28 | School of Information 29 | 30 | ## Link 31 | > [Specilisation Course](https://www.coursera.org/specializations/python) -------------------------------------------------------------------------------- /Slides And PDF/00-Master.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/00-Master.ppt -------------------------------------------------------------------------------- /Slides And PDF/Anaconda_CheatSheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Anaconda_CheatSheet.pdf -------------------------------------------------------------------------------- /Slides And PDF/Braile-Slide-Summaries-01-10.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Braile-Slide-Summaries-01-10.doc -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-01-Intro.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-01-Intro.pptx -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-01/MOOCMap-highres.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-01/MOOCMap-highres.jpg -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-01/RaspberryPi.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-01/RaspberryPi.jpg -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-01/Raspi_Colour_R.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-01/Raspi_Colour_R.png -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-01/clown_car.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-01/clown_car.jpg -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-02-Expressions.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-02-Expressions.pptx -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-03-Conditional.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-03-Conditional.pptx -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-04-Functions.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-04-Functions.pptx -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-05-Iterations.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-05-Iterations.pptx -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-06-Strings.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-06-Strings.pptx -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-07-Files.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-07-Files.pptx -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-08-Lists.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-08-Lists.pptx -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-09-Dictionaries.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-09-Dictionaries.pptx -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-10-Tuples.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-10-Tuples.pptx -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-11-Regex.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-11-Regex.pptx -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-12-HTML/Wikipedia_File_Utf8webgrowth_svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-12-HTML/Wikipedia_File_Utf8webgrowth_svg.png -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-12-HTML/ascii-cheat-sheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-12-HTML/ascii-cheat-sheet.png -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-12-HTML/bytes_decode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-12-HTML/bytes_decode.png -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-12-HTML/str_encode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-12-HTML/str_encode.png -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-12-HTML/unicode-web-site.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-12-HTML/unicode-web-site.png -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-12-HTTP.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-12-HTTP.pptx -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-13-WebServices.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-13-WebServices.pptx -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-14-Objects.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-14-Objects.pptx -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-15-Databases.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-15-Databases.pptx -------------------------------------------------------------------------------- /Slides And PDF/Pythonlearn-16-Data-Viz.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/Pythonlearn-16-Data-Viz.pptx -------------------------------------------------------------------------------- /Slides And PDF/intro-wrapup.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/Slides And PDF/intro-wrapup.pptx -------------------------------------------------------------------------------- /Slides And PDF/program1.py: -------------------------------------------------------------------------------- 1 | print("hello world") -------------------------------------------------------------------------------- /Slides And PDF/pythonlearn-11-Regex-handout.txt: -------------------------------------------------------------------------------- 1 | Python Regular Expression Quick Guide 2 | 3 | ^ Matches the beginning of a line 4 | $ Matches the end of the line 5 | . Matches any character 6 | \s Matches whitespace 7 | \S Matches any non-whitespace character 8 | * Repeats a character zero or more times 9 | *? Repeats a character zero or more times 10 | (non-greedy) 11 | + Repeats a character one or more times 12 | +? Repeats a character one or more times 13 | (non-greedy) 14 | [aeiou] Matches a single character in the listed set 15 | [^XYZ] Matches a single character not in the listed set 16 | [a-z0-9] The set of characters can include a range 17 | ( Indicates where string extraction is to start 18 | ) Indicates where string extraction is to end 19 | -------------------------------------------------------------------------------- /michiganlogo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ashleshk/Python-For-Everybody-Coursera/ea256e11ac752c1d0ac6f5a766fb6f0d0d15a00d/michiganlogo.jpg --------------------------------------------------------------------------------

48 | This is a cool map from 49 | www.pythonlearn.com. 50 |