Can You Write Python Code in Notepad, Wordpad or Word?


Can You Write Python Code in Notepad, Wordpad or Word?

If you are just starting to learn programming or you have to quickly edit a Python script on a Windows machine, where you cannot or do not want to install any extra software, you might be wondering which tool is the best for the job? Is it possible to edit Python code with Notepad or Word?

You can edit Python files in Notepad, but you should not use it as there are much better tools for code editing. You can install a proper code editor or an IDE in a few minutes and you will be much more productive. Avoid using a rich editor like Wordpad, Microsoft Word, LibreOffice Writer or OpenOffice.

In this article I’ll show you how to edit Python scripts in notepad, I’ll explain why it is not a good idea to do so and I’ll show some better alternatives.

Why You Should Never Edit Python Files with Word Processors like Wordpad, Microsoft Word, OpenOffice or LibreOffice Writer

A word processor or rich text editor is a program that is able to add formatting to your text like changing font weight, size or style. These programs should never be used to edit program code because the interpreter/compiler will not be able to add the extra formatting that they add to the source code.

For example take this simple python script:

print("hello world")

If I edit it with LibreOffice Writer and save it in rtf format, the result will be something like:

{\rtf1\ansi\deff3\adeflang1025
{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}{\f1\froman\fprq2\fcharset2 Symbol;}{\f2\fswiss\fprq2\fcharset0 Arial;}{\f3\froman\fprq2\fcharset0 Liberation Serif{\*\falt Times New Roman};}{\f4\fswiss\fprq2\fcharset0 Liberation Sans{\*\falt Arial};}{\f5\fnil\fprq2\fcharset0 Noto Sans CJK SC;}{\f6\fnil\fprq2\fcharset0 Lohit Devanagari;}{\f7\fnil\fprq0\fcharset128 Lohit Devanagari;}}
{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;\red192\green192\blue192;}
{\stylesheet{\s0\snext0\widctlpar\hyphpar0\cf0\kerning1\dbch\af5\langfe2052\dbch\af6\afs24\alang1081\loch\f3\hich\af3\fs24\lang2057 Normal;}
{\s15\sbasedon0\snext16\sb240\sa120\keepn\dbch\af5\dbch\af6\afs28\loch\f4\fs28 Heading;}
{\s16\sbasedon0\snext16\sl276\slmult1\sb0\sa140 Text Body;}
{\s17\sbasedon16\snext17\sl276\slmult1\sb0\sa140\dbch\af7 List;}
{\s18\sbasedon0\snext18\sb120\sa120\noline\i\dbch\af7\afs24\ai\fs24 Caption;}
{\s19\sbasedon0\snext19\noline\dbch\af7 Index;}
}{\*\generator LibreOffice/6.0.7.3$Linux_X86_64 LibreOffice_project/00m0$Build-3}{\info{\creatim\yr2020\mo4\dy13\hr0\min8}{\revtim\yr2020\mo4\dy13\hr0\min10}{\printim\yr0\mo0\dy0\hr0\min0}}{\*\userprops}\deftab709
\viewscale100
{\*\pgdsctbl
{\pgdsc0\pgdscuse451\pgwsxn11906\pghsxn16838\marglsxn1134\margrsxn1134\margtsxn1134\margbsxn1134\pgdscnxt0 Default Style;}}
\formshade\paperh16838\paperw11906\margl1134\margr1134\margt1134\margb1134\sectd\sbknone\sectunlocked1\pgndec\pgwsxn11906\pghsxn16838\marglsxn1134\margrsxn1134\margtsxn1134\margbsxn1134\ftnbj\ftnstart1\ftnrstcont\ftnnar\aenddoc\aftnrstcont\aftnstart1\aftnnrlc
{\*\ftnsep\chftnsep}\pgndec\pard\plain \s0\widctlpar\hyphpar0\cf0\kerning1\dbch\af5\langfe2052\dbch\af6\afs24\alang1081\loch\f3\hich\af3\fs24\lang2057{\rtlch \ltrch\loch
print(\u8220\'93hello, world!)}
\par }%                             

You can see that a lot of extra formatting and metadata has been added to the file. If I open it with a word processor it will display the code correctly, but this is completely unintelligible for the Python interpreter, the code will not run anymore.

How To Edit Python Code in Notepad

Notepad is not a word processor, it can handle only plain text files, it will not add any formatting to your code, so it is safe to use it for coding.

However notepad is not made for writing code, it is just a very basic editor, so if you want to implement anything beyond the most trivial toy examples you’ll have a hard time working with it.

You will have an especially hard time if you try to edit Python files, as Python code is structured by indentation, which means that a missing or an extra space will break your program and working with notepad will make it very hard to find the error.

Why You Should Use an IDE or a Code Editor

Editing Multiple Files

Very early in your programming career, you will reach a point when you’ll leave single file scripts behind and start working on real-world projects that consist of multiple files (sometimes even tens of thousands). As very often you’ll have to jump back and forth between a bunch of files, you must have an editor that can handle multiple buffers at the same time, as you definitely do not want to run dozens of editor instances simultaneously.

Powerful Editing Features

There are plenty of advanced editing features that can speed up your work, some of the most used ones: regex-based search and replace, block editing, multiple cursors, macros/scripts, etc.

Syntax Highlighting

Adding some pretty colors to your code can make it much easier to quickly scan through it, understand it or find syntax or logic errors

Syntax Checking and Linting

In most IDEs, you’ll get instant feedback for syntax errors. Setting up a linter will help you to adhere to coding best practices and to keep a consistent coding style.

Automatic Code Generation and Refactoring

Generating boilerplate code by one click or a hotkey can save you a lot of boring typing and can greatly reduce development time.

Advanced Code Navigation

Most IDEs can build a source map for your code, so you do not have to look up the definition or the usages of a variable or a function, you can just quickly jump to it with the help of your editor.

Code Completion

Context-sensitive code completion will help you avoid typos and save a lot of time.

Built-in Debugging

Many IDEs provide some builtin debugging feature or allow you to connect to a debugger via the editor interface. Placing breakpoints in the editor and running a debugger without leaving the IDE is much more comfortable than running a separate debugger.

Builtin CVS Support

Having integrated code versioning support in your IDE is also a great addition. It can help you with visualizing your git branches, resolving conflicts and examining diffs.

What’s the Difference Between an IDE and a Code Editor?

Code editors are powerful text editors specialized for editing source code files. IDE stands for Integrated Development Environment - they provide a lot of builtin extra functionality that is usually not available for code editors - or only available by installing 3rd party plugins.

Code editors are usually more lightweight, while IDEs tend to be heavier on resources like HDD and memory space.

Most of the time IDEs are specialized for a certain language or group of languages while code editors tend to be language agnostic.

Best IDEs and Code Editors for Python

PyCharm

A great IDE by Jetbrains. It is written in Java, so it is anything but lightweight. On the upside, it has the most extensive Python support of all the IDEs.

PyCharm has a freely available Community Edition and a premium Pro Edition with a few extra features.

Atom

Code editor developed by GitHub. Lightweight, powerful and beginner-friendly.

Vi/Vim

Vi is arguably the most powerful code editor, but it has a notoriously steep learning curve. Its UI is extremely minimalistic and the key bindings seem to be unintuitive for beginners, but once you get used to it Vi can speed up your work tremendously. It is optimized for touch typists: the keystrokes are organized around the home row.

Notepad++

Notepad alternative for developers. It’s free and open-source, but available only on Windows systems. Its functionality is rather basic, but it’s quite fast.

Visual Studio Code

Microsoft’s open-source code editor, it’s Python functionality is almost as extensive as PyCharms. It is a bit more lightweight though and supports a lot of different languages other than Python.

JupyterLab

Jupyter is not a regular text editor, but an IDE with an integrated web server that is used primarily to edit Jupyter Notebooks.

It is quite popular in academic circles and among machine learning and data science specialists.

Which IDE to Choose?

If you are working only in Python I’d recommend choosing PyCharm. You can download the CE for free, you won’t need the premium features as a beginner.

If you’re planning to do full-stack development you are probably better off with something like VS Code, as PyCharm is not the greatest choice for the frontend and switching back and forth between two editors is pretty tedious.

If you’re going to work with scientific computations, data science or machine learning applications you should definitely give JupyterLab a try.