How-to guide: Learn to code

Is 2020 the year you will learn to code? If self-doubt is holding you back, don't let it! Let's work on it together.

Below is a simplified Python environment, courtesy of DataCamp, a popular platform for learning data science. Follow the instructions and type your code in the script.py section. Click Run or Submit to see your output in the IPython shell section.

# Click "Run" to see this example
print("hello world!")
# Then, modify the above line of code
# so that it prints "hello {your name}"

# When you're finished, hit "Submit" Ex().has_no_error()
Ex().has_output(r"^([h|H][e|E][l|L][l|L][o|O]) (?!world)w+( w.+)*!*$",no_output_msg="Just replace the word world with your own name.")

Not so hard, right? How about a little basic math?

# Add two numbers like this
# (again, click "Run" to see the example):
print(5 + 5)
# Subtract two numbers using the '-' sign

# Multiply two numbers using the '*' sign

# Divide one number by another using the '/' sign

Ex().has_no_error()

Memorizing and typing out all these mathematical formulas can get a little tedious. Spreadsheet applications, like Microsoft Excel, provide pre-defined functions to perform more complex calculations.

Python has lots of useful built-in functions too.

# Say you had a list of scores
scores = [77, 50, 93, 75, 82, 87, 78, 94, 36, 29]

# The min() function will tell you the minimum number
print(min(scores))

# See if you can get the maximum using the max() function

# Now sum them all up with the sum() function

Ex().has_no_error()

You can also define your own functions in Python. Here's one that calculates the percent change. See if you can use it to figure out the percent change in the number of points scored by star NBA player James Harden during the Houston Rocket's last two games. Harden scored 18 points against the Portland Trailblazers on January 29, and he scored 35 points against the Dallas Mavericks on January 31.

# Here's how you might calculate a percent change function
def calc_percent_change(old, new):
return round(((new - old) / old) * 100)
# Say your older number was 1 and your newer one was 2
# Here's how you use it:
print(calc_percent_change(1, 2))

# Now you try it!
Ex().has_no_error()

Data shows us precise details. However, journalists—and the audiences we serve—care more about the big picture. Scatter plots, histograms and other data visualizations help us find and convey insights buried in data.

Let's make a data viz of points put up by Harden in his last 12 games.

from matplotlib.dates import datestr2num, DateFormatter
import matplotlib.pyplot as plt

x = [ datestr2num("Fri 1/3"), datestr2num("Wed 1/8"),
datestr2num("Thu 1/9"), datestr2num("Sat 1/11"),
datestr2num("Tue 1/14"), datestr2num("Wed 1/15"),
datestr2num("Sat 1/18"), datestr2num("Mon 1/20"),
datestr2num("Wed 1/22"), datestr2num("Fri 1/24"),
datestr2num("Wed 1/29"), datestr2num("Fri 1/31"),
]
y = [
44, 41, 17, 32, 41, 13, 34, 29, 27, 12, 18, 35
]

plt.plot_date(x, y, xdate=True, linestyle ='solid')
plt.gca().xaxis.set_major_formatter(DateFormatter('%b-%d'))
plt.show()

Ex().has_no_error()

If you were able to follow the instructions above, then you're capable of learning to code. You don't need a technical background. You don't need a computer science degree. You are already prepared. Actually, you have already started.

But should you learn to code?

Journalists have been asking this question since at least 2012 as part of increase in interest in encouraging STEM education. At this point, it's not just a trope; it's a dated trope.

I am journalist, and I write a lot of code. I also teach other journalists how to write code. I am (still) asked this question often, and here's my answer.

No one else gets to say you should learn to code. And you have good reason to be skeptical of anyone giving this as career advice. 

Even when offered in earnest, as catchall career advice, “learn to code” reeks of technochauvinism, a neologism introduced by data journalist and New York University professor Meredith Broussard to describe the assumption that technically sophisticated solutions are always superior. Such thinking tends to discount relationship building, first-person observation, storytelling and other activities intrinsic to good journalism.

Having said all that, no one else gets to say you shouldn't learn to code. It's your time, attention and energy, and you should spend it doing the work that feels most urgent to you. If you do take this path, you will be in good company with stellar journalists producing meaningful journalism at places like The New York Times, The Washington Post and the Los Angeles Times, ProPublica, BuzzFeed, Quartz, The PuddingFiveThirtyEight and many others.

Here is some additional advice if you're ready to start learning: 

Use an interactive learning environment

We learn best through feedback, so it's best to use a tool that's constantly telling you how you are doing.

We already mentioned DataCamp, but there's also Codecademy, repl.it and several others. Any of these is a great place to start because you don't have to install anything on your own computer until you are full committed to being a coder.

Do a little every day.

Bootcamps and workshops are great kickstarter, but it's all for naught unless you keep practicing.

Like interviewing, writing, photography or any other skill, coding can't be learned in a weekend, a month or even a year. It's a skill set you can build on and expand over the course of your entire career as you continue to practice and learn more about it. 

But give yourself a break.

You will get stuck. You will spin your wheels. You will frantically send hundreds of search queries to Google with prayers of finding the right code snippet to copy and paste. These are your cues to stop and take a break. 

Take your hands off the keyboard. Stand up. Stretch. Walk around. 

Like any other muscle, your brain needs rest. It can't function effectively through sheer force of will. You can keep on mindlessly slinging code until the sun explodes, or you can step away for five minutes and let your subconscious work its wonders.

Even better than waiting for mental fatigue to set in, build regular breaks into your work routine. I try to take a short break at least every 30 minutes.

To sum up, just take it one step at a time, however far you want to take it.

Comments

Comments are closed.