r/learnpython • • Jan 05 '24

Why are interpreters required for programming ?

i'm trying to learn python ....i'm hearing a lot of stuff about interpreters....just wanna know what are they for..why are they required...can i learn python without interpreter...without vs code or py charm

Cuz i wrote some codes in python and it worked without connecting python to interpreter ....why is an interpreter necessary

Plz enlighten me

14 Upvotes

71 comments sorted by

View all comments

1

u/PhilipYip Jan 08 '24

You can use Python in the Terminal to run a script file created for example in notepad however this is generally used only for small script files and you will lack a lot of visual aids which help you write code and recognise mistakes... Your question is kind of similar to the concept of writing a word document in notepad versus using a word processor. The word processor essentially has a large number of grammar check tools which flag up whitespace issues, missing commas and bad spelling which can be easily missed when writing a document. In this case of a document bad grammar will only make the document look unprofessional. In the case of code bad grammar makes the code harder to understand and if the Python interpreter cannot understand the code it will not fail to run the Python script file. The Python interpreter essentially interprets your instructions and then instructs the Python program to execute them.

IDEs typically use the Python interpreter to apply syntax highlighting which will for example colour code a str differently from an instance name. (Reddit is essentially a text editor and applies no syntax highlighting in the example below).

greeting = 'Hello World!'
farewell = 'Bye!'

Syntax highlight makes it easy to distinguish the two and reduces the changes of mixing the two up. Syntax highlighting will also show something is wrong for example if you forget to place a quote at the end of one of the str instances, the code will be unable to run. In an IDE the syntax highlight will show a problem in line 1 and also show an error beside the line number:

greeting = 'Hello World!
farewell = 'Bye!'

IDEs typically also use the Python interpreter to look up identifiers in response to a prefix. For example if you input p, identifiers that begin with p display such as the print function:

greeting = 'Hello World!'

The Python interpreter will also know that greeting is an instance of the str class and therefore when you input:

greeting.

The str identifiers display.

IDEs also use the Python interpreter to look up the docstring, inputting print() will display the input arguments expected for the print function.

The ability to quickly look up the documentation as you type will prevent issues such as using the wrong name for an input argument in a function or failing to provide an input argument.

If you are having issues installing Python, creating a Python environment and then trying to install and configure an IDE, steps which often confuse beginners and likely the reason for the above question then I would recommend installation of the Anaconda Python Distribution and use of one of the preinstalled IDEs such as JupyterLab or Spyder to get started. Once familiar with these you can then look at configuring VSCode. VSCode is essentially a text editor like notepad but has a large number of extensions that give it the features of an IDE for code editing. If you need more details I put together a markdown tutorial here Anaconda and VSCode Installation which gives installation instructions and a good overview of the Python related programs in Anaconda.