DEV Community

Vidit Sarkar
Vidit Sarkar

Posted on

Python Basics (Working with Interpreter and writing code in file)

Introduction

In my previous post I have discussed about how to install Python in your system. In this post I will go through how to work with Python Interpreter and how to write python code in a file and run it.

Contents

  • Working with Python interpreter
  • Writing code in a file and run it
  • What code editor should I use?

Working with Python interpreter

In your terminal type python and hit enter. You will see something like this

\> python
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

>>> sign is a indication that you are in python shell. Now type 3+2 and hit enter. Output will be 5,

>>> 3+2
5
>>>

You can also use -, * or / for subtraction, multiplication and division respectively.

Let us write a simple for-loop (I will discuss more about for-loop in a later post).

>>> for i in range(4):
...     print(i)
...
0
1
2
3
>>>

If you are typing something which will take multiple lines then those ... signs will going to show up. They are indication of continuation lines. Notice there is a colon after range statement and 4 spaces before print statement. That : sign is necessary (you can refer to here for more info). You can use any number of spaces, but they have to be greater than zero. It is a good practice to use 4 spaces, so I am going to use 4 spaces.

Writing code in a file and run it

Interpreter is good for small code and for testing purposes. But if your code gets bigger, then you should consider using a file. One main benefit is that you can save and use it for later purposes, but the code in interpreter can not be saved using easy methods (At least I think so).Here are the steps,

  • Create a file with .py extension and avoid using spaces in the name. Example - app.py.
  • Copy and paste the code of for-loop in that file and save it. Same rule for spaces are also applicable here and also don't forget to put the : sign.
for i in range(4):
    print(i)
  • In the terminal type python app.py and you will see output like this.
\> python app.py
0
1
2
3

\>

By default, Python source files are treated as encoded in UTF-8. To declare an encoding other than the default one, a special comment line should be added as the first line of the file. The syntax is as follows,

# -*- coding: encoding -*-

For example, to declare that Windows-1252 encoding is to be used, the first line of your source code file should be,

# -*- coding: cp1252 -*-

You can check more about valid encodings here. Note that # sign indicates a comment line in python.
One exception to the first line rule is when the source code starts with a UNIX “shebang” line (not for Windows users). In that case, the encoding declaration should be added as the second line of the file.

#!/usr/bin/env python3
# -*- coding: ascii -*-

On BSD’ish Unix systems, Python scripts can be made directly executable, like shell scripts, by putting the line,

#!/usr/bin/env python3

at the very beginning of the file (assuming that the interpreter is on the user’s PATH). #! must be the first two characters of the file.

Give the script executable mode, or permission, using chmod and run it like a script.

$ chmod +x app.py

You can check more about this here.

What code editor should I use?

There are lots of popular code editors available out there like Visual Studio Code, Atom, Notepad++ and more. You can use any editor you prefer. You can even use IDEs. As long as you can write code and run it, any code editor or IDE should be fine. I personally use Visual Studio Code or Notepad++ to write code and run python files using command prompt (or integrated terminal of VS Code).

Happy coding!

Top comments (0)