Basics of Python Programming

Python is an interpreted programming language. Python source code is compiled to bytecode as a .pyc file, and this bytecode can be interpreted.

There are two modes for using the Python interpreter:
  1. Interactive Mode
  2. Script Mode

Interactive Mode

Without passing python script file to the interpreter, directly execute code to Python prompt.

Example:
>>>2+6
Output:
8

The chevron at the beginning of the 1st line, i.e., the symbol >>> is a prompt the python interpreter uses to indicate that it is ready. If the programmer types 2+6, the interpreter replies 8.

Script Mode

Alternatively, programmers can store Python script source code in a file with the .py extension, and use the interpreter to execute the contents of the file. To execute the script by the interpreter, you have to tell the interpreter the name of the file. For example, if you have a script name MyFile.py and you're working on Unix, to run the script you have to type:

python MyFile.py

Working with the interactive mode is better when Python programmers deal with small pieces of code as you can type and execute them immediately, but when the code is more than 2-4 lines, using the script for coding can help to modify and use the code in future.


Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.