Is it possible to run a function in Python using the command line?

0 votes
I have recently started learning Python. I want to know whether it is possible to run a function using the command line. Please suggest how to do it.
Jun 26 in Python by Arvind
• 1,530 points
30 views

1 answer to this question.

0 votes

Suppose your file name is demo.py and it contains the following code

def my_func():

     return "Hello, World!"

Now you can run it from command line using following line

$ python -c 'from demo import my_func; print my_func()'
answered Jun 26 by Neel
• 1,610 points

Related Questions In Python

0 votes
1 answer

Is there a foreach function in python and is there a way to implement it if there isnt any

Every occurence of "foreach" I've seen (PHP, ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,710 points
64 views
0 votes
1 answer

is it possible to create zip of a directory in Python?

Please go through this code. This should ...READ MORE

answered Jul 10 in Python by Arvind
• 1,530 points
14 views
0 votes
1 answer

Is it possible to print a string and a variable in a single print statement in Python?

This can be done using simple string ...READ MORE

answered 6 days ago in Python by Neel
• 1,610 points
11 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 133 views
+4 votes
6 answers
0 votes
1 answer

Is it possible to print all the modules imported in a python script?

Since I am using Python 3.6, I ...READ MORE

answered Jul 3 in Python by Neel
• 1,610 points
15 views
+1 vote
1 answer

is it possible to compare a values to a backslash in Python?

The backslash "\" character is used to ...READ MORE

answered Jun 28 in Python by Neel
• 1,610 points
24 views