I am a Computer Science student currently in a debate with my school curriculum regarding how Python execution modes are defined.
My school textbook asks the following multiple-choice question: “Python programs are typed in:”
a) Interactive mode
b) A combination of interactive mode and script mode
c) Script mode
d) All of these
My teacher insists that the answer is strictly Script mode because code typed in Interactive mode is not saved to a .py file. However, option (d) All of these suggests that programs can be written/tested in interactive mode, script mode, or both.
From the perspective of the creators and maintainers of Python: Does code entered into the interactive interpreter count as typing/running a Python program, or is “program” strictly reserved for saved script files?
I took a quick look on Wikipedia and BBC bitesize as backup, and I think a program is the instructions themselves A file is a method of storing those instructions, but a program can exist without file storage.
In answer to your multple choice question, a Python program can be typed-in in interactive mode, or stored and executed from a Python .py file, (Or executed from a shell here document). I would answer any of the above - I will halt the execution of a .py file and continue in interactive mode, especially when debugging or investigating an algorithm. Answer D comes closest.
In many years working on the Rosettacode.org site I found that few languages that had Read Evaluate Print, (REPL), loops allowing for typing instructions at the command line, actually gave task examples that used their REPL abilities.One purpose of the rosettacode site is to show off a languages capabilities and I gave several task implimentations as what would appear when using the REPL, such as this task result that asks for the computation of a large imteger, 5**4**3**2 and information on the result.
Unfortunately - or perhaps fortunately - questions like this are built on a lot of assumptions, like “what even IS a ‘program’ in this context?”. This could be a good thing, if you can use the question to open a discussion. For example, suppose that I launch interactive Python (which for me is such a common thing that I have the global hotkey Ctrl-Alt-P to do it), and retrieve a recent command to re-execute:
I can then go and edit some of those numbers, and determine how many more minutes of production are required in order to fulfil the Space Elevator’s final requirements. The last time I checked, we had 3731 out of 5000 Nuclear Pasta produced, and were making 11.2 per minute, so I can see from my command history how far we’ve come since then. So, is that a program? I mean, if I didn’t have a REPL, I would probably have to write this as a script and run it [1] which implies that this is a program. On the other hand, maybe it’s only a “program” once it’s been saved for reexecution.
And then, suppose that you start writing a script, jump over to the REPL, try something out, and then copy and paste it into your script. Does that mean that you typed the program in interactive mode?
Use this as the start of a ripe old discussion, it should be fun - and productive
but note, even before I’d heard of this language called Python, I was doing the exact same thing with EREXXTry on OS/2 - but that’s a story for another day ↩︎
I’d say this is a bad question and advise you to spend more time & focus on other parts of your course. Apart from that, it isn’t a practical question. In my 1-2 years of hobby-coding Python, this question has never bothered me.
If you’re still willing to spend time on it, I’d actually challenge your teacher’s view that code in interactive mode can’t be called a “program”. I save code snippets like this in files on my computer:
def print_tokens(t):
# some lines of code
...
Sometimes it’s more convenient to use IDLE’s interactive mode than save code into a file then run it. So I copy-paste code from the file to interactive mode, then add
>>> a = ... # Load some tokens into a
>>> print_tokens(a)
What I saved in that file is a program, but what I entered into interactive mode is a program too.[1]
If your teacher further argues that the code in the file here has no output, you can argue with something that has output, e.g. import datetime; print("Time now:", datetime.datetime.now().isoformat())↩︎