0

I have a Python code as follows, I wrote a function as the same name as the Program name and gave an entry point for the function as follows:

import math
def Problem1():
    total=0
    for i in range(1000):
        if i%3 == 0 or i%5==0 or i%6==0 or i%9==0:
            total=total+i
    return total

Now I went to terminal ran IPython, and then executed the following statements:

import Problem1
Problem1.Problem1()

It printed the output, even though I dont have a print statement, I have given a return statement, so what is happening here?

Another question : How do I run this directly from the command line, ie how to give something equivalent to void main() of C in Python?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
user76170
  • 173
  • 3
  • 10
  • You do not need `import math`. In Python, method names are lower_case_with_underscore and class names are InitialCaps; more tips like these are in the [python style guide](http://legacy.python.org/dev/peps/pep-0008/). – Burhan Khalid Apr 06 '14 at 11:21

1 Answers1

1

What IPython does is it prints the output if you simply call a function or a variable

so if you just enter a variable name and hit enter it will print it. the same thing is happening for a function output. This is standard behavior in an interactive shell.

The idea behind this is that if you do interactive work you usually want to see your output directly.

To run this you would put your code in a *.py file where you actually call the function at the bottom of your file. so you don't need to do void main(). You then execute the code with the Interperter, i.e.

python yourfile.py

however there is a remotely similar pattern to the void main() thingy

Note that because Python is an interpreted language, you can simply put

print "hello world"

in a *.py file call it with the python executable and it right away works. There is no "main()" function that you need to define

Community
  • 1
  • 1
Retozi
  • 7,521
  • 1
  • 23
  • 16
  • So I dont need to put void main similar things at all, any statements below a function will be interpreted line by line? Because Python is an interpreted language and not a compiled language unlike C, so I just put different function definitions and then write statements to call these functions without indentation, so Python runs these line by line? – user76170 Apr 06 '14 at 10:47
  • -1 for `so if you just enter a variable name and hit enter it will print it. the same thing is happening for a function output. This is an Ipython thing and has nothing to do with python itself` - The standard python shell has the exact same behaviour. It will print the result of the evaluated expression, regardless of what the expression is (function call, variable, literals, whatever). So this part of your answer is just plain wrong... – l4mpi Apr 06 '14 at 10:58
  • 1
    It's also worth noting that this behaviour is the same in probably every interpreter for every language since lisp (and probably even earlier). That's why they're called a REPL, it Reads input from the user, Evaluates it, Prints the result and then Loops. – l4mpi Apr 06 '14 at 11:06
  • 1
    @l4mpi: changed, i always use ipython and thought this would not happen in the regular shell. For some reason I had that info made up in my mind. – Retozi Apr 06 '14 at 11:15