Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Of course there are a lot of great text editors out there. Sublime Text, Brackets, Atom. Iâve always personally been a fan of Atom because it is completely free, and it has a lot of available packages and themes that makes coding a little easier. Here, Iâll look at how you can set up a âpython friendlyâ development environment with Atom, some of the packages that are useful to coding in python, and then take a look at writing some basic code.
1) Downloading Atom
First thingâs first, if weâre going to use Atom as our text editor, we better download it. Hereâs a link to do just that: https://atom.io/
2) Install a Syntax Theme
Once you have Atom installed, you can go to preferences, and in the preferences menu, select +install. Then, select the option for themes to download a theme. A syntax theme will have a color scheme that will make the code easier to read at a glance. Iâve found that certain themes seem to be great in JavaScript, but they arenât as readable in Python. For example, Iâve always been a big fan of Atom Dark and One Dark, for my UI and Syntax themes. Up until recently Iâve mainly coded in JavaScript, React and Node, and this theme works great for me with those languages. However, in Python, I donât like it as much. Finding a syntax theme that works for you can be difficult, especially once you get used to one theme.
Here are some of my favorites for Python:
1) Atom Material
This is the theme Iâll be using throughout this post, and is the one seen in the example above. I like it because a lot of themes tend to be somewhat monochromatic, and donât seem to be as readable for that reason. Atom Material uses a lot of colors, and there is a high contrast, making it easy to find and read code. https://atom.io/themes/atom-material-syntax
2) Jackhammer
This one doesnât have quite as wide of a use of color as Atom Material, but still a lot of contrast. https://atom.io/themes/jackhammer-syntax
3) PreDawn
Another great one. Iâve seen quite a few developers use this one, and seems very Python friendly. https://atom.io/themes/predawn-syntax
If you want to use one of these, or find your own, you can search for them in the search bar to the right of the themes button. Or do a generic search for âsyntaxâ or âpythonâ.
3) AutoComplete Python Package
Not everybody likes the auto-complete feature. And I agree, sometimes it can get in the way. But, if youâre like me, and just starting out in Python, it can be really useful. The autocomplete-python package gives you the option of being powered by either Jedi or Kite. Jedi is a locally based library, while Kite accesses an online library. Hereâs more documentation on the package: https://atom.io/packages/autocomplete-python
4) File Icons Package
The file Icons package allows you to see the icon/logo for each file type youâre working on. If you only work in Python, or only JavaScript or whatever, then this is probably a non-issue. But more than likely your file tree can contain multiple files in multiple languages and formats. In which case, having this package will help you easily find files within your tree. https://atom.io/packages/atom-file-icons
5) Linter-flake8 package
This is a great modular source code checker. Once you install the package, youâll also need to user the command line to complete the installation. Instructions on how to do it are all well documented. https://atom.io/packages/linter-flake8
6) Minimap package
Minimap isnât python-specific, but is a great tool for any coding language. Once your code gets to be hundreds of lines long, it can be difficult to find where you are in the code base. Minimap provides a âzoomed outâ view of the entire code, and highlights where you are in the code, keeping the entire visualization in a concise sidebar in the atom editor. https://atom.io/packages/minimap
7) python-autopep 8Â package
autopep8 automatically formats Python code to conform to the PEP 8 style guide. It uses the pycodestyle utility to determine what parts of the code needs to be formatted. autopep8 is capable of fixing most of the formatting issues that can be reported by pycodestyle. Once you have this package installed, you might want to click on settings and select the âFormat on Saveâ option. This will also require to you complete the installation using pip on the command line, as you can see in the documentation. https://atom.io/packages/python-autopep8
8) script package
By far one of the most important packages on this list is the script package. This allows you to run scripts in the Atom editor using the âcommand + iâ keyboard shortcut. The code will run in a panel at the bottom of the text editor. https://atom.io/packages/script
9) Getting started writing code in Python
One of my favorite ways to get started is finding a problem to solve. The Project Euler site has a number of math based problems that you can solve in any programming language. Since I am new to Python, I decided to take one of the project euler problems I had solved in JavaScript, and solve the same one in Python. Number 5 on project Euler reads like this:
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
So in JavaScript, I solved the problem this way:
let number = 1; while ( number % 1 !== 0 || number % 2 !== 0 || number % 3 !== 0 || number % 4 !== 0 || number % 5 !== 0 || number % 6 !== 0 || number % 7 !== 0 || number % 8 !== 0 || number % 9 !== 0 || number % 10 !== 0 || number % 11 !== 0 || number % 12 !== 0 || number % 13 !== 0 || number % 14 !== 0 || number % 15 !== 0 || number % 16 !== 0 || number % 17 !== 0 || number % 18 !== 0 || number % 19 !== 0 || number % 20 !== 0) { number = number + 1; }
console.log(number);
Basically here I have a while loop, starting at 1, it checks if the number is divisible by 1 through 20 with no remainder. If not, it adds one and checks again until it finds a match. So with Python, I tried to simply use the same code, but translate it to Python, which looked like this, and let my preface this with the fact that this is really not a good idea.
number = 1while (number % 1 <> 0 or number % 2 <> 0 or number % 3 <> 0 or number % 4 <> 0 or number % 5 <> 0 or number % 6 <> 0 or number % 7 <> 0 or number % 8 <> 0 or number % 9 <> 0 or number % 10 <> 0 or number % 11 <> 0 or number % 12 <> 0 or number % 13 <> 0 or number % 14 <> 0 or number % 15 <> 0 or number % 16 <> 0 or number % 17 <> 0 or number % 18 <> 0 or number % 19 <> 0 or number % 20 <> 0): number += 1 print number
Basically this does the same thing as the above code, but written for Python. A major difference that I quickly realized is that Python will print every single number from 1 until it reaches the answer, which can take a long time. So as I started trying to understand Python, here are some of the syntactical differences, that will help in getting started:
1) For Loops
I found that a normal for loop in JavaScript looks like this:
for (var i = 0; i < array.length; i++) { array[i] }
However, in Python, it looks more like the JavaScript âfor inâ loop:
for x in range(0, 3): print "We're on time %d" % (x)
for loop from 0 to 2, therefore running 3Â times.
2) Variables
In JavaScript, variables need to be defined by first calling âlet, var or constâ.
let x = 1const y = 2var z = 3let my_array = [1, 2, 3, 4]
In Python, you can simply type the variable name without defining it as a variable.
x = 1y = 2z = 3
my_array = [1, 2, 3, 4]
3) Functions
In JavaScript, functions are called using âfunctionâ and can take a parameter, or multiple parameters:
function test_prime(n){// do stuff}
In Python, they are basically the same but are called using the keyword âdefâ instead.
def test_prime(n): //do stuff
Another major difference between function calls is that in JavaScript, the work inside the function is always between curly braces, following the parameters. In Python, a function begins with a colon, and instead of curly braces, the function is anything indented below the line calling the function. With some of the packages we installed earlier, youâll often see âunexpected indentâ letting you know that youâve indented something that doesnât need to be there. This is a little hard to get used to, if youâre more familiar with something like JavaScript, where indentions donât affect the code.
4) console log
In JavaScript, if you want to run a script, or block of code, you can simply console.log it
console.log(my_function);
In Python, you generally use the âprintâ command
print solution
With these building blocks in place, I then went on Stack Overflow and found a better solution for my original project Euler problem:
check_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
def find_solution(step): for num in xrange(step, 999999999, step): if all(num % n == 0 for n in check_list): return num return None
if __name__ == '__main__': solution = find_solution(2520) if solution is None: print "No answer found" else: print "found an answer:", solution
Hereâs a link to the problem on Stack Overflow: https://stackoverflow.com/questions/8024911/project-euler-5-in-python-how-can-i-optimize-my-solution
Here, we have a variable âcheck_listâ which holds an array.
Inside the function âdef find_solutionâ we step through the numbers 1 through 999999999, and check each number for divisibility against the numbers in our array. If it matches, we return the number, if not, we return none.
Then, we feed the solution the step of 2520, instead of checking each number, or every other number, it only checks every 2520th number, which allows the code to run much faster. Then it prints the solution, or if none were found, it prints that.
Obviously Iâm fairly new in Python, but if youâre like me, and just getting started, hopefully this gives you a few things to consider. Thanks so much for reading!
Setting up a Python Development Environment in Atom was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.