In our previous lessons, typing
'hello world' into the console quickly returns
'hello world' as an output. Using an editor is different. When using an editor, you write your code, save it with a .py extension (e.g. hello.py), and run it. An IDE is more advanced and can compile your code. You can check out pycharm once we’ve done a little more coding and you’ve gotten comfortable compiling your own code.
Choose an editor
There are a couple of editors you can use. Some even prefer to use certain editors over IDEs because they are equally good. Here are a few editors you can choose from.
This is the most basic editors of all and it’s quite difficult to use it for writing very lengthy codes. It’s just a plain ol text editor.
This editor understands python and will change the colors of your code once you save the file with the .py extension. It can suggest texts you’ve typed before, helping you to write code without cramming all the syntax of the programming language.
This is a more advanced form of notepad. It is similar to sublime; it just appears a bit classic in look (my opinion).
This is quickly becoming a star among editors. It looks sleek and it does the job just like the other editors.
There are more out there, but any of these would be good for you to start with. (If you’re using the online editor and shell here. then you’ve been given an editor over there. However, you could still download any of these editors and write your code locally, then you run it online).
Using an editor
Switching from the console to the editor can be a bit tricky sometimes. You may find your self running your code and seeing nothing happen. Before I go ahead to explain what I mean, let’s talk about how to run your code after saving it.
Open your editor, type the code
print 'I love this editor'. Save it as test.py. Open command prompt or terminal and navigate to the folder your file is located in. (Check this article to know how to use the command line.)
Now, run the file using the command
python test.py (python followed by the name of the file).
One thing you should know is, typing just
'hello world' will not print hello world to the console. Without using the
Something more than ‘hello world’
Now that we’ve compiled our first python script, let’s go ahead and do something more. Take a look at the code below, type it into your editor. (Do not copy and paste. The action of typing the code helps you learn better)
#test.py #code that calculates profit #variables cost_price = 100.0 selling_price = 105.0 units = 250 #the code profit = (selling_price - cost_price)*units #output it print profit
Let’s take it line at a time. The first line is a comment. Comments are things we write in our code that do not run as part of the program. They are usually for information and description. In python we write comments by beginning the line with a
selling_price are float variables and
units is an integer variable.
profit is a float variable derived from the calculation done by our program. We subtract
cost_price which is 100.0 from
selling_price which is 105.0 then we multiply by the number of products to get our profit.
The final line is important. Without that, you won’t see your result. Comment it out and run your code and see what happens.
Run your code in the console like you did before.
In our next lesson, we’ll discuss functions. So far we’ve been using the function
Stay curious, stay motivated, programmers never stop learning. 🙂