pycse - Beginner mode

There are some concepts in Python that are tricky for beginners. pycse.beginner is a somewhat experimental package to eliminate some of these issues. I don’t actually use it a lot, and I am not sure it is better than taking time to learn it another way. It exists in pycse though, so I document it here.

from pycse.beginner import *

Avoiding indexing in lists

Indexing can be tricky at first. There is syntax you have to learn, and you have to get comfortably with the idea that indexing starts at 0 in Python.

This is not critical though, and there are functional approaches in pycse to avoid this and to use more natural names.

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]

pycse.beginner defines functions for getting the first through fifth elements by name, and getting the nth element.

first(a)
1
second(a)
2
third(a)
3
fourth(a)
4
fifth(a)
5

You can get the “nth” item, with the caveat that n starts at 0.

nth(a, 8)
9

functional approach to slicing

You often want to “cut” a list, e.g. to get the list from some start position to stop position, optionally skipping items. Here we get the items from index=5 on. Remember that index=5 actually is the 6th position.

cut(a, 5)
[6, 7, 8, 9]
# get index 5..7
# as with Python, the stop position is not inclusive. It means up to but not including the stop.
cut(a, start=5, stop=7)
[6, 7]
# get every other element starting at index=0
cut(a, step=2)
[1, 3, 5, 7, 9]

Other pieces of a list

These functions are inspired by similar functions in the programming language lisp.

last(a)
9
# everything past the first element
rest(a)
[2, 3, 4, 5, 6, 7, 8, 9]
# everything but the last element
butlast(a)
[1, 2, 3, 4, 5, 6, 7, 8]

More user-friendly functions

Many numerical methods are not very user-friendly for new programmers. These functions provide (in my opinion) nicer user-interfaces.

A better fsolve

Let us solve \(x^2 = -2.5\). This does not have a real answer. To solve this we have to create a function that will be zero, and use fsolve.

def f(x):
    return x**2 + 2.5

fsolve simply gives you a warning, and an answer that does not work.

from scipy.optimize import fsolve
fsolve(f, 2.5)
/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/scipy/optimize/_minpack_py.py:178: RuntimeWarning: The iteration is not making good progress, as measured by the 
  improvement from the last ten iterations.
  warnings.warn(msg, RuntimeWarning)
array([-0.00090143])

nsolve actually raises an exception so you know it did not solve correctly.

nsolve(f, np.sqrt(2.5))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[17], line 1
----> 1 nsolve(f, np.sqrt(2.5))

NameError: name 'np' is not defined

Simpler integration

The quad function in scipy returns both the integral of a function and an estimated error. When you start out, this is more complex than preferred, so we change the behavior to only compute the integral, and to raise an exception if the result is not sufficiently accurate.

Here we evaluate \(\int_0^1 x^2 dx\).

def f(x):
    return x**2

integrate(f, 0, 1)
0.33333333333333337