Starting with Python

Python programs must be written with a particular structure. The syntax must be correct, or the interpreter will generate error messages and not execute the program.

For example       print(“VKS-Learning Hub” )

We will consider three ways in which we can run this statement

  • 1. command-line interpreter
  • 2. enter the program directly into IDLE’s interactive shell
  • 3. enter the program into IDLE’s editor, save it, and run it.

command-line interpreter

To run interactive shell on command prompt you should follow the following step.

  1. After installation, the python interpreter lives in the installed directory. On Windows machines, you must reach to your directory where you have installed the python. typically its inside user folder’s inside c: drive for example i have installed inside vinsr user folder. you can use Path command to check your python folder path and use cd command to reach to your python folder

To add this directory to your path, you can type the following command into the command prompt in a DOS box:

You can start Python from DOS, or any other system that provides you a command-line interpreter or shell window. Typing “python” in the command line will invoke the interpreter in immediate mode. We can directly type in Python expressions and press enter to get the output.

C:\Users\vinsr\AppData\Local\Programs\Python\Python36-32>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.

print(“hello world”)
hello world

IDLE’s interactive shell

  • To start  IDLE from the Microsoft Windows Start menu
  • The IDLE interactive shell will open with >>> prompt.
  • You may type the above one line Python program directly into IDLE and press enter to execute the program
  • the result will be display using the IDLE interactive shell

IDLE’s editor

Since it does not provide a way to save the code you enter, the interactive shell is not the best tool for writing larger programs. The IDLE interactive shell is useful for experimenting with small snippets of Python code

    null
  • IDLE’s editor. IDLE has a built in editor. From the IDLE menu, select New Window.
  • Editor will open a file . Type the text  print(“VKS-Learning-Hub”) into the editor.

We can run the program from within the IDLE editor by pressing the F5 function key or from the editor’s Run menu: Run?Run Module. The output appears in the IDLE interactive shell window

If program is not saved it give message to save first before run

You can save your program using the Save option in the File menu as shown in Figure. Save the code to a file named try1.py. The extension .py is the extension used for Python source code.

If no error python shell will show out as given below

print(VKS-Learning Hub)

This is a Python statement. A statement is a command that the interpreter executes. This statement prints the message VKS-Learning Hub on the screen. A statement is the fundamental unit of execution in a Python program.

Statements may be grouped into larger chunks called blocks, and blocks can make up more complex statements. Higher-order constructs such as functions and methods are composed of blocks.

The statement print(“VKS-Learning Hub”) makes use of a built in function named print

It is important that no whitespace (spaces or tabs) come before the beginning of each statement.

In Python the indentation of statements is signi?cant and must be done properly. If we try to put a single space before a statement in the interactive shell or editor mode

interactive mode
editor mode