Python Interpreter Tutorial | Python 3 Tutorial For Beginners

Python Interpreter Tutorial | Python 3 Tutorial For Beginners 2018. Here in this blog post Coding compiler sharing Python interpreter tutorial for beginners. This tutorial is for absolute beginners who are looking for learning Python programming. Let’s start learning Python 3.

Python Interpreter Tutorial

Here in this Python tutorial we are going to learn about invoking Python interpreter, parameter passing, interactive mode, the interpreter and its environment.

Related Articles: Python Getting Started Guide |  Learn Python In One Day

Invoking the Python interpreter

Python interpreter is usually installed in the target machine’s /usr/local/bin/python3.5directory. The /usr/local/bindirectory contains the search path into the Unix shell’s to make sure you can enter:

Python3.5

Since the installation path of the Python interpreter is optional, this may also be another path, which you can contact the user who installed Python or the system administrator to confirm (for example, /usr/local/pythonis a common choice).

Related Article: Python For Machine Learning

On a Windows machine, Python is usually installed in C:\Python35position, of course, you can modify this value when you run the installation wizard. To add this directory to your PATH environment variable, you can type the following command in a DOS window:

Set  path =% path % ; C : \ python35

Usually you can enter an end-of-file character in the main window (on a Unix system Control-D, on a Windows system Control-Z) to have the interpreter exit with a 0 status code. If that does not work, you can enter the quit()exit command interpreter.

The Python interpreter has a simple line editing function. On Unix systems, any Python interpreter may have added GNU readline library support, which has sophisticated interactive editing and history and other features.

Entering Control-P in the main Python window may be the easiest way to check if command-line editing is supported. If the beeps (computer speakers), then you can use the command-line editing; introduce more shortcuts, refer to the interactive input line editing history back . If there is no sound or display ^Pcharacters, command line editing is not available; you can only delete and re-enter the characters from the current line typed by the backspace key.

The Python interpreter has some operations similar to the Unix shell: when invoked with a terminal device (tty) as standard input, it interactively interprets and executes the command; when called with a file name parameter or as a file as standard input, it reads the file and file as a script execution.

The second way is to start the Python interpreter , this method can command line execute Python statements, similar to the shell’s -c option. Since Python statements often contain spaces or other special shell characters, generally recommended command in single quotes wrapped up.python -c command [arg] ...

Some Python modules can also be used as scripts. You can use the command to call them, which is similar to type the full path name in the command line execution module source file the same.python -m module [arg] ...

When using a script file, you often run the script and enter interactive mode. This can also be achieved by preceding the script with the -i parameter.

Parameter Passing

When the interpreter, the script name and additional parameters passed in a named sys.argvlist of strings. You can get this list by execution , the length of the list is greater than or equal to 1; when there is no given script and parameters, it also has at least one element: this is an empty string. When the script name is specified as (indicating standard input), it is set to be used when the -c command is used . When using the -m module parameter, the full name of the specified module is set. -c commandor -m module after the parameter will not be intercepted by the Python interpreter’s option processing but left in , the script for command operations.import syssys.argv[0]'-'sys.argv[0]'-' sys.argv[0]'-c' sys.argv[0] sys.argv

Interactive mode

When reading from the tty command interpreter is said to work in interactive mode . In this mode it is performed according to the main prompt, usually three main prompt greater than ( >>>); for continuation is called secondary prompt by three dots ( ...). Before the first line, the interpreter prints the welcome message, version number, and authorization prompt:

$ python3.5
Python 3.5.2 (default, Mar 16 2014, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

A multi-line structure requires a secondary prompt, for example, the following if statement:

>>> the_world_is_flat  =  1 
>>> if  the_world_is_flat : 
...     print ( "Be careful not to fall off!" ) 
... 
Be careful not to fall off!

 

The Interpreter And Its Environment

Source code

By default, Python source files are UTF-8 encoded. With this encoding, characters from most languages ​​around the world can be used simultaneously in strings, identifiers, and comments—although the Python standard library uses only ASCII characters as identifiers, this is just a convention that any portable code should follow.

If you want to display all the characters correctly, your editor must be able to recognize that the file is UTF-8 encoded, and the font it uses can support all the characters in the file.

You can also specify different character encodings for source files. To this end, the #!row (first row) after the insertion of at least one row special comment line to define the source code files:

# -*- coding: encoding -*-

With this declaration, the source file will be treated as all things with encoding refer to the UTF-8 encoding treat. You can find a list of available codes in the codecs section of the Python Library Reference Manual .

For example, if your editor does not support UTF-8 encoded files, but supports some other encodings like Windows-1252, you can define:

# -*- coding: cp-1252 -*-

This makes it possible to use all the characters in the Windows-1252 character set in the source file. This special code must be in the annotation file of the first or second line definition.

Related Articles: 

Python Interview Questions

Python Programming Interview Questions

Leave a Reply

Your email address will not be published. Required fields are marked *