Learn by Doing Exercises Using Python
Learning to program by taking a course is great, but the way that you really learn how to program is by just doing it. The best courses give you plenty of good hands-on exercises.
I learn best from others’ examples. I’ll modify the code and tweak it to my needs. There is so much code that is freely available on Github, Stackoverflow, w3 Schools and other sites that the key ingredient is just to be curious and explore.
And, I learn especially well when I need to solve a problem of my own.
1. Python Interactive Dictionary
I found this code in a blog post entitled, “Master Python through building real-world applications (Part 1)“. The code was simple to understand and it worked. The user just enters in a word and the definition is retrieved from a dictionary text file and displayed on the screen.
The only change I made was to look up the dictionary of words online. The author of the original code required you to download the dictionary file and read it from your local hard drive. I just added in a few lines of code to look up the dictionary online.
It’s through doing little changes like this that you make the program your own and accelerate your learning by building off the work of others. This was tested with Python 3.7.
from difflib import get_close_matches
### READ THE DICTIONARY ONLINE ###
import urllib.request, json
with urllib.request.urlopen(“https://raw.githubusercontent.com/Dhrumilcse/Interactive-Dictionary-in-Python/master/dictionary.json”) as url:
data = json.loads(url.read().decode())
### END – READ THE DICTIONARY ONLINE ###
word = word.lower()
if word in data:
elif word.title() in data:
elif word.upper() in data:
elif len(get_close_matches(word, data.keys())) > 0:
action = input(“Did you mean %s instead? [y or n]: ” % get_close_matches(word, data.keys()))
if (action == “y”):
return data[get_close_matches(word, data.keys())]
elif (action == “n”):
return (“The word doesn’t exist, yet.”)
return (“We don’t understand your entry. Apologies.”)
word_user = input(“Enter a word: “)
output = retrive_definition(word_user)
if type(output) == list:
for item in output:
2. File Compare (and Output)
This next code snippet was created from several different sources. I just typed into Google search terms like: “python how to read a CSV file into an array” and “python how to write a text file”. Each time I found code from Stackoverflow, Github or w3 Schools and just extracted the parts I needed and created my own program.
I had a need to create a CSV file that was just two elements, a Stock Symbol and it’s CIK code. CIK stands for Central Index Key and it is used by the SEC (Securities and Exchange Commission) to locate companies and their owners in the SEC Edgar Database.
This was tested with both Python 2.7 and Python 3.7. You’ll need to download the two csv files below to get the program to work.
with open(‘c:\\data\\sp500.csv’) as file:
array = file.readlines()
f = open( ‘c:\\data\\cik.csv’ , ‘r’ )
f2 = open( ‘c:\\data\\sp500cik.csv’ , ‘w’)
line = f.readline()
line = f.readline()
while line :
#print line.split(‘|’)+’ ‘+line.split(‘|’)
x = line.split(‘|’)
z = line.split(‘|’).zfill(10)
if x in str(array):
#print “S&P 500 match ” + z + ” ” + x
f2.write(chr(34) + x + chr(34) + “,” + chr(34) + z + chr(34) +”\n”)
line = f.readline()
3. Sometimes the program you need is too complicated to create on your own
In this case, you may need to connect with your network to get some assistance from an experienced developer. Many developers are generous with their knowledge and are more than happy to help you get started.
I have also worked with developers through sites like Upwork, Fiverr or Guru. Recently, I had a developer on Upwork write me a program for a very small price to query the SEC Edgar Database to find Owners in a publicly traded company that have purchased their own stock. The program outputs the data to a sortable HTML table.
The idea behind this is that if an owner of a company used their own money to buy their company’s stock, that they thought it was a good investment and it might indicate the stock will go up in price.
If you’d like to try this program for yourself, join our Community and then click on the left side bar called “Topics” and then click on “FREE Resources”. Next click on the FREE Resource called “Stock Purchase Program” and follow the directions.
Have Questions? Join a community of Data Science Professionals and others interested in Data Science
Upskill Create is all about creating a lifestyle of continuous learning and then applying what you have learned to secure your future and create a life you love.
We offer an engaging private community of fellow “Upskillers” to provide an environment where we can learn, collaborate and encourage each other in this journey. You can request an invitation to join this FREE community at the bottom of this post.
Just click this button to join today!