• Courses: LernerPython.com
• Newsletters: BetterDevelopersWeekly.com • BambooWeekly.com
• Books: PythonWorkout.com • PandasWorkout.com
• Videos: YouTube.com/reuvenlerner
Just about every day, my students ask great questions. I love sharing those questions (and my answers), along with other resources to help you improve your Python/Pandas fluency.
from operator import add, sub, mul
ops = {'+':add, '-':sub, '*':mul}
s = input('Enter +, -, or * : ')
r = ops[s](10, 20)
print(f'10 {s} 20 = {r}')
from operator import add, sub, mul
ops = {'+':add, '-':sub, '*':mul}
s = input('Enter +, -, or * : ')
r = ops[s](10, 20)
print(f'10 {s} 20 = {r}')
My book, #Python Workout (2nd edition), is now the #20 best-selling book in programming languages at Amazon.
Thanks so much to everyone who has bought it, and also to everyone leaving such kind reviews.
My book, #Python Workout (2nd edition), is now the #20 best-selling book in programming languages at Amazon.
Thanks so much to everyone who has bought it, and also to everyone leaving such kind reviews.
d = {'a':10, 'b':20, 'c':30}
list(d) # ['a', 'b', 'c']
d.pop('b') # remove 'b'
d['b'] = 99 # re-add 'b'
list(d) # ['a', 'c', 'b']
This applies in for loops, too!
d = {'a':10, 'b':20, 'c':30}
list(d) # ['a', 'b', 'c']
d.pop('b') # remove 'b'
d['b'] = 99 # re-add 'b'
list(d) # ['a', 'c', 'b']
This applies in for loops, too!
New questions every Wednesday: bambooweekly.com
New questions every Wednesday: bambooweekly.com
d = defaultdict(0) # ❌ Error!
Pass a function that returns the default:
d = defaultdict(int) # int() returns 0
d = defaultdict(list) # list() returns []
d = defaultdict(dict) # dict() returns {}
d = defaultdict(0) # ❌ Error!
Pass a function that returns the default:
d = defaultdict(int) # int() returns 0
d = defaultdict(list) # list() returns []
d = defaultdict(dict) # dict() returns {}
myfunc('outfile.txt', filename='abcd', x=100)
Solution: Set filename to be positional only:
def myfunc(filename, /, **kwargs):
Now *any* keyword argument works!
myfunc('outfile.txt', filename='abcd', x=100)
Solution: Set filename to be positional only:
def myfunc(filename, /, **kwargs):
Now *any* keyword argument works!
s = 'ab cd ef'
s.split() # ['ab', 'cd', 'ef']
Limit splits with maxsplit:
s.split(maxsplit=1) # ['ab', 'cd ef']
Or split from the right, with a limit:
s.rsplit(maxsplit=1) # ['ab cd', 'ef']
s = 'ab cd ef'
s.split() # ['ab', 'cd', 'ef']
Limit splits with maxsplit:
s.split(maxsplit=1) # ['ab', 'cd ef']
Or split from the right, with a limit:
s.rsplit(maxsplit=1) # ['ab cd', 'ef']
source (via linkedin): hubs.la/Q042bvtT0
the book:
• on manning: hubs.la/Q042bwqc0
• on amazon: hubs.la/Q042bwGq0
source (via linkedin): hubs.la/Q042bvtT0
the book:
• on manning: hubs.la/Q042bwqc0
• on amazon: hubs.la/Q042bwGq0
Most books expect too much. But not my new "AI-Assisted Python for Nonprogrammers"!
It's meant for you. And uses AI to help you learn, *not* to write code for you.
Check it out, in early release: buff.ly/4BCUU49
Most books expect too much. But not my new "AI-Assisted Python for Nonprogrammers"!
It's meant for you. And uses AI to help you learn, *not* to write code for you.
Check it out, in early release: buff.ly/4BCUU49
mylist = [10, 20, 30]
for i in mylist:
print(i)
if i < 100:
mylist.append(i ** 2)
This prints 10, 20, 30, 100, 400, and 900.
BUT don't insert or remove. That'll cause real trouble.
mylist = [10, 20, 30]
for i in mylist:
print(i)
if i < 100:
mylist.append(i ** 2)
This prints 10, 20, 30, 100, 400, and 900.
BUT don't insert or remove. That'll cause real trouble.
This is issue 156, marking 3 years of weekly Pandas challenges.
This week, we look at Winter Olympic data. Since they love snow so much, we'll use #Polars along with #Python #Pandas.
Check it out, at bambooweekly.com.
This is issue 156, marking 3 years of weekly Pandas challenges.
This week, we look at Winter Olympic data. Since they love snow so much, we'll use #Polars along with #Python #Pandas.
Check it out, at bambooweekly.com.
def sum_and_mean(nums):
return sum(nums), sum(nums)/len(nums)
Unpack it into separate variables:
num_sum, num_mean = sum_and_mean([10, 20, 25])
def sum_and_mean(nums):
return sum(nums), sum(nums)/len(nums)
Unpack it into separate variables:
num_sum, num_mean = sum_and_mean([10, 20, 25])
class StringList:
def __init__(self, d):
self.d = d
def __setitem__(self, i, v):
self.d[i] = str(v)
sl = StringList(['2', '4', '6', '8'])
sl[2] = 99
sl.d
['2', '4', '99', '8']
class StringList:
def __init__(self, d):
self.d = d
def __setitem__(self, i, v):
self.d[i] = str(v)
sl = StringList(['2', '4', '6', '8'])
sl[2] = 99
sl.d
['2', '4', '99', '8']
class DoubleIndex:
def __getitem__(self, i):
return i * 2
d = DoubleIndex()
d[5] # returns 10
Use [] for custom logic — returning filenames, random numbers, or database records.
class DoubleIndex:
def __getitem__(self, i):
return i * 2
d = DoubleIndex()
d[5] # returns 10
Use [] for custom logic — returning filenames, random numbers, or database records.
class MyList:
def __init__(self, items):
self.items = items
def __getitem__(self, i):
return self.items[i]
m = MyList([1,2,3])
m[0] # returns 1
class MyList:
def __init__(self, items):
self.items = items
def __getitem__(self, i):
return self.items[i]
m = MyList([1,2,3])
m[0] # returns 1
In the latest Bamboo Weekly, we use #Python and #Pandas to find out. Then we compare its price with silver, and with central banks' gold and dollar holdings.
Get data analysis puzzles every Wednesday: bambooweekly.com
In the latest Bamboo Weekly, we use #Python and #Pandas to find out. Then we compare its price with silver, and with central banks' gold and dollar holdings.
Get data analysis puzzles every Wednesday: bambooweekly.com
d = {'a':2, 'b':4}
list(d.items()) # ✅ calls method
list(d.items) # ❌ passes method object
Result:
Error: 'builtin_function_or_method' object is not iterable
Remember: () calls the method!
d = {'a':2, 'b':4}
list(d.items()) # ✅ calls method
list(d.items) # ❌ passes method object
Result:
Error: 'builtin_function_or_method' object is not iterable
Remember: () calls the method!
In my AI-Powered workshops, you'll solve problems using Claude Code. We'll discuss prompts, code output, and testing, and the implications.
Hear more at my free info session today (Thursday): buff.ly/Rs7J4Eh
In my AI-Powered workshops, you'll solve problems using Claude Code. We'll discuss prompts, code output, and testing, and the implications.
Hear more at my free info session today (Thursday): buff.ly/Rs7J4Eh