[Tutor] Abs
I want to be able to ask a user to input an integer and print out the root and power of the given integer. Why do you use abs(x) for this program? I don't understand or see the link between abs() and root and powers. This reminds me of this: By knowing that when x%2==1 x is an odd number and when x%2 ==0 x is even, I was able to create a program that asked the user to enter 10 integers and printed out the largest odd number . So If I understand how and why abs() is used to find the cube root of a perfect cube or how to use abs() to make the computer print out the root and power of a given integer I may make this program. Thank you and forgive for my fuzzy thoughts. Job ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Abs
On 27/07/15 02:06, Job wrote: I want to be able to ask a user to input an integer and print out the root and power of the given integer. Why do you use abs(x) for this program? You don't need to, and I'm not sure why you think you do? I assume it says something about it in your assignment description? abs() converts a number to a positive so you could use it as a guard to prevent you from taking the square root of a negative number. So If I understand how and why abs() is used to find > the cube root of a perfect cube In Python 2 the pow() function (and ** operator) refuse to take fractional powers of negative numbers. So even though you should be able to do: >>> pow (-8,0.3) you can't. You need to convert the number to positive which is where abs comes in. Note: In Python v3 pow() function (and the ** operator) happily take negative numbers. But it will tell you the root of a negative number as a complex number result. And abs() will convert a complex number into its magnitude so that's a second option. if using Python 3. In both cases you will need to manage the sign of the final answer yourself since the use of abs() will always convert things to a positive. 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] Abs
On 27/07/15 11:06, Job wrote: I want to be able to ask a user to input an integer and print out the root and power of the given integer. Why do you use abs(x) for this program? I don't understand or see the link between abs() and root and powers. This reminds me of this: By knowing that when x%2==1 x is an odd number and when x%2 ==0 x is even, I was able to create a program that asked the user to enter 10 integers and printed out the largest odd number . So If I understand how and why abs() is used to find the cube root of a perfect cube or how to use abs() to make the computer print out the root and power of a given integer I may make this program. Thank you and forgive for my fuzzy thoughts. Job ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor You will fine that any attempt to find the root of a negative value will result in an error. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Shading Between Curves with Different Colour Over Specified X value Range
*Goal:* Shade between I_2 (curve 1) and I_3 (curve 2) with following conditions: - Green for 0 < x < 4 - Red for 4 < x < 12 *Code: * *Note: Code currently only attempting to shade green for 0 < x < 4 * import numpy as np import pylab from pylab import * import matplotlib.pyplot as plt import csv # Load data from .txt file with open('current_mirror_output_swing.csv', 'rb') as f: reader = csv.reader(f) your_list = list(reader) data = np.asarray(your_list) I_ref = np.asarray(data[1:,0]) I_1 = data[1:,1] I_2 = data[1:,2] I_3 = data[1:,3] # Create an array of x values to fill b/w curves with a certain color. X1 = np.linspace(0.,4.,len(I_3)) I_ref = I_ref.astype(float)*1000. I_1 = I_1.astype(float)*1000. I_2 = I_2.astype(float)*1000. I_3 = I_3.astype(float)*1000. # Plotting commands. plot(I_ref, I_2, 'r-') plot(I_ref, I_3, 'b-') title('Current Mirror Output Swing') xlabel('$I_{ref}$ (mA)') ylabel('I (mA)') plt.fill_between(X1, I_2, I_3, color = 'g', alpha = '0.5') plt.legend(['$I_{ref}$', '$I_{out}$'], loc='upper left') plt.grid() show() *Issue: * See attached figure. Thank you. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shading Between Curves with Different Colour Over Specified X value Range
On 27/07/2015 19:47, Colin Ross wrote: *Goal:* Shade between I_2 (curve 1) and I_3 (curve 2) with following conditions: - Green for 0 < x < 4 - Red for 4 < x < 12 *Code: * *Note: Code currently only attempting to shade green for 0 < x < 4 * import numpy as np import pylab from pylab import * import matplotlib.pyplot as plt import csv # Load data from .txt file with open('current_mirror_output_swing.csv', 'rb') as f: reader = csv.reader(f) your_list = list(reader) data = np.asarray(your_list) I_ref = np.asarray(data[1:,0]) I_1 = data[1:,1] I_2 = data[1:,2] I_3 = data[1:,3] # Create an array of x values to fill b/w curves with a certain color. X1 = np.linspace(0.,4.,len(I_3)) I_ref = I_ref.astype(float)*1000. I_1 = I_1.astype(float)*1000. I_2 = I_2.astype(float)*1000. I_3 = I_3.astype(float)*1000. # Plotting commands. plot(I_ref, I_2, 'r-') plot(I_ref, I_3, 'b-') title('Current Mirror Output Swing') xlabel('$I_{ref}$ (mA)') ylabel('I (mA)') plt.fill_between(X1, I_2, I_3, color = 'g', alpha = '0.5') plt.legend(['$I_{ref}$', '$I_{out}$'], loc='upper left') plt.grid() show() *Issue: * See attached figure. Thank you. There is no attachment to see, sorry :( One thing to note about the following lines. from pylab import * import matplotlib.pyplot as plt The first was designed to make matplotlib easy to use interactively, especially in iPython, the second in a script. IIRC the former is deprecated so I suggest you stick with the latter. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] _ vs. _name vs. __name vs. name_ vs. __name__ usages
On 25Jul2015 16:08, boB Stepp wrote: After having a long discussion with my wife on her user requirements, I am convinced that an OO approach is required. Which is just as well as that has been one of my next areas of learning to do. I am currently reading "Python 3 Object Oriented Programming" by Dusty Phillips, which so far seems to be a reasonably good text. This has led me to the subject line topics. From my understandings to date: Broadly everything you have said is correct. 1) A single underscore is used conventionally for a "throw-away" variable, such as a loop index for which the index value is not actually used in a subsequent calculation. Yes, but there are some problems. Some don't like it, and it can conflict with other uses. I use it very rarely, almost only ever in very simple list comprehensions, such as this real example: self.hashlist = [None for _ in range(maxchunks)] which primes a list of None values. Using it in a wider context like a loop has the same issues as any other "trite" variable name: that you will reuse it later and forget that it is either already in play (eg nested loops) or already initialised (eg code later break because of some presumption it is not set up). It is almost always better, if only for readability, to pick a sensible name, like "i", even if it is not used. A short name like "i" pretty much says "this is used only here, because otherwise I would have used a longer name that has more context". Of course, the flip side of that is that "_" _is_ the name that says "not used elsewhere", and thus if you use it in in an expression t should scream "mistake" at you. So this: [ (i,) for i in range(10) ] to make a list of tuples (made up example) should not be used with "_" instead, as "i" does get used. So yes, correct, but many people use it very sparingly and some avoid it altogether. 2) _name is used inside class methods to indicate that the programmer's intention is that this name should only be accessed internally by that particular class. Other supposedly "adult" Python programmers will attempt to respect this original intent if they use my code. Or by cooperating classes. But only _closely_ cooperating classes that have knowledge of your first class' internals, usually because they muck with them directly. Basicly, a _name is held to be part of the private API, and subject to change. So code which is prepared to be broken by changes might use it, such as if you have a pair of classes which both muck _directly_ with some shared data structure. Note than in pure OO one class never mucks with another class' internals; they only ever communicate via methods. 3) __name invokes Python's name mangling mechanism. The intent of this usage is to not allow subclasses of the class containing __name to access this name, and to add additional emphasis to future users of my code that this name is meant to be strictly hands-off. Yeah. I have pretty much ceased using this. It causes pain and doesn't fulling solve the problems. The usual use case is subclassing: class Shape(object): def __init__(name): self.name = name self.__count = 0 def inc(self): self.__count += 1 class Cube(Shape): def __init__(name): Shape.__init__(name) self.__count = 6 def add_face(self): self.__count += 1 Internally there two classes work on "__Shape_count" and "__Cube_count", and thus avoid stepping on each others' toes, because although each wants a counter with a nice simple name, they mean very different things with it. So you Cube class has two separate counters. But the names are arguably uglier and harder to type. Also, consider this: from my_geometry_module import Shape as UrShape class Shape(UrShape): def __init__(self, name): UrShape.__init__(name) self.__count = 5 def bfmi(self): self.__count -= 1 class Pyramid(Shape): ... Here, your local "Shape" class will _also_ use "__Shape_count", and _will_ have trouble. So this system is not totally robust. While the example above is contrived, a deeper nesting of classes might easily have the same issue because the final class may not be aware that a class of the same name is already in use higher up the chain. 4) name_ is used when one is "forced" to use one of Python's reserved words as a name. Yes, quite command. Also for predefined names as well as reserved names. My commonest use is the name "type", which is no a reserved word (i.e. special in the language grammar, like "if") but predefined as the "what is the type of this object" function. I've got a few objects floating around where a ".type" attribute is a natural name to indicate their flavour. That works just fine. But when I want to access the object, "type" is also a natural name for the parameter: def check_type(self, type): return self.type == type While that works, it means you have lost access to the predefined