Re: [Tutor] Help with 'if' statement and the concept of None
marat murad via Tutor writes: > The author introduced a new way of coding the Boolean NOT operator > with the 'if' statement I have highlighted the relevant > area,apparently this if statement actually means if money != 0,which I > understood Not quite. The statement actually means “if ‘money’ evaluates as true”. Any Python value can be interrogated with the question “Are you true or false?”, and that is what the ‘if’ statement does. This is different from asking ”Is this the True constant or the False constant?” because for most values the answer is ”Neither”. In Python the question “Is this value true, or false?” is usually implemented as ”Is this value something, or nothing?”. If the value is conceptually nothing, it evaluates as false when interrogated in a boolean context. See the Python documentation for a comprehensive list of false values https://docs.python.org/3/library/stdtypes.html#truth-value-testing>; any not-false value is true by default. If you learn the conceptual “if it's not something, it's false”, then you will have a fairly good intuition for how ‘if somevalue’ works. -- \“None can love freedom heartily, but good men; the rest love | `\ not freedom, but license.” —John Milton | _o__) | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with 'if' statement and the concept of None
On Tue, May 31, 2016 at 04:16:21PM +0100, marat murad via Tutor wrote: > money = int(input("How many dollars do you slip the Maitr D'? ")) > *if money:* > print("Ah I think I can make something work") > else: > print("Please sit ,it may be a while") All values in Python can be used where a true or false boolean value is expected, such as in an "if" or a "while" statement. We call this a "boolean context", meaning something which expects a true or false flag. So we have True and False (with the initial capital letter) as special constants, but we also can treat every other value as if they were true or false (without the initial capital). Sometimes we call them "truthy" and "falsey", or "true-like" and "false-like" values. For Python built-ins, we have: Falsey (false-like) values: - zero: 0, 0.0, 0j, Fraction(0), etc. - empty strings: "", b"" - empty containers: [], (), {}, set() etc. (empty list, empty tuple, empty dict, empty set) - sequences with len() == 0 - None - False Truthy (true-like) values: - non-zero numbers: 1, 2.0, 3j, Fraction(4, 5), etc. - non-empty strings: "Hello world!", b"\x0" (yes, even the null-byte is non-empty, since it has length 1) - non-empty containers - sequences with len() != 0 - classes, object() - True We say that "false values represent nothing", like zero, empty strings, None, and empty containers; while "true values represent something", that is, anything which is not nothing. So any time you have a value, say, x, we can test to see if x is a truthy or falsey value: values = [1, 5, 0, -2.0, 3.7, None, object(), (1, 2), [], True] for x in values: if x: print(x, "is a truthy value") else: print(x, "is a falsey value") -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python OLS help
On 31/05/16 16:30, Vadim Katsemba wrote: > Hello there, I am having trouble running the Ordinary Least Squares (OLS) > regression on Spyder. I had no idea what Spyder was but a Google search says its an IDE somewhat akin to matlab or IPython... It also has a discussion group: http://groups.google.com/group/spyderlib You may find someone on this list who knows it but you will likely get a better response on the spyder forum. This list is really for core python language questions and although we try to be helpful on other matters a dedicated forum is usually better. -- 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] Help with 'if' statement and the concept of None
On 31/05/16 16:16, marat murad via Tutor wrote: > program whose code I have pasted below. The author introduced a new way of > coding the Boolean NOT operator with the 'if' statement I have highlighted > the relevant area,apparently this if statement actually means if money != > 0,which I understood, Boolean values are either true or false. Other values are given boolean equivalent values by Python. eg. For strings an empty string is False, anything else is True. For numbers 0 is False anything else is True. So in your example: > *if money:* > print("Ah I think I can make something work") > else: > print("Please sit ,it may be a while") > The first 'if' test is saying 'if money is equivalent to True' (anything other than zero) In effect that's the same as you said (it's like testing for != 0) but the important difference is that it is relying on the boolean *equivalence* of an integer value. The same is true in your second example: > idea of slicing also has a if statement similar to the first one,except the > second one accepts 0 value but the first one doesn't. > > start=input("\nStart: ") > >* if start:* > start=int(start) Again this is really saying if start is True and for a string (which is what input() returns), as I said above, True means not empty. So the string '0' is not empty and therefore True. It's not the value of the character that matters it's the fact that there is a character there at all. All objects in Python have these boolean equivalent values, for example an empty list, tuple,set or dictionary is also considered False. As is the special value None. > I hope i made sense. Yes, although your subject also mentions the concept of None? Did you have another question about that? 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
[Tutor] Python OLS help
Hello there, I am having trouble running the Ordinary Least Squares (OLS) regression on Spyder. I typed in lm = smf.ols(formula='LATITUDE~DIAMETER',data=dataf).fit(), and I ended up getting this error: ValueError: For numerical factors, num_columns must be an int. How am I supposed to run the OLS, is there another module I need to use? Any help would be appreciated. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Help with 'if' statement and the concept of None
Hi I'm learning how to code with python an I have purchased the book 'Python Programming for the absolute beginner,third edition' by Michael Dawson. There is one concept that is confusing me in chapter 3 page 71 there is a program whose code I have pasted below. The author introduced a new way of coding the Boolean NOT operator with the 'if' statement I have highlighted the relevant area,apparently this if statement actually means if money != 0,which I understood,but the program below this one which introduces the idea of slicing also has a if statement similar to the first one,except the second one accepts 0 value but the first one doesn't. print("Welcome to the Chateau D'food") print("It seems we are quit full today.\n") money = int(input("How many dollars do you slip the Maitr D'? ")) *if money:* print("Ah I think I can make something work") else: print("Please sit ,it may be a while") input("\nPress enter to exit") The slicing program word = "existential" print("Enter the beginning and ending index for the word existential") print("press he enter key at 'Start' to exit") start= None while start != "": start=input("\nStart: ") * if start:* start=int(start) finish=int(input("Finish: ")) print ("word[",start, ":",finish, "]is " , end="") print(word[start:finish]) input("\nPress enter to exit") I hope i made sense. Thankyou ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Correct use of model-view-controller design pattern
On 31/05/16 02:16, boB Stepp wrote: > I perhaps see a general principle developing here: Before passing > data elsewhere, the data should be validated and in a form suitable > for where it is being sent. Correct. > By "business service" are you intending this to mean a totally > different application, or just a different model within the same > overall program? Either. It could even be a web service provided by a different organisation altogether. This is quite common for credit checks and the like. (Although a credit check would almost certainly be done by a model rather than a controller... That falls into business logic territory not presentation.) > I take it "braces" are what we call "suspenders" in the US? I believe so. Suspenders in the UK are used to hold up your socks :-) > you are talking about is redundant validation? When would this be > actually called for as it seems to contradict DRY principles? Correct. The file handling example is a case where it might be valid since the model may use the same method to process a line from a file as a record from the UI. However some OOP practitioners like to build some validation into models anyway because models are often reusable in non UI context - eg batch processing jobs - where there is no front end validation. Another case is where an organisation publishes its model API behind a web service. Can you trust your web clients to validate? Maybe not. So you either validate in the web service code or in the model. Yet another case is where you handle mission critical shared data. If you are writing that data into a central database shared with multiple applications you need to be sure you don't corrupt it so you may add some validation, just in case... > only thing that is coming to my mind now is code for something where > failure is not an option and the source of the incoming data (Which > should be doing proper validation.) was coded by someone else. If I > were coding both myself, then I would think that I should be able to > trust that I did the job correctly the first time! Absolutely right. > I guess what I am getting out of this discussion of MVC, is to not be > too rigid in my thinking, but try to decouple these three broad areas > of responsibility as much as is practical, so as to make the resulting > code more maintainable and scalable. Is this the correct big picture > idea? Yes, although for any given implementation you should decide the responsibilities and stick to them. I'm currently doing a lot of Java coding and the Java Swing UI framework has a very specific and rigid take on MVC and you have to use that definition of the UI simply won't work! So the framework you choose will often determine how you use MVC. But in the bigger picture it is a set of principles. MVC is one specific version of the more general programming rule to separate presentation from business logic. -- 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] Correct use of model-view-controller design pattern
On 31/05/16 02:25, boB Stepp wrote: > This "views register with models" and "registered views" terminology > is unfamiliar to me. Googling suggests registered views is referring > to a dynamically generated presentation, perhaps in the sense of a db No, this is different. Try googling publish-subscribe instead. The pattern is that something has data to publish (the model) and other things want to know about when the data changes (the associated view(s)). So when a view is created it knows which model(s) it wants updates from and registers with them. (The model has a register() method that adds the new view to a list of subscribers.) When something changes on the model it calls its publish() method which simply traverses the subscriber list and sends an update() message to each (usually with itself as the argument). The view can then interrogate the model to see if any of the data changes affect its display and, if so, make the corresponding updates on the UI. When a view is deleted it unregisters from the model. In summary: - A view constructor must have a model parameter and register with the model. - A view must implement an update() method - A view destructor must unregister itself from the model - A Model must have a register() method - A Model must have an unregister() method - A model must have a publish() method This mechanism is most commonly used where the view/controller are combined into a single implementation entity. -- 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