You can learn about Introduction to Python Programming Programs with Outputs helped you to understand the language better.
Python Programming – How To Start Python
After installation is successfully completed, you can start Python in the following ways (see Figure 3.11). These are:
(a) Python Shell – Interactive mode (Commandline)
(b) Python Editor – Script mode (Integrated Development Environment – IDLE)
Python Shell is used to executing a single Python command and get the result in the same window. It waits for the input command from the user. As soon as the user enters the command, it executes it and displays the result. IDLE is a simple Integrated Development Learning Environment that comes with Python. The important feature of IDLE is that it is a program that allows the user to edit, run, browse and debug a Python program from a single interface.
Interactive Mode (Command-line)
Interactive mode of working means you type the command one at a time, and Python executes the given command and gives you output. An interactive interpreter of Python is also called Python Shell. Follow these steps to work in interactive mode while using Python:
(a) Click on Start Button —> All Programs —> Python 3.7(32 bit). It will give you a screen similar to the one shown in Figure 3.12.
Python shell is an interactive window where you can type Python code and see the output in the same window by pressing Enter key. When Python’s interactive mode starts up, it tells you which version is running, the date on which it was released, and a few hints about what to do next. Then, it displays the Python prompt: »> also known as primary prompt indicating that Python is ready to take commands from the user. When you use interactive mode, Python processes each line of code you type as soon as you press Enter key.
You can enter Python and start coding right away from the command line in the interactive interpreter. In Interactive mode, when you type Python command in front of Python command prompt >>>, the Python executes the given command there and then gives the output. For example, if you type 2+3 in front of Python prompt, it will display result 5. >>> 2+3 5
The Python interpreter can be used as a calculator. If you just want to do simple calculations, you can type numbers and operators and press Enter key to see the result, as shown in Figure 3.13. The >>> is the prompt the interpreter uses to indicate that it is ready. If you type 2 + 2, the interpreter displays result 4. (See Figure 3.13).
In Programming languages, a complete command is called a statement. In Python, you can also print statements. Type the following text to the right of the Python prompt and press Enter Key.
>>> print(“Hello World”)
- The quotation marks in the program indicate the beginning and end of the text to be displayed. They do not appear in the result.
will display the result as shown in figure
How to write multi-line code in Interactive mode?
You can write multi-line code in interactive mode. However, note that interactive mode does not let you save your work. When you write multi-line statements, the … (continuation prompt) will be dis¬played. This … (continuation prompt) is also called secondary prompt indicating that the interpreter is waiting for additional input to execute the current statement. If you type a function definition in inter¬active mode, the interpreter prints ellipses (…) to let you know that the definition is not complete.
A function definition specifies the name of a new function and the sequence of statements that executes when the function is called. To end the function, you have to enter an empty line.
>>> def golden lines ( ):
. . . print(“Work is worship.”)
. . . print(“Time and tide waits for
. . .
Python allows us to put a sequence of statements together to create a brand-new command called function. The first line tells Python that you are defining a new function named golden lines( ). The following lines are indented to show that they are part of the golden lines ( ) function. The blank line (obtained by hitting the <Enter> key) lets Python know that the definition is finished and does not cause anything to happen.
Ensure that the statements in a block have the same indentation level by pressing the Spacebar or Tab key. The Python interpreter will throw an error if the indentation level in the block is not the same.
The function is invoked by typing its name.
>>> golden lines ( )
Work is worship.
Time and tide wait for none.
>>> is the default Python prompt of the interactive shell. It is often seen for code examples that can be executed interactively in the interpreter.
… is the default Python prompt of the interactive shell while entering code for an indented code block or within a pair of matching left and right delimiters (i.e., parentheses, square brackets, or curly braces). Working in interactive mode is convenient for beginners and for testing a small piece of code, as you can test them immediately. But for writing more than few lines, you should always use script mode. In script mode, you type the Python program in a file and save our code so that you can modify and reuse the code later and then execute the content from the file.
Quitting Interactive Mode
To exit Python interactive mode, press Ctrl-Z or type quit, as shown below:
>>> quit( )
Script Mode (Integrated Development Learning Environment-IDLE)
IDLE (Integrated Development and Learning Environment) is an integrated development environment (IDE) for Python. The Python installer for Windows contains the IDLE module by default. The interactive mode does not save commands entered by the user. But if you want to save all the com¬mands in the form of a program file and see all output lines together then the script mode of Python is used. A program stored in a file (usually one that will be interpreted) is called a script. Python script mode is basically used for writing programs, i.e., a set of instructions that can be saved for further use.
This file is saved on a disk so that it can be used over and over again. Since Python interactive mode does not save any of your work when you quit, you may want to store your important work in text files. Text files that contain Python code are called scripts (if they are whole programs) or modules (if they con¬tain chunks of code meant to be imported into other programs). Both scripts and modules end with the suffix .py.
- A user can switch to script mode by creating a file from the File -> New option.
To run Python from a Graphical User Interface (GUI) environment, you need a GUI application on your system that supports Python. A special type of program known as a Programming environment is specifically designed to help programmers to write programs and include features, such as automatic indenting, color highlighting, and interactive development. To create and run a Python script, you will use IDLE.
When you write multi-line statements, IDLE does not display the … continuation prompt. IDLE automatically indents lines for you.
IDLE uses colored syntax to highlight your code. By default, built-in functions are purple-, strings are green-, and language keywords (such as like print, if, and else) are orange. Any results produced are in blue. The code you type is colorized to make it easier to distinguish one part of a statement from another.
- The IDLE development environment includes Python interactive mode and more tools for writing and running programs and for keeping track of names.
- The Python code contained in the script is executed by including a file system path (absolute or relative) referring to either a Python file, a directory containing a __main___.py file, or a zip file containing a_main__.py file.