How does Python interpret its code? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am currently trying to learn Python. I have learned some Liberty Basic and wanted to know how Python interprets its code. I have noticed with some of my small programs that it runs it in a strange way, and I am not sure how it runs it. In Liberty Basic, it goes from top to bottom. Before I get into any major Python programming, I would like to know how it runs its code. Top to bottom, bottom to top? Any information relating to this would be appreciated.

Python interprets any given code file top-to-bottom. However, there are many ways that code that be interpreted top-to-bottom but not run in exactly that way.
For instance, consider the following small program:
def foo():
print("World")
print("Hello")
foo()
If you were looking at only the ordering of the print() calls in the file, you might expect that the output would be
World
Hello
But, because the first print statement is inside a function definition, it isn't run immediately when the interpreter gets to that line - instead, it's made part of the foo function. Later on, when the foo() call is made, is when it is actually run, and thus the actual output is...
Hello
World

Have you considered reading any book about Python programming? Here's a list of good reads: http://docs.python-guide.org/en/latest/intro/learning/
Python starts executing code line by line from top to bottom, and branches on branching conditions like any other imperative programming language. This shouldn't be too far removed from Basic...

Related

How to a create a random Python program generator? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Don't ask why, but I want to create a Python program that generates a random Python program that is valid for execution. I found a similar thread Searching for a random python program generator , but it's old and all the links except the demonstration http://www.4geeks.de/cgi-bin/webgen.py are dead. And maybe something has changed in these years and it's easier to do such thing?
One of idea is to use quantum collapse algorithm/technique. This would require to make a kind of database, where I describe what symbols can came after previous one. For example after a can come (,, any letter, etc, but never, for example, ). Still I think this will create very gibberish source and I could easily set wrong conditions.
There are two main ways to achieve this:
Use third parties to generate runnable junk code, for instance using yarpgen to generate a valid C program, and then using things to translate that to python like https://github.com/DAN-329/C_to_Python_translator
Write a python code that writes randomly generated functions with random parameters calling eachother, if you just need a bunch of functions calling eachother doing random math operations that should not be that hard to implement.

User-Defined Functions and Learning to Think Like a Computer Scientist with Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am brand new to coding / programming and am starting with the foundations of Python. I've used a few different resources (Codecademy, Automate the Boring Stuff, and most recently, How to Think Like a Computer Scientist by Using Python).
So far my education has been good, albeit a bit slow to start (not used to thinking in computer terms quite yet!). But I've run into a problem with one of the lessons that I cannot replicate in my IDE. Please see the code below:
def printTwice(bruce):
print('bruce')
print('bruce')
The lesson states the output should be 'Bruce, Bruce' which makes sense logically. However, when I go to run the code I get the following:
===RESTART: /Users/owner/Documents/bruce.py======
So essentially just another line to start a new code from. I am writing this code in a new file and just cannot figure out what I'm doing wrong.
Any help is appreciated.
Thanks!
Add printTwice('bruce') on a new line after the function, with no indentation like so:
def printTwice(bruce):
print('bruce')
print('bruce')
printTwice('bruce')
This line will call your printTwice function, passing the value 'bruce' to the variable bruce, which is not used.

Is there a limit to how large a function can be? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
New programmer here!
I'm creating my first script on my own, and I have a particular function that is quite large, as in 50 lines.
I understand that theoretically a function can be as large as you need it to be, but etiquette-wise, where is a good place to stay under?
I'm using Python 2.something if that makes a difference.
A good rule of thumb (and it's more a guideline of thumb really) is that you should be able to view the entire function on one screen.
That makes it a lot easier to see the control flow without having to scroll all over the place in whatever editor you're using.
If you can't understand fully what a function does at first glance, it's probably a good idea to refactor chunks of code so that the more detailed steps are placed in their own, well-named, separate function and just called from this one.
However, it's not a hard-and-fast rule, you'll adapt your approach depending on your level of expertise and how complex the code actually is.

Exit in a function using Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
A quick function question in Python. Is it possible to stop a function from processing and return early. i.e like this:
def _do_something():
if not exists('/etc/', use_sudo=True):
# no files don't do any more
return
# if ok continue with stuff
sudo('/etc/init.d/nginx stop')
Is this ok? Or is there something like 'exit' ?
Your question sounds like " is there a better way to do this?".
no, that looks fine and will work.
For loops you could use the break-statement, but in a function this is good way to "stop" it - returning None implicitly.
Yes, this is a common way of structuring program logic.
Some people would argue that you write your function the other way around, like:
def _do_something():
if exists('/etc/', use_sudo=True):
sudo('/etc/init.d/nginx stop')
because it is shorter and, in a way, more directly represents what you mean by writing the function.
One thing to worry about with the structure that you have in your question is what happens when the function becomes more complicated. It is possible for a large and complicated function with many return statements to actually have unreachable code, where the function is guaranteed to return early and there is code at the end of the function that will not be executed.
def lol():
print "first!"
return
print "unreachable"
In this case, it is trivial to see that "unreachable" will never be printed, but if you have many nested if/else statements, this becomes less obvious. Good IDEs will warn you about unreachable code, but this is something to consider if you choose to program in a simpler text editor.
Side note: Just in case you weren't aware, Python functions that start with one or two underscores often have a "special meaning" attached to them. I'll leave the rest for you to explore.

My Python code return different answers when it runs and when it is in step-by-step debugging [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I try to write some code on Python2.7, which will able to implement a bignum arithmetic, using linear lists. I know, this is useless in Python, but it's my homework in collage. I write some working pieces of code, but problem is in dividing. I'm sure that function works, but when I run code to test it, I just get wrong answer (in some cases). But if I execute code step-by-step, it works correctly.
I'm using linux, but I tested my code on my friend's windows computer, and I got the same problem. I wrote code in Eclipse with PyDev, if it is matter.
My code on Ideone: Code
If lines in console output are the same - output is correct. On Ideone output is incorrect too. But if you put a breakpoint on line 383 and then go in the _simple_div method, answer will be correct
I hope you help me to find a reason of this.
P.S. Sorry for ugly code.
If I run your code, I get
~/coding:$ python divbug2.py
1-1
10
That -1 doesn't look right. Is there a -1 being inserted somewhere in the division? First thing to try is to search for -1 in that function, which gives
i-=1
res._addFirst(i)
if i==-1: i=0
.. and that looks strange, because if i == -1, then you've just added it to res. Maybe we should check first, i.e.
i-=1
if i==-1: i=0
res._addFirst(i)
Swapping those two lines produces
~/coding:$ python divbug2.py
10
10
And then -- after writing a real .copy() method, because copy.deepcopy was really slow and even using PyPy I got bored waiting for things to finish:
>>>> all(int(str(LongNum(x)._simple_div(LongNum(y)))) == x/y for x in range(2000) for y in range(1, 2000))
True
I'm not sure why this was working for you when you did it step-by-step, but I'm a little surprised it worked at all.

Categories