Lots of people ask me how they can translate their knowledge of Python into a new career path. The thing is, no one is looking for people who know Python. Rather, they're looking for people who know how to solve problems using Python.
Consider this: No one will hire you to speak a foreign language. But they will hire you to:
• translate to/from a foreign language
• conduct business negotiations in a foreign language
• be a tour guide in a foreign language
• edit advertising copy in a foreign language
Nov 14, 2022 • 11 tweets • 2 min read
Did you know that #Python only supports two kinds of arguments?
I don't mean data types. Python functions (if we ignore type hints) don't care about those. Rather, there are only two ways that Python can take arguments and assign them to parameters.
Let's back up a moment.
When we call a function, we can pass it one or more arguments. These can be:
1. Positional arguments, assigned to parameters based on their positions and 2. Keyword arguments, assigned to parameters based on explicit name-value pairs.
Do x and y have the same values? Are they interchangeable?
The answer, as always, is: Yes and no.
x is a list. We know this because we assigned it the result of running a list comprehension — that's what the [] indicate.
y, by contrast, is a generator. We know this because we used a generator expression — which looks the same as a list comprehension, but uses () instead.
In today's #Python class, someone asked which was faster, given s = 'a b c':
x,y,z = s.split() # unpacking
or
fields = s.split() # explicit assignment
x = fields[0]
y = fields[1]
z = fields[2]
Let's find out! A thread...
First, let's put these two snippets of code into functions:
def unpack():
s = 'a b c'
x,y,z = s.split()
def assign():
s = 'a b c'
fields = s.split()
x = fields[0]
y = fields[1]
z = fields[2]
Why put them in functions? To disassemble them!
Jan 18, 2022 • 8 tweets • 2 min read
A #Python student showed me this code, and asked how to improve it:
def f(g, **kwargs):
for k, v in kwargs.items():
if "x1" == k:
g.x1 = v
elif "x2" == k:
g.x2 = v
elif "x3" == k:
g.x3 = v
First, what's this function doing?
- It gets an object and a dict of keyword arguments
- It goes through each keyword arg
- For three keys, it creates an attribute of that name on the object (g), with the value.
Let's assume the function can work for *all* keys. What can we do?
I can try to use "sum(mylist)". But that'll fail, because they're strings. I can use a list comprehension to convert them:
sum([int(y)
for y in x])
But it also works without the []:
sum(int(y)
for y in x)
Huh?
List comprehensions return (by definition) lists. So this:
[int(y)
for y in x]
returns a list -- in this case, a list of ints. The list is returned all at once, though.
Jan 13, 2022 • 16 tweets • 3 min read
Many #Python beginners are confused by space characters, empty strings, and the term "whitespace." After all, how can nothing be something? (Or: How can something be nothing?)
A thread about nothing!
(Cue the Seinfeld theme, I guess...)
Given:
s = 'a b'
This string contains three characters. As humans, we only see the "a" and "b", two characters separated by a space.
But computers don't work that way: The space character is a character. It takes up just as much space in memory as either "a" or "b".
Dec 29, 2021 • 4 tweets • 2 min read
Want a #Python#pandas data frame with all Apollo missions, indexed by date?
df = df.set_index('Date')
The first line scrapes the Wikipedia page for the Apollo program, putting all HTML tables into data frames. The missions are in the third table, aka index 2.
The second line turns lines containing date ranges into single (launch) dates, also removing commas and hyphens.
Dec 5, 2021 • 25 tweets • 5 min read
For about a year now, I've been upset with the unvaccinated. Why don't they, or won't they, get vaccinated? Are they suicidal, ignorant, or sociopathic?
Two great books have changed my thinking: High Conflict, by @amandaripley, and Empire of Pain, by @praddenkeefe.
A thread.
First, just to make it clear: I'm vaccinated (3 shots). I think the covid vaccines are among the greatest achievements of modern science. My family all got vaccinated ASAP. They work, and save lives. Everyone should get vaccinated.
Jun 4, 2021 • 12 tweets • 3 min read
Soon after you start to learn #Python, you start to hear that some data is mutable (i.e., can be changed), whereas other data is immutable (i.e., cannot be changed).
I find that many developers confuse "immutable" with "constant." These are very different ideas.
To appreciate the difference, remember that a Python variable is a reference to an object. It is *not* an alias for a location in memory.
So when you say "x = 5", you aren't sticking 5 in x's memory location. Rather, you are saying that the name "x" is another way to refer to 5.
Jun 2, 2021 • 12 tweets • 3 min read
One of the hardest things for people to learn in #Python is list comprehensions. Some quick tips that make them easier to work with:
(1) Break them up into multiple lines! It drives me batty to see people writing comprehensions on a single line.
You can then reason about each line separately:
[int(x)
for x in '1 2 b 3'.split()
if x is.digit()]
Line 1: Expression
Line 2: Iteration
Line 3: Condition
Or if you're a fan of SQL:
Line 1: SELECT
Line 2: FROM
Line 3: WHERE
Oct 15, 2020 • 20 tweets • 4 min read
Some thoughts on teaching online (a thread).
Background: I've done corporate #Python and data-science training for 20 years. Even before the pandemic, I taught live, online courses (via WebEx and Zoom) at least 1 week/month. I also offer many video (recorded) courses.
My work slowed down in April-May, when companies didn't know what was happening.
Training is now about where it was before. Except it's 100% online.
I teach everything from "Python for non-programmers" to "intro to data science." 5 days/week, 4-8 hours/day. All online.