def single_tax(pay):
tax = 0
if pay > 415050:
tax += (pay - 415050) * .396
pay = 415050
if pay > 413350:
tax += (pay - 413350) * .35
pay = 413350
if pay > 190150:
tax += (pay - 190150) * .33
pay = 190150
if pay > 91150:
tax += (pay - 91150) * .28
pay = 91150
if pay > 37650:
tax += (pay - 37650) * .25
pay = 37650
if pay > 9275:
tax += (pay - 9275) * .15
pay = 9275
return tax + pay * .1Neovim
Thinking Machines
Why Neovim?
On Neovim
- Neovim is free,
- Neovim is very widely available,
- Neovim is highly customizable,
- Neovim is stable, year-on-year,
- and Neovim is quite powerful.
Why not Neovim?
- Modal - it is not a “What you see is what you get” (WYSIWYG) editor like MS Word, Google Docs, Notepad, or VS Code
- Terminal-based - it is used within the terminal and not often as a stand-alone program.
- Steep-learning curve - students generally find it difficult to get started, though it is widely preferred by experienced users
Running Example
Diving In
- Taking Neovim as a given, we’ll:
- Continue the piecewise functions example
- Touch on Python and the terminal in the context of Neovim
- Introduce Neovim modes, motions, and operators.
- Spoilers for the last exercise!
Recall
- Write function
def single_tax(pay):- Return tax cost.
- Return not print!
- Bonus: Also write
single_tax_ratewhich returns the percent tax rate at some income level.
| Rate | From |
|---|---|
| 10% | 0 |
| 15% | 9275 |
| 25% | 37650 |
| 28% | 91150 |
| 33% | 190150 |
| 35% | 413350 |
| 39.6% | 415050 |
Solutions
Tedious to Type
PS C:\Users\calvin> python
Python 3.12.5 (tags/v3.12.5:ff3bc82, Aug 6 2024, 20:45:27) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def single_tax(pay):
... tax_policy = [
... [415050, .396],
... [413350, .35],
... [190150, .33],
... [91150, .28],
... [37650, .25],
... [9275, .15]
... ]
... tax = 0
... for bracket in tax_policy:
... if pay > bracket[0]:
... tax += (pay - bracket[0]) * bracket[1]
... pay = bracket[0]
... return tax + pay * .1
...
>>> single_tax(400000)
115529.25
>>>Instead: .py files
- Python has an associated file type, the
.pyfile.- Similar to
.png(portable network graphic) for images - Similar to
.pdf(portable document format) for papers
- Similar to
Test-based
- The
.pyfile is much like a.txtfile in that:- It is a small, lightweight file containing only typed characters.
- It can be opened, edited, and viewed in many editors.
- The
.pyfile can save typed code to be used withinpython.
Enter Neovim
- Neovim is a great way to edit text.
Give it a shot!
Students often object to switching from WYSIWYG editors but:
- Many students know no other way to write text-based files, and
- We’ve ample time this term to properly explore Neovim
Be patient, but also work hard!
Getting Neovim
Installation
- We go to the offial webpage to get an installer for our computer:
- Neovim offers a few more installation options than you may be used to.
- I recommend…
Install from download
Downloads are available on the Releases page.
Terminal
- On MS Windows
- I press Windows key, type “terminal” then press enter.
- On MacOS
- I open Launchpad, type “terminal” then press enter.
MacOS

Windows

Expression evaluation
- Returning to the terminal, we can type at the “prompt”.
- On MacOS, perhaps a line that begins with
$and a flashing cursor - On Window, perhaps
PS C:\Users\calvin>PSstands for “powershell” - more latter.C:\Users\calvinis the name of a folder - more latter>is the prompt, with a flashing cursor.
Run Neovim
In the following examples, I remove line numbers to denote they are not Python code snippets.
- On MacOS, type
nvim
MacOS
$ nvim- On Windows, type
nvim
Windows
PS C:\Users\calvin> nvim- On both, press the ↵ᴇɴᴛᴇʀ key.
See Neovim
- You’ll see something like this:
NVIM v0.11.1
Nvim is open source and freely distributable
https://neovim.io/#chat
type :help nvim<Enter> if you are new!
type :checkhealth<Enter> to optimize Nvim
type :q<Enter> to exit
type :help<Enter> for help
type :help news<Enter> to see changes in v0.11
Help poor children in Uganda!
type :help iccf<Enter> for informationModes
Modality
nvimis a modal text editor- By default, typed characters will not appear in the document.
- Rather, by default, we being in command mode.
- We won’t worry about that too much, but the first command to learn is
iifor insert
Insert Mode
- By the way, you should see
-- INSERT --at the bottom of the terminal window. - Insert mode is not unlike WYSIWYG
- Use it prolificly until you are more comfortable.
- Navigate with arrow keys or (depending on system) mouse
- For now, let’s copy paste in the
piecewisefunction.
Piecewise
- For me, basic copy/paste commands like:
- ctrl/cmd+c, ctrl/cmd+v
- rclick->menu->copy
- Worked amicably in
iinsert mode
pw.py
- For
.pyfiles I’ll include line numbers but add a filename on top.
Command Mode
- Having written some text, we now need to save it to a file.
- Press the “ESC” (Escape) key to return to command mode
- Your cursor will move the bottom of the terminal, where
-- INSERT --was
Remember!
Remember to press escape!
- In
nvimexamples today, it will not always be easy to make a note of when to press escape! - As a rule, use escape before anytime you try to do something (exit, save)
- These “do things” usually are
:prefixed - Press
ito get back to typing (insert mode)
Issuing commands
- After
:w(write) and the name of the file - Include the
.pyending! - I wrote:
:w pw.pySave and Quit
- You can always use
:wto save while working. - Make any edits you make need to make - a missed paren perhaps.
- Then both save and exit
nvimwith:x- This special command saves the file and exits
nvim
- This special command saves the file and exits
- You will return to the command line you started with, likely without incident.
- But there will now be a new file you can use!
Scripting
Back in Terminal
- On MacOS, you’ll see
nvimand a new prompt
MacOS
$ nvim
$- On Windows, the same
Windows
PS C:\Users\calvin> nvim
PS C:\Users\calvin> Aside: Terminal Commands
- Like Python and Neovim, there are also terminal commands!
- Two are
python/python3andnvim!
- Two are
- Another is
ls, which stands for “list”- This command has been around so long, it is from an era when commands were shorter to save precious computer memory!
- It “lists” local files, and can list them by name!
Aside: Using ls
- Try out this command, to see that you have a
pw.pyfile!
$ ls pw.py- What do you see (different on Windows and MacOS)?
Checkpoint
- If you do not have a
pw.pyfile, stop here until you have one!
Windows
PS C:\Users\calvin> ls pw.py
Directory: C:\Users\cd-desk
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 5/20/2025 3:21 PM 151 pw.pyMacOS
$ ls pw.py
pw.py
$.py files
There are two common ways to use
.pyfilesI believe the most common is via
importTo begin, at the command line, start Python
On Windows, type
python
Windows
PS C:\Users\calvin> python- On MacOS, type
python3
MacOS
$ python3- On both, press the ↵ᴇɴᴛᴇʀ key.
See Python
- You’ll see something like this:
Python 3.12.5 (tags/v3.12.5:ff3bc82, Aug 6 2024, 20:45:27) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>- Take note of the prompt!
>>>- Those three are how you know it is Python, and not the Terminal, that you are working in.
Import
- Versus last time, we now have a piecewise function written in a
.pyfile we can reference. - To do so, we:
- Type
importfollowed by a space - Type the filename less the
.pyextension - It should look like this:
- Type
PS C:\Users\calvin> python
Python 3.12.5 (tags/v3.12.5:ff3bc82, Aug 6 2024, 20:45:27) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pw
>>>Modules
import pwwill introduce a new variable to Python to which we can refer by name (pw)- It is of a new type for us, “module”
>>> type(pw)
<class 'module'>- Modules correspond to
.pyfiles!
>>> pw
<module 'pw' from 'C:\\Users\\calvin\\pw.py'>- (The
fromwill look different on different computers)
Importing Functions
- We can use functions inside modules in a few ways.
- First, we can use the module name (
pw) followed by a dot or period (.) followed by the name of the function.
>>> pw.piecewise(-1)
14Renaming Functions
- Second, we can just use single equals assignment
>>> piecewise = pw.piecewise
>>> piecewise(4)
9Using from
- Third, we can use the
fromkeyword in our import.- This is by far preferred (it is more clear)
>>> from pw import piecewise
>>> piecewise(11)
-9- I only show you the other methods to make it clear what this method is doing.
Exit Python
- Oftentimes, we’ll run a bit of Python then want to go back to the command line to use
nvim
exit()
You can exit the Python >>> prompt back to Windows > or MacOS $ at any time:
- Use
exit() - Use
quit() - Use ctrl+z
Practice starting and leaving Python a few times!
Using Terminal
Command Line Utilities
I find the following tedious:
nvim- Write some code
:w somefile.py- ESC then
:x pythonfrom somefile import somefunctionsomefunction(x)exit()
Alternative
- We will instead:
- Show how to how run script files
- Show how to provide input to script files
Hello, Terminal
- To begin, we will do terminal “Hello, World!”
- I use
nvim hi.pyto create and open a new file namedhi.py - Then I write:
Running Scripts
- I save and exit via
:x - Then I run the Python script via
python hi.py - For me, it looks like this:
$ nvim hi.py
$ python3 hi.py
Hello, terminal!
$What happened?
- Basically, Python runs the code written in the file as if it were entered at the prompts.
- It then provides the expected print statements.
- We can run multiple times without rewriting the script.
Piecewise Script
- Say we wish to find the piecewise results from the prior lecture.
nvim pw.py- I use
Gto jump to the end of the file- This is a “vim motion”
- I use
ito change into insert mode, and add:
- Save and exit with (escape then)
:x
Test it
$ nvim pw.py
$ python3 pw.py
[-1, 14]
[4, 9]
[5, 9]
[8, 9]
[11, -9]
$Using input
- Fortunately, Python has a built-in function that is (basically) the opposite of
print - Within the terminal, run
python - Then within Python, call
input()
>>> input() - What happens?
What if?
- We want to be able to check values without either
- Opening
pythonand doing animport, or - Opening
nvimand editing the list
- Opening
- We can do that by providing input
- We write a script that expects us to type in some information!
Python input
- Python helpfully has an input that is basically the opposite of print.
input- It is easiest to learn by trying it out!
Providing input
- Open Python
- Call the function
input() - Type something - anything - then press enter.
>>> input()
something anything
'something anything'- The
input()function returns the text that is typed in
Script input
- Let’s make a little script to try out
input()
- Think about the steps to create and run this file!
Example
$ nvim
$ python3 reply.py
I wrote this text then pressed enter.
I wrote this text then pressed enter.
$- Try it out!
Return v. Print
Print, Not Return
- To see the results of some expression or computation other work in the terminal, we must
printwithin scripts. print()for terminalreturnwithin Python
Datatypes
A Wrinkle
- There’s one little problem here.
- Let’s update
pw.pyto to run oninput()and see what happens.
Uh oh!
- I see the following:
$ python3 pw.py
10
Traceback (most recent call last):
File "/home/user/pw.py", line 10, in <module>
y = piecewise(x)
File "/home/user/pw.py", line 2, in piecewise
if (x < 4):
TypeError: '<' not supported between instances of 'str' and 'int'
$Recall
- We worked with different types last time:
- Think about what is happening here
- The type of
input()is a new type -str- Short for “string” - as in string of characters
Casting
- Changing data of one type (like
str) into another (likeint) is called casting - In Python, to cast we use the name of the type we want as a function on the value in the type we don’t want.
- Easy to try within
python
Try it
>>> x = input()
10
>>> x
'10'
>>> type(x)
<class 'str'>
>>> y = int(x)
>>> y
10
>>> type(y)
<class 'int'>
>>> y < 11
True- Python shows something is a string by enclosing it within quote marks
''
Fix pw.py
Use it
$ nvim pw.py
$ python3 pw.py
10
-8
$ python3 pw.py
-1
14
$Exercise
Income Tax
- We’ll return to the income tax example, and
- Add a wrinkle.
- Here’s an income tax solution:
Arguments
- There is one other way to specify what you want a script to do.
- Command line arguments
- This is my favorite way (simpliest to use)
- (Bit harder to write)
- It is based on the Python
sysmodule
sys
- Create and try out the following file:
- What does it do?
‘argv’
- ‘argv’ stands for “argument vector”
- You may have noticed it is a list
- Other programming languages sometimes call lists vectors
- Computer systems often call lists vectors
- Python follows this convention
Add an arg
- What happens if you do this?
$ python3 args.py hello worldRemember!
Exercise
- Create a file
tax.py - Have it accept an income as a command line argument
- Have it print a tax cost at command line.
- Here’s an example how it should work!
$ python3 tax.py 400000
115529.25Challenge Problem
- Accept two command line arguments
- Income, and
- One of
['single', 'married', 'separate', 'head']
- Print the tax regardless of “single-ness”