Re: [Tutor] formatting strings
On Fri, May 08, 2015 at 05:11:49PM -0700, Danny Yoo wrote: > Also, you can write a loop that goes from 1 to N by using range(). For > example: > > > for n in range(1, N+1): > print(n, 2*n) > > > The while loop that you have does work, but the for loop here is more > idiomatic in expressing the idea of "Do the following for this > collection of values ..." Why do so many beginners turn to while loops when they want to iterate over a fixed sequence? I know that Learn Python The Hard Way teaches while loops first. I think that is a terrible idea. That's like going to a cookery class where for the first three weeks they teach you to chop vegetables with a spoon, and only in the fourth week say "Guess what? There's an easier way! Introducing the knife" While-loops should be taught after for-loops. The fact that from a computer-science theoretical perspective while-loops are more fundamental is irrelevant. We don't teach people bitwise operators before teaching them arithmetic. Loops are no different. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] formatting strings
On Thu, May 07, 2015 at 06:57:30PM +, Tudor, Bogdan - tudby001 wrote: > Hi, > > This is my first time. > I am using python 3.4.3 on windows 7 64bit. > > I am trying to make a binary counter that will prompt for and read a > decimal number (whole number). Then display all decimal numbers > starting from 1 up to and including the decimal number entered along > with the binary representation of the numbers to the screen. Start by handling a single number: py> def display_number(n): ... print(n, bin(n)) ... py> display_number(15) 15 0b Now do a loop, displaying each number: py> for i in range(1, 11): ... display_number(i) ... 1 0b1 2 0b10 3 0b11 4 0b100 5 0b101 6 0b110 7 0b111 8 0b1000 9 0b1001 10 0b1010 Does that help? -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] formatting strings
> I am trying to make a binary counter that will prompt for and read a decimal > number (whole number). Then display all decimal numbers starting from 1 up to > and including the decimal number entered along with the binary representation > of the numbers to the screen. You might consider writing a separate function toBinary() that takes as input a number 'n' and returns its binary representation as a string. You're copying and pasting, and that's a sign that you've got a block of code that you can *reuse*. If you don't know how to write functions, please ask! Also, you can write a loop that goes from 1 to N by using range(). For example: for n in range(1, N+1): print(n, 2*n) The while loop that you have does work, but the for loop here is more idiomatic in expressing the idea of "Do the following for this collection of values ..." ... Reading the code... Ah. You have a fixed number of variables to capture values such as next_num_1, binary_num_1, next_num_2, binary_num_2, and so on. But this means you'll only be able to handle a fixed number of values, where by "fixed", it looks like you've gone up to four. :P As you're noticing, this approach with capturing results with a fixed number of variables isn't going to work well when we don't know how many times we're walking through the loop. Do you know about lists? They allow you to hold onto a variable-sized collection of values. For example, let's say that we want to produce the output: ### 1 2 2 4 3 6 4 8 5 10 ... ### Basically, our 2-times table. Here's how we can do this. # ## pseudocode inputs = [] outputs = [] for x in range(10): inputs.append(x) outputs.append(x * 2) # Then we can get at any particular input/output by indexing the list at the same position. For example, we can print the inputs and outputs like this: # inputs = [] outputs = [] for x in range(10): inputs.append(x) outputs.append(x * 2) for (i, o) in zip(inputs, outputs): print (i,o) # and this takes an approach similar to what you've got, but it works because it can hold onto all the results in a variable-sized list. But that being said, for your particular program, you might not even need to hold onto the entire collection of inputs and outputs at once. Can you just do something like this instead? ### for x in range(10): doubled = x * 2 print(x, doubled) ### where we interleave computation with output within the loop itself? This has the advantage that we don't need to hold onto anything but the very last thing we just computed, so it reduces the number of things we're juggling to just the range that we're walking over, the current value that we're walking, and the output from that current value. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pointer puzzlement
On 05/08/2015 06:26 PM, Alan Gauld wrote: On 08/05/15 19:10, Jim Mooney Py3.4.3winXP wrote: On 7 May 2015 at 18:42, Dave Angel wrote: Python doesn't have pointers So what is the difference between a python name and a pointer? OK, This could get deepo. Lets start with the supoerficial... A pointer (in most languiages) is a named alias for a memory address. That is a name that is a specidic memory location. At least in C and some other languages. That means you can, in C , declare a variable (say p) to be a pointer to some array of objects and it is simply the memory address of the first object in the array. The second objects address is therefore p+sizeof(o) where 'o' is the object type of the array. You can in fact write code like this in C (and frequently do): int p*; // declare p to be a pointer to an integer int ia[] = {1,2,3,4,5,6,7,8,9,0}; // an array of 10 integers p = ia; # p holds the address in ia, ie its first element This is a shorthand for p = & (ia[0]) printf("%d\n", p); // print the object, ie ia[0] print("%d\n", *p) // print the first object ia[0] print(("%d\n", p[0]); // print the first object, ie ia[0] printf("%d",p+3); // print the 4th object ie ia[3] printf("%d",*(p+3)); // print the 4th object ie ia[3] printf("%d",p[3]); // print the 4th object ie ia[3] and p[274] is a random pile of bits which might or might not happen to be readable. Using it might read some other int, it might read a few bytes of code, it might cause a segmentation fault. So p is a pointer to an integer. And an array is a sequence of integers in memory so you can access subsequent memory locations by adding numbers to p. (The compiler multiplies the number by the size of the type of p to get the actual memory address.) And all this is part of the language specification. C is after all, an overblown assembly language, and all processors are expected to emulate the appropriate Dec machine. In other languages (eg Pascal) pointers are slightly more abstract but not much. They are more like C++ references than memory addresses, but the end result is much the same: they are very tightly tied to the physical concepts of memory versus variables. Now in contrast... In Python a name is a much more abstract concept and is just a label that is attached to an object. How the label gets attached is an entirely abstract and implementation specific concept. In practice its usually via a dictionary so that a variable is a name which is a key of a dictionary. The corresponding value is an object or, (crucially) maybe a pointer to an object. But the name is not the pointer it's the corresponding value that (may be) a pointer. So this Python code (compare to the C above) makes no sense: aList = [1,2,3,4,5,6,7,8,9,0] print aList_+ 3 aList is not a pointer to the start of a sequence of objects in memory. aList is a key to a dictionary that holds a reference to a list of objects. And adding 3 to a key in a dictionary is not a sensible operation. I'd maintain that this is still more than what the language guarantees. The model I have of the language is that a name is a key in some namespace (but not necessarily a dict). The value that's associated with that key is an abstraction that the interpreter knows how to decode to identify one particular object. It might be a pointer, it might be an integer, it might be a pair of values. The object might have a fixed location, or it might move around. As long as at any moment that the code is running, the interpreter can find the object, it doesn't matter. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pointer puzzlement
On 08/05/15 19:10, Jim Mooney Py3.4.3winXP wrote: On 7 May 2015 at 18:42, Dave Angel wrote: Python doesn't have pointers So what is the difference between a python name and a pointer? OK, This could get deepo. Lets start with the supoerficial... A pointer (in most languiages) is a named alias for a memory address. That is a name that is a specidic memory location. At least in C and some other languages. That means you can, in C , declare a variable (say p) to be a pointer to some array of objects and it is simply the memory address of the first object in the array. The second objects address is therefore p+sizeof(o) where 'o' is the object type of the array. You can in fact write code like this in C (and frequently do): int p*; // declare p to be a pointer to an integer int ia[] = {1,2,3,4,5,6,7,8,9,0}; // an array of 10 integers p = ia; # p holds the address in ia, ie its first element printf("%d\n", p); // print the object, ie ia[0] printf("%d",p+3); // print the 4th object ie ia[3] So p is a pointer to an integer. And an array is a sequence of integers in memory so you can access subsequent memory locations by adding numbers to p. (The compiler multiplies the number by the size of the type of p to get the actual memory address.) In other languages (eg Pascal) pointers are slightly more abstract but not much. They are more like C++ references than memory addresses, but the end result is much the same: they are very tightly tied to the physical concepts of memory versus variables. Now in contrast... In Python a name is a much more abstract concept and is just a label that is attached to an object. How the label gets attached is an entirely abstract and implementation specific concept. In practice its usually via a dictionary so that a variable is a name which is a key of a dictionary. The corresponding value is an object or, (crucially) maybe a pointer to an object. But the name is not the pointer it's the corresponding value that (may be) a pointer. So this Python code (compare to the C above) makes no sense: aList = [1,2,3,4,5,6,7,8,9,0] print aList_+ 3 aList is not a pointer to the start of a sequence of objects in memory. aList is a key to a dictionary that holds a reference to a list of objects. And adding 3 to a key in a dictionary is not a sensible operation. PS. I'm writing this at 11:30pm and I've just drunk a full bottle of (very good!) red wine all by myself (how sad!). If it makes no sense, my apologies, I'll review it in the morning!!! :-( HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] formatting strings
On 05/07/2015 02:57 PM, Tudor, Bogdan - tudby001 wrote: Hi, This is my first time. First time doing what? Presumably the first time on this forum. But what is your history of using Python, or of programming in general? I am using python 3.4.3 on windows 7 64bit. I am trying to make a binary counter that will prompt for and read a decimal number (whole number). Then display all decimal numbers starting from 1 up to and including the decimal number entered along with the binary representation of the numbers to the screen. I expected nesting a bunch of loops over and over again would work, what I've realized is that there must be an easier way of coding this without repeating loops, I just cannot for the life of me work this out. What I'm doing at the moment is writing way to much code over and over again. https://github.com/rks1337/binary-counting/blob/master/binary%20counting Sorry I'm not sure how to use inline quoting on outlook online. I can't answer for outlook, but normally you add stuff to your message by using copy/paste. On Windows, that's Ctrl-C, Ctrl-V I can't comment on the specifics of your code, since it's online rather than in your message. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pointer puzzlement
On 05/08/2015 02:10 PM, Jim Mooney Py3.4.3winXP wrote: On 7 May 2015 at 18:42, Dave Angel wrote: Python doesn't have pointers So what is the difference between a python name and a pointer? I'm a bit fuzzy on that. What's the difference between a painting of Obama and a living state Senator from New York? Pointers are not part of Python. Python is a language specification. Some implementations may use pointers to implement the binding, but that's irrelevant unless you're doing something like linking C code to such an implementation. Some implementations may run on Motorola processors, but that's not part of the language either. Other implementation do not use pointers for the binding. A name can be reached from a namespace, and it in turn is bound to an object. That binding is a one-way connection that lets the interpreter find the object, given the namespace and the name. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] formatting strings
Hi, This is my first time. I am using python 3.4.3 on windows 7 64bit. I am trying to make a binary counter that will prompt for and read a decimal number (whole number). Then display all decimal numbers starting from 1 up to and including the decimal number entered along with the binary representation of the numbers to the screen. I expected nesting a bunch of loops over and over again would work, what I've realized is that there must be an easier way of coding this without repeating loops, I just cannot for the life of me work this out. What I'm doing at the moment is writing way to much code over and over again. https://github.com/rks1337/binary-counting/blob/master/binary%20counting Sorry I'm not sure how to use inline quoting on outlook online. I will greatly appreciate any help! Kind Regards. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pointer puzzlement
On 7 May 2015 at 18:42, Dave Angel wrote: > Python doesn't have pointers So what is the difference between a python name and a pointer? I'm a bit fuzzy on that. -- Jim "What a rotten, failed experiment. I'll start over. Maybe dogs instead of monkeys this time." --God ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] introspection
On 05/08/2015 02:26 AM, Alex Kleider wrote: On 2015-05-07 20:45, Dave Angel wrote: You also only showed it working on module globals. (For code at top-level, locals() returns the same as globals() ) You could also try it inside functions, where locals() really makes sense as a name. And you could try it in a nested function where there may very well be non-locals (eg. closure items) that aren't local or global. You've taken me to new territory: http://www.shutupandship.com/2012/01/python-closures-explained.html I wasn't familiar with 'closures' and to be truthful, still am not, although thanks to you I am at least aware of the idea. But more interestingly, you could try it on items of a list, or members of a dictionary, where there's no name at all associated with the object. It simply returns None. I assume that's the point you are making? (That it has no name, or perhaps more accurately expressed: there is no name to discover.) That's right. But depending on why you're looking, it might be worth doing some more subtle searching. For example, you're currently looking at all the members of a dict. But if one of those members is of type dict, list, or tuple, you could look at its members. If you do that recursively, you could identify a lot more objects. Also, since you have to pass the object into the search function, you already have one known reference. That one might not be the interesting one, as it might have been made just for this testing. So it would be useful if this function produced a list of *all* the references it can find. And you could have your function work from an id() value. Given an integer, try to find names or expressions that yield this id value. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] my membership and access to the Tutor list
On 08/05/15 09:09, Stewart Lawton wrote: Hi Alan Thank you very much for your response to my Tutor@python.org question. I thought my membership was complete and that I could log in to answer your comments. The tutor list is a mailing list not a web forum. You don't login to answer comments you send an email reply. Use Reply to send to the individual (as you've just done with me) or, more usually, use ReplyAll (or ReplyList if your mail tool has that feature) to reply to everyone on the list. Use plain text to preserve code layout and use interleaved posting (as I'm doing here) rather than top-posting. I found I could not login again. PLEASE can you help to get my password reset? Only you can change the password, its purely web based. I only approve messages in the moderation queue, virtually nothing else. But the password just gives you access to your admin settings. I think I am failing to understand what user and or group permissions are required between apache python, and the python myUnix2.cgi program I am using. OK, I'm no expert here but several things about your program have me puzzled. First remember that the web server will have its own user account and thus your code is effectively being run by another user. So any permissions on your files need to allow that user to have access. This is obviously a security risk and the reason its best not to have web programs accessing files in a users area but to copy any files needed into the web server space. This program script is listed below, hopefully with spaces corrected Spacing is now legal, but you should increase the indentation to make it more readable. Consider 2 spaces as the absolute minimum, most people use 3 or 4. If you ever submit code to the Python standard library it must use 4 spaces. One space makes the indentation hard to line up and almost defeats the point of having it. path to uds_socket corrected as Felix Dietricl suggested may be and Issue. 1) From my user directory I issued the script Unix2.cgi to a listening Unix sockets server and this worked OK. 2) the permissions of Unix2.cgi are:- -rwxrwxrwx. 1 johnlawton johnlawton 987 May 7 17:55 myUnix2.cgi This is not good from security but surely proves the script can execute if permissions are not considered. 3)This file is copied to the apache cgi directory /var/www/cgi-bin with the permissions forced as -rwxrwxrwx. 1 johnlawton johnlawton 987 May 7 18:19 ../../../var/www/cgi-bin/myUnix2.cgi 4) Execution of the cgi script directly works OK. OK, Permissions of the cgi script are not critical they just need to be executable to the web server. So you could have ---r-xrwx and it should be more secure and work OK. What is important is that you change ownership to whatever the apache user account is (local config, I can't help there you'll need to look at the files). 5) http is enabled in the fedora firewall 6)The apache server is started using sudo systemctl start httpd.service. When firefox is used to have Unix2.cgi executed the localhost receives the following error report. Traceback (most recent call last): File "/var/www/cgi-bin/myUnix2.cgi", line 37, in creSockettoServer() File "/var/www/cgi-bin/myUnix2.cgi", line 26, in creSockettoServer sys.exit(1) SystemExit: 1 7) The copy process of myUnix2.cgi from my user directory to /var/www/cgi-bin but setting user and group to root with full permissions results in -rwxrwxrwx. 1 root root 987 May 7 18:45 ../../../var/www/cgi-bin/myUnix2.cgi OK, But I sincerely hope the web server is NOT running as root, that would be a security disaster and a crackers paradise! 8)When firefox is used to have Unix2.cgi executed the localhost receives the same error report given under 6). 9) summary since the 'o' permissions are forced to rwx the script should execute no matter what use group are specified? 10) How do I establish neccessary cgi permissions? The problems are not with your script but with the socket you are trying to create, or the path to it. Its those permissions that likely need to be changed. #!/usr/bin/env python import cgi import socket import sys def htmlTop(): print("""Content-type:text/html\n\n MyServer Template """) def htmlTail(): print(""" """ ) def creSockettoServer(): sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) server_address = '/home/johnlawton/workspace/myUnixSock/uds_socket' I confess I've never used a socket like this, indeed I was only vaguely aware of their existence! I assume you have previous experience of using UNIX domain sockets (in C?) since there is relatively little tutorial help out there. I've always used sockets for IP and given an IP address to the socket. So I can only guess what's going on in your case. Can I ask what you are trying to do in your program that you need UNIX sockets? Just curious. Also one thing that occurs to me - have you made sure the socket file is
Re: [Tutor] Tip (thank) your mentors
Hi, True that. Appreciate everyone who is offering a helping hand for the newbies like me. Best Regards, Sreenath On Fri, May 8, 2015 at 8:08 AM, Ben Finney wrote: > Howdy all, > > I just received a private message that, briefly and simply, thanked me > for advice I gave in this forum, explaining specifically how it helped > the recipient understand a concept better. > > It made my day; I have a spring in my step now. > > Everyone, never hesitate to thank someone – privately if you wish – for > help they've given you. It is inexpensive to give, and very dear to > receive. > > Most of us, most of the time, are engaging in these public discussions > to help the community. Gratitude sincerely expressed is an important and > valuable way to motivate us to keep contributing. > > Thank you for keeping our community welcoming and happy. > > -- > \ “Oh, I realize it's a penny here and a penny there, but look at | > `\ me: I've worked myself up from nothing to a state of extreme | > _o__) poverty.” —Groucho Marx | > Ben Finney > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Best Regards, Sreenath ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor