C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Python

Executing first program on Python in Different Modes

Learn: In this article, we will learn how to execute our first program in Python in different modes?
Submitted by Abhishek Jain, on August 05, 2017

1) Interactive Mode

For working in the interactive mode, we will start Python on our computer. We type Python expression / statement / command after the prompt (>>>) and Python immediately responds with the output of it.

Executing first program on Python

1) Let’s start with typing print “Hello Python!” after the prompt.

>>>print("Hello Python!")
Output: Hello Python!
python program 1

2) We may try the following and check the response:

a) 5+7
b) 4*10
c) 7/8
d) 9%2
e) 6//4

python program 2

3) We can also write as:

>>> x=1
>>> y=5
>>> z = x+y
>>> print (z)
Output: 6
python program 3

4) Other operation:

>>> a=3
>>>a+1, a-1
Output: (4,2)  #result is tuple of 2 values
python program 4

Here,"#result is tuple of 2 values" is a comment statement. We will talk about it later.

Note: While writing in Python, remember Python is case sensitive. That means x & X are different in Python.

2) Script Mode

In script mode, we type Python program in a file and then use the interpreter to execute the content from the file. Working in interactive mode is convenient for beginners and for testing small pieces of code, as we can test them immediately. But for coding more than few lines, we should always save our code so that we may modify and reuse the code.

Python, in interactive mode, is good enough to learn experiment or explore, but its only drawback is that we cannot save the statements for further use and we have to retype all the statements to re-run them.

To create and run a Python script, we will use following steps in IDLE, if the script mode is not made available by default with IDLE environment.

  1. File>New File (for creating a new script file) or Press ctrl+N.
  2. Write the Python code as function i.e. script.
  3. Save it (^S).
  4. Execute it in interactive mode- by using RUN option (^F5).

If we write Example 1 in script mode, it will be written in the following way:

Step 1: File> New File

Step 2: Write:

def test():
x=2
y=6
z = x+y
print (z)
python program 4

Step 3: Save or File > Save As - option for saving the file (By convention all Python program files have names which end with .py).

Step 4: For execution, press ^F5 or press Run>Run Module, and we will go to Python prompt (in other window).

>>>test()
Output: 8
python program 4






Sponsored Links






COMMENTS