Python- How to define function on command line

In normal cases we use Python CLI as just test for one liner commands and its output but there may be some cases where we want to test function call in Python at CLI level only just for our understanding or any tutorial purpose. In such cases we can use Pyhon/Pyhon3 CLI: itself.

To do so we normally define a function and write all the needed step one line at a time. And once a function is over add two enter line and now our command line function is ready to use. See the example

$ python3
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Define a funtion at cli only")
Define a funtion at cli only
>>> def add_two_number(num1, num2):
...     sum = num1 + num2
...     return sum
>>> print(add_two_number(3, 4))

Catch is till the time we are writing in each subsequent line, statement belongs to function and when we press enter twice, our function is ready to invoke via CLI.

Leave a Reply

Your email address will not be published. Required fields are marked *.

You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>