Re: [Tutor] Function question
On 25-03-17 11:17, Alan Gauld via Tutor wrote: method: print(' '.join(anotherFunction(4)) Many thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function question
Many thanks! On 25-03-17 11:17, Alan Gauld via Tutor wrote: On 25/03/17 10:01, Peter O'Doherty wrote: def myFunc(num): for i in range(num): print(i) print(myFunc(4)) 0 1 2 3 None #why None here? Because your function does not have an explicit return value so Python returns its default value - None. So the print() inside the function body prints the 0-3 values then the function terminates and returns the (default) None to your top level print. def myFunc(num): for i in range(num): return i print(myFunc(4)) 0 #why just 0? Because return always returns from the function immediately. So you call the function, it enters the loop, sees the return for the first element and exits. The print() then prints that returned value. The preferred method to do what I think you were expecting is to build a list: def anotherFunction(num): result = [] for i in range(num): result.append(i) return result Which is more concisely written using a list comprehension but that would hide the general point - that you should accumulate results in a collection if you want to return more than a single value. To print the result you would typically use the string.join() method: print(' '.join(anotherFunction(4)) //===== // Peter O'Doherty // http://www.peterodoherty.net // m...@peterodoherty.net //= ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Function question
Hi, Apologies for the very basic question but could anyone explain the behaviour of these two functions (in Python3.5)? def myFunc(num): for i in range(num): print(i) print(myFunc(4)) 0 1 2 3 None #why None here? def myFunc(num): for i in range(num): return i print(myFunc(4)) 0 #why just 0? Many thanks, Peter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Geometric sequence
To the people who kindly replied to my question: many thanks! On 10/31/2013 06:29 PM, Danny Yoo wrote: As an aside: It shouldn't be too bad to write a "generator" for the geometric series, so that we can pick out the terms on-demand. # >>> def geometric(base): ... x = 1 ... while True: ... yield x ... x *= base ... >>> twos = geometric(2) # 'twos' is a representation of the geometric series; we can then pick out the elements one at a time: # >>> twos.next() 1 >>> twos.next() 2 >>> twos.next() 4 >>> twos.next() 8 >>> twos.next() 16 >>> twos.next() 32 >>> twos.next() 64 # ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Geometric sequence
Hi List, I know a geometric sequence can be produced by: series = [2**x for x in range(7)] But I would like to curtail the sequence before the last element excedes a certain value. Is there a better way of doing it that the following: for x in range(20): series_element = 2**x print series_element if series_element > 60: break print series Many thanks, Peter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner's question
Many, many thanks to all those who replied to my question. I hope the next time I post, it will be with something more advanced. Judging by the volume of replies, is it fair to say that this problem was much too advanced for page 16 of an introductory text? Best wishes, Peter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner's question
On 11/22/2012 03:17 PM, Walter Prins wrote: Hi Peter, On 22 November 2012 12:55, Peter O'Doherty <mailto:m...@peterodoherty.net>> wrote: Hi list, Firstly, apologies for the low-level nature of this question - it's really quite basic but I don't seem to be able to solve it. I need to write a program that examines 3 variables x, y, z, and prints the largest odd number. I've tried all sorts of variations and this is the current version: x, y, z = 26, 15, 20 if x > y and x > z and x%2 != 0: print 'x is largest and odd' elif y > x and y > z and y%2 != 0: print 'y is largest and odd' elif z > x and z > y and z%2 != 0: print 'z is largest and odd' else: print 'no odd' The key logical mistake you make is that by your current logic the *smallest* number can never be the largest odd number, which is obviously false as in your example. Break the problem down (divide and conquer). Suppose I gave you only 2 numbers, and you had to say which of the 2 numbers were the largest odd, what would be the possible outcomes and what would the the solution be? (Hint, both can be odd, only x can be odd, only y can be odd, or neither can be odd.) Once you have that answer, then repeat the exact same solution for the first 2 numbers and apply to the answer from x&y and and the remaining z. The result from that is tells you the largest odd number from all 3. (Aside, your question states to print the largest odd number, which I interpret to mean the value, not the name of the variable holding the value. ) Walter Thanks Walter. This code appears to work although it's very cumbersome. Is there a better way to do it? x, y, z = 6, 23, 16 if x%2 != 0 and y%2 !=0: if x > y: ans = x else: ans = y elif x%2 !=0 and y%2 == 0: ans = x else: ans = y if ans%2 != 0 and z%2 !=0: if ans > z: ans = ans else: ans = z elif ans%2 !=0 and z%2 == 0: ans = ans else: ans = z print str(ans) + ' is largest odd' Regards, Peter -- //= -> Peter O'Doherty -> http://www.peterodoherty.net -> m...@peterodoherty.net -> https://joindiaspora.com/people/70716 //= ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner's question
Hi Varun, Thanks for your reply. I agree the problem is logic - but how can one inspect one number using if x%2 == 0 and then compare it to two other numbers which should at the same time be checked for "oddness", just using the basic constructs? Thanks, Peter On 11/22/2012 02:06 PM, Varun Nagpal wrote: Hi Peter, The reason why your program is not behaving as it should is not Pythonic but rather logic. Instead of giving away the code, I would suggest you revisit the 'and' conditions. The program should print the largest odd number i.e. the number which is largest among all odd numbers. Instead you are printing the number which is largest AND odd. -- Varun On Thu, Nov 22, 2012 at 6:25 PM, Peter O'Doherty mailto:m...@peterodoherty.net>> wrote: Hi list, Firstly, apologies for the low-level nature of this question - it's really quite basic but I don't seem to be able to solve it. I need to write a program that examines 3 variables x, y, z, and prints the largest odd number. I've tried all sorts of variations and this is the current version: x, y, z = 26, 15, 20 if x > y and x > z and x%2 != 0: print 'x is largest and odd' elif y > x and y > z and y%2 != 0: print 'y is largest and odd' elif z > x and z > y and z%2 != 0: print 'z is largest and odd' else: print 'no odd' A solution should be possible using only the simple operators and keywords above, no functions, lists or any other form of iteration. (It's from p. 16 of Introduction to Computation and Programming Using Python, and no, it's not "homework"!) Many thanks, Peter ___ Tutor maillist - Tutor@python.org <mailto:Tutor@python.org> To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- //= -> Peter O'Doherty -> http://www.peterodoherty.net -> m...@peterodoherty.net -> https://joindiaspora.com/people/70716 //= ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Beginner's question
Hi list, Firstly, apologies for the low-level nature of this question - it's really quite basic but I don't seem to be able to solve it. I need to write a program that examines 3 variables x, y, z, and prints the largest odd number. I've tried all sorts of variations and this is the current version: x, y, z = 26, 15, 20 if x > y and x > z and x%2 != 0: print 'x is largest and odd' elif y > x and y > z and y%2 != 0: print 'y is largest and odd' elif z > x and z > y and z%2 != 0: print 'z is largest and odd' else: print 'no odd' A solution should be possible using only the simple operators and keywords above, no functions, lists or any other form of iteration. (It's from p. 16 of Introduction to Computation and Programming Using Python, and no, it's not "homework"!) Many thanks, Peter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor