Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run python function from command line

Tags:

python

This is my code:

def isPrime(n):
    if n>1:
        for i in range(2, n):
            if (n%i)== 0:
                print(n, "is not a prime number")
                print(i, "*", n//i, "=", n)
                break
        else:
            print(n, "is a prime number")
    else:
        print(n, "is not a prime number")

Earlier I have used idle and Jupyter notebook but now I am trying to run it via command line and I don't know how to enter argument for the function defined. I'm using cmd for the first time for running scripts. This is what it shows:

Command Prompt image

like image 641
user12856241 Avatar asked Oct 20 '25 11:10

user12856241


1 Answers

You can use the flag -c while doing python like this:

python -c 'isPrime(11)'

you can also import this way as well

python -c 'import foo; foo.isPrime(2)'

or do this

python -c 'from foo import isPrime; foo.isPrime(n)'
like image 130
YamiAtem Avatar answered Oct 22 '25 02:10

YamiAtem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!