CBSE Class 11: Python Fundamentals 2

PROGRAMMING IN IMMEDIATE/INTERACTIVE MODE

In Python, there are two options/methods for running code:

Immediate / interactive mode
Script mode

Here, we will see
* The difference between the interactive modes and script mode and
* The pros and cons of running scripts in both of these modes.

Immediate / Interactive Mode

Interactive mode provides us with a quick way of running blocks or a single line of Python code. The code executes via the Python shell, which comes with Python installation.
Interactive mode is handy when we want to execute basic Python commands or when we are new to Python programming and we just want to get familiar with this wonderful language.

To access the Python shell, we have to open the terminal of your operating system and then type “python”. On Pressing the enter key and the Python shell will appear.

“>>>” is the prompt of python language. It indicates that the Python shell is ready to execute and send your commands to the Python interpreter. The result is immediately displayed on the Python shell as soon as the Python interpreter interprets the command.

To run our Python statements, we have to just type them and hit the enter key. and we will get the results immediately.

>>> print("Welcome to Python programming")
Welcome to Python programming
>>>
>>> 100
100
>>> print(2*10)
20
>>> print("hello"*5)
hellohellohellohellohello
>>>

We can also run multiple statements on the Python shell. A good example of this is when we need to declare many variables and access them later.
Example:

>>> a=10
>>> b=20
>>> c=30
>>> print("a",a,"b",b,"c",c)
a 10 b 20 c 30
>>>

Another Example:

>>> name="Amit"
>>> salary=45000
>>> print("name ",name," salary ",salary)
name Amit salary 45000
>>>

From the above examples, it is clear that in an immediate mode as we type the command and press enter we get the result.

Example: To display a welcome message in immediate mode

>>> print(“welcome to python”)
Welcome to python

Example: To display the value of a variable in immediate mode

>>>a=10
>>>

Writing the above statement will create a variable named “a” and assign value “10” to it. (in python we are not required to declare a variable first and then use it as in some other languages like c, c++, java etc).

Now to display the value of variable “a” we can write as:

>>>a
10

>>> print(a)
10

>>>print(“a = “,a)
a=10

Example: to calculate and print sum of two numbers in immediate mode
1st method

>>> a=10
>>> b=20
>>> c=a+b
>>> print(a)
10
>>> print(b)
20
>>> print(c)
30
>>> 

Example: to calculate and print sum of two numbers in immediate mode
2nd method

>>> a=10
>>> b=20
>>> c=a+b
>>> print("1st no = ",a)
1st no =  10
>>> print("2nd no = ",b)
2nd no =  20
>>> print("sum = ",c)
sum =  30

Example: to calculate and print square of a number in immediate mode

>>> a=5
>>> b=a*a
>>> print("no = ",a)
no =  5
>>> print("square = ",b)
square =  25
>>> 

Example: to calculate and print cube of a number in immediate mode

>>> a=5
>>> b=a*a*a
>>> print("no = ",a)
no =  5
>>> print("cube = ",b)
square =  125
>>> 

Number Of Visitors

0238260