Re: [Tutor] Where do I start learning Python
On 09/08/2013 09:00 PM, olatunde Adebayo wrote: hey everyone, I am taking a graduate level class this fall that required python programming. can anyone direct me to where can i get a free python training crash course / program anyone with idea. I have one week to learn..is it possible You can definitely have something going on in a week for your stuff (It takes less if you have some background in programming and these things just make sense to you. You won't develop an awesome piece that solves world problems, but for what you'll probably do (lists, etc..) you'll be able to do that). There is a great one from Zed Shaw called "Learn Python The Hard Way". Basically, it gets you to actually write a lot (provided you don't cheat). It's available here: http://learnpythonthehardway.org/book/ Welcome, good luck and keep us posted on your progress, Olatunde. -- ~Jugurtha Hadjar, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] [Re:] I need help with the following question
On Aug 27, 2013, at 3:40 AM, isaac Eric wrote > print "For a circle of radius %s the area is %s" % (radius,area) > Question: What is the purpose of %s ? I will admit that this is homework for me. However, this is more for my log book and not for marks. According to my understanding, the purpose of the %s is to turn the numbers, which the program has recognized as numbers, into strings, so that they fit in the print command without any syntax errors. Could you guide me in the right direction if it is completely off? *Tab Tab* ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Re:] I need help with the following question
On 10 September 2013 08:58, Thabile Rampa wrote: > On Aug 27, 2013, at 3:40 AM, isaac Eric wrote > > > >> print "For a circle of radius %s the area is %s" % (radius,area) > >> Question: What is the purpose of %s ? > > I will admit that this is homework for me. However, this is more for my log > book and not for marks. > > According to my understanding, the purpose of the %s is to turn the numbers, > which the program has recognized as numbers, into strings, so that they fit > in the print command without any syntax errors. > > Could you guide me in the right direction if it is completely off? You are correct. '%s' is used to convert numbers (or other non-string things) into strings so that they can be used in places where text is required. In the particular case of the print command, this is done automatically so printing a number directly works just fine: >>> a = 123.0 >>> print a 123.0 >>> print 'The size is:', a The size is: 123.0 The '%s' form though allows you to insert the string representation of the number at the appropriate place in the string making it a bid cleaner to read e.g.: >>> radius = 4 >>> pi = 3.1412654 >>> area = pi * radius ** 2 This one: >>> print 'For a circle of radius', radius, 'the area is', area For a circle of radius 4 the area is 50.2602464 is equivalent to this one: >>> print 'For a circle of radius %s the area is %s' % (radius, area) For a circle of radius 4 the area is 50.2602464 If you just want to get the string representation of a number you can just use the str() function: >>> area 50.2602464 >>> str(area) '50.2602464' Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Re:] I need help with the following question
On 10/09/13 08:58, Thabile Rampa wrote: print "For a circle of radius %s the area is %s" % (radius,area) Question: What is the purpose of %s ? Oscar has answered your basic question but to add to his comments thee are other reasons for using the %s rather than str() or simply printing the variables directly. The %s allows us to add extra information to control the format of the string produced, for example the total field length and whether it is left or right justified. eg Try >>> "%12s" % "Hello" >>> "%-12s" % "Hello" >>> "%-12.4s" % "Hello" You can read about all the string formatting characters and their 'extras' here: http://www.python.org/doc//current/library/stdtypes.html in Section 6.6.2 Note that in Python 3 this style of string formatting is being deprecated in favour of the new format method of strings (linked on the same page above under the str.format() method) which offers even more options. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ 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] [Re:] I need help with the following question
On 10/9/2013 03:58, Thabile Rampa wrote: > > On Aug 27, 2013, at 3:40 AM, isaac Eric > wrote> > > print "For a circle of radius %s the area is > %s" % (radius,area) > > > Question: What is the purpose of %s ?I will > admit that this is homework for me. However, this is more for my log book and > not for marks.According to my understanding, the purpose > of the %s is to turn the numbers, > which the program has recognized as numbers, into strings, so that they fit > in the print command without any syntax errors.Could you > guide me in the right direction if it is completely off? > Tab Tab > Please post using text email, not html email. In this particular message, your text was pretty easy to extract, but in many cases the html email will become quite garbled by the time the newsreaders pick it up. Besides, it wastes space. The statement, print "For a circle of radius %s the area is %s" % (radius,area) has several areas of interest. Let's decompose it. print - the print statement, which will take whatever expressions it is handed, and convert each to a string before sending them to stdout. In this particular case, there is exactly one expression, and it already is a string. % - the formatting operator, which takes a string on the left side, and a tuple (or other interable) on the right, and combines them together to form a single string. http://docs.python.org/2/library/stdtypes.html#string-formatting-operations "For a circle of radius %s the area is %s" This string contains two %s place-holders. These are not python language syntax, but are clues to the formatting operator that you want a substitution to happen there. So there are two of them, to line up with the two items in the tuple. In this case they are both looking for strings. But %d could have been used to indicate that we want int numbers. And many other combinations could be used, depending on the type of object being used. %08d would mean take the int and left pad it with zeroes till it's 8 characters wide. If you are writing new code, the docs prefer you to use the format method of strings. in this case, the print statement might look something like: print "For a circle of radius {0} the area is {1}".format(radius, area) http://docs.python.org/2/library/stdtypes.html#str.format http://docs.python.org/2/library/string.html#formatstrings -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I need help with the following question
> Message: 3 > Date: Tue, 10 Sep 2013 09:58:31 +0200 > From: Thabile Rampa > To: tutor@python.org > Subject: [Tutor] [Re:] I need help with the following question > Message-ID: > > Content-Type: text/plain; charset="iso-8859-1" > > On Aug 27, 2013, at 3:40 AM, isaac Eric wrote > > > > print "For a circle of radius %s the area is %s" % (radius,area) > > Question: What is the purpose of %s ? > > I will admit that this is homework for me. However, this is more for my log > book and not for marks. > > According to my understanding, the purpose of the %s is to turn the numbers, > which the program has recognized as numbers, into strings, so that they fit > in the print command without any syntax errors. > > Could you guide me in the right direction if it is completely off? > > *Tab Tab* I'm not a python "brainiac" so I apologize in advance if my answer is lacking I'll try to be as thorough as possible. In the light of another recent question on here "Where do I start python" I want to point out there's been tons of linkage to places where it's rather easy to find the answer what %s %d and %r are One of my favs is the http://learnpythonthehardway.org/book/ and if you start from exercise 5 onwards you should get a better idea of all the print options there are in Python and how to use them efficiently. If you were inquiring SPECIFICALLY about 'formaters' (the %s,d,i,r look no further then basic Python manual here: http://docs.python.org/2/library/stdtypes.html#string-formatting Lucky for you the %s automatically converts any argument to string with str() which works for everything in Python, except you might not like the look of the output. Be careful to use %i or %d for integers otherwise floats will be rounded up. Printing strings when using %i will report an error. I don't think there's any difference between %d (d does NOT stand for double) and %i. If you want pretty decimals (1.1) and not floats (1.10001) use the decimal module. Else if you were interested in all the ways you can print in python just look at the learn python link but here's the crash course anyhow. I don't think you should have any problems if you ever worked in any of the big c's before. Printing in Python 3 onwards needs parentheses around the arguments you're printing, I think for Python <3 following should work: Basically if in python you want to print string, python can automatically connect them in a sentence: print "This"+"is"+"ok" This is ok but that won't work if the print arguments are not strings i.e.: print "This"+"is"+"not" + 6 + "ok" TypeError: cannot concatenate string and integer objects and it's also silly to do that, could you imagine explicitly converting everything to string? print str(hours)+":"+str(minutes)+":"+str(seconds)) fugly! What you want to do, resembles the c and c++ printf syntax print "You can add string %s and number %d like this" %(string, number) You can add string Banana and number 5 like this Or also fine, valid only for python>=2.6, and also the way that I prefer for longer strings is the C# syntax (I think): print "Something is here: {0}".format(anything) Something is here: (any number or string) Because it avoids the necessity for declaring exactly what is it you're printing and program won't crash, or at least it avoids the need to add special try catch blocks just for printing. If you see your output is not what you want you can return and try to work out exactly what happened. Hope no serious rules were broken by answering to this question... Regards, Dino ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Question about Functions
Dear tutors: The following is an example I found in the Raspberry Pi for Dummies book: #function test def theFunction(message): print "I don't get ", message return "ARRRGH!" theFunction("this") result=theFunction("this either") print "reply is: ", result - The result of this code looks like this: I don't get this I don't get this either reply is: ARRRGH! -- Here's what I don't understand: When I declare a variable to be equal as the fucntion (result=theFunction("this either")) is Python also executing the function? The way I see it, I only called for the function once before printing ARRRGH!! Then after that I declared a variable and then I print. This is how I expected the result to look like: I don't get this reply is: I don't get this either ARRRGH! - Can you help me understand? I can't move forward until I understand how Python solves this code. Thanks in advance Optional ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about Functions
On Tue, Sep 10, 2013 at 1:49 PM, novo shot wrote: > Dear tutors: > > The following is an example I found in the Raspberry Pi for Dummies book: > > #function test > > def theFunction(message): > print "I don't get ", message > return "ARRRGH!" > > theFunction("this") > the above line invokes theFunction, and prints "I don't get this". It returns "ARRRGH" but you con't assign that to a name, so it is lost. > > result=theFunction("this either") > Now you call theFunction again and it prints "I don't get this either". You save the ARRRGH bit in result > print "reply is: ", result > You print the value of result with is ARRRGH > > - > The result of this code looks like this: > > I don't get this > I don't get this either > reply is: ARRRGH! > > -- > Here's what I don't understand: > > When I declare a variable to be equal as the fucntion > (result=theFunction("this either")) is Python also executing the > function? > Yes it is > > The way I see it, I only called for the function once before printing > ARRRGH!! Then after that I declared a variable and then I print. > > You see it wrong. Each time you have theFunction(...) in your code that function is run > This is how I expected the result to look like: > > I don't get this > reply is: I don't get this either > ARRRGH! > > - > > Can you help me understand? I can't move forward until I understand > how Python solves this code. > > Thanks in advance > Optional > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Question about Functions
Dear tutors: The following is an example I found in the Raspberry Pi for Dummies book: #function test def theFunction(message): print "I don't get ", message return "ARRRGH!" theFunction("this") result=theFunction("this either") print "reply is: ", result - The result of this code looks like this: I don't get this I don't get this either reply is: ARRRGH! -- Here's what I don't understand: When I declare a variable to be equal as the fucntion (result=theFunction("this either")) is Python also executing the function? The way I see it, I only called for the function once before printing ARRRGH!! Then after that I declared a variable and then I print. This is how I expected the result to look like: I don't get this reply is: I don't get this either ARRRGH! - Can you help me understand? I can't move forward until I understand how Python solves this code. Thanks in advance Optional ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about Functions
On 2013-09-10 13:34, novo shot wrote: > When I declare a variable to be equal as the fucntion > (result=theFunction("this either")) is Python also executing the function? You're not declaring it as equal, that would be `==' (or `is' for identity). `=' assigns, it doesn't check for equality. > The way I see it, I only called for the function once before printing > ARRRGH!! Then after that I declared a variable and then I print. > > This is how I expected the result to look like: > > I don't get this > reply is: I don't get this either > ARRRGH! Why do you expect "reply is" to happen on the second line? It clearly only happens when printing the returned value, not when printing from inside the function itself: > def theFunction(message): > print "I don't get ", message > return "ARRRGH!" > > theFunction("this") > > result=theFunction("this either") > print "reply is: ", result The extra spaces are because "," implies one. If you don't want a double space before the message, remove the trailing space in the string. pgpLcomPv7jtT.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about Functions
novo shot wrote: > Dear tutors: > > The following is an example I found in the Raspberry Pi for Dummies book: > > #function test > > def theFunction(message): > print "I don't get ", message > return "ARRRGH!" > > theFunction("this") > > result=theFunction("this either") > print "reply is: ", result > > - > The result of this code looks like this: > > I don't get this > I don't get this either > reply is: ARRRGH! > > -- > Here's what I don't understand: > > When I declare a variable to be equal as the fucntion > (result=theFunction("this either")) is Python also executing the > function? Yes, when you do function() it calls the function. In this case it is calling theFunction with the string arguments 'this' and 'this either', respectively. > The way I see it, I only called for the function once before printing > ARRRGH!! Then after that I declared a variable and then I print. > > This is how I expected the result to look like: > > I don't get this > reply is: I don't get this either > ARRRGH! You do not get this output because you do not return the string with the `message`, you print it immediately and return "ARRRGH!". "ARRRGH!" then gets bound to the name `result` which you then print. If you want the result you specify you should return "reply is: " + result # where result must be a string Not sure how to expect to get "ARRRGH!" unless you return that too. You can return multiple objects but you typically need to attach it to an object. Lists and tuples are frequently used to return multiple objects. Some examples are below. # tuple return a,b,c # list (in-line) return [ a, b, c] # list (created and all objects added earlier) list_object = [] for x in xrange(4): list_object.append( x ) #just an example return list_object # as attribute (use when you need to pass state / data handling) obj = Class() obj.attribute = [a,b,c] return obj > > - > > Can you help me understand? I can't move forward until I understand > how Python solves this code. I recommend going through some beginner Python tutorials first to get a grasp of how Python works before you start on a book for Raspberry Pi. > > Thanks in advance > Optional ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about Functions
On 10/09/13 18:49, novo shot wrote: Dear tutors: The following is an example I found in the Raspberry Pi for Dummies book: #function test def theFunction(message): print "I don't get ", message return "ARRRGH!" All the code above does is define the function and assign it the name theFunction (which is a terrible name but we'll ignore that for now!) It does not execute the function. theFunction("this") Any time the function name is followed by parentheses (but not preceded by def) the function gets executed. So this calls the function passing in the argument 'this' which results in the string "I don't get this" being printed. The value 'AARGH!' is returned but not stored anywhere so is lost. result=theFunction("this either") This time the function is executed with the argument 'this either' passed in and the string "I don't get this either" is printed. Again the value 'AARGH!' is returned but this time it is assigned to the variable result. print "reply is: ", result This prints the string "reply is: AARGH!" The result of this code looks like this: I don't get this I don't get this either reply is: ARRRGH! Which is what I described above. When I declare a variable to be equal as the fucntion (result=theFunction("this either")) is Python also executing the function? Yes, every time the function name is followed by parentheses it gets executed (and the return value assigned to any variable ready to receive it). The way I see it, I only called for the function once before printing ARRRGH!! Then after that I declared a variable and then I print. Wrong. You defined the function once. You called it twice. Then you printed the return value of the second invocation. This is how I expected the result to look like: I don't get this reply is: I don't get this either ARRRGH! The 'I don't get...' line is printed inside the function. The program outside the function knows nothing about that. It has no way to access that string. That separation of what's inside the function from the code outside is a very important feature of programming and is known as abstraction, data hiding and encapsulation. (All subtly different variations on the same theme but very important in programming). It is this feature that enables us to write reusable functions that can be inserted into any program without relying on, or breaking, the surrounding code. One final thing to note is that when you are at the Python interpreter prompt the return value of a function is always printed. But when executing a script file the return value is not printed unless the program does it explicitly using print. >>> def f(): ... print 'this is always printed' ... return "this isn't" ... >>> f() this is always printed "this isn't" Now if you put the definition of f() in a file called test.py like this: def f(): print 'this is always printed' return "this isn't" f() ## and run it, you will only see the first line printed. The return value has been lost because it was never explicitly printed out. This difference in behaviour between the prompt and executing a script often confuses beginners but is a useful feature when testing/experimenting at the >>> prompt. You just need to remember that it will not always give identical behaviour to a script. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ 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] Question about Functions
> Date: Tue, 10 Sep 2013 13:34:50 -0400 > From: novo shot > To: tutor@python.org > Subject: [Tutor] Question about Functions > Message-ID: > > Content-Type: text/plain; charset="iso-8859-1" > > Dear tutors: > > The following is an example I found in the Raspberry Pi for Dummies book: > > #function test > > def theFunction(message): > print "I don't get ", message > return "ARRRGH!" > > theFunction("this") > > result=theFunction("this either") > print "reply is: ", result > > - > The result of this code looks like this: > > I don't get this > I don't get this either > reply is: ARRRGH! > > -- > Here's what I don't understand: > > When I declare a variable to be equal as the fucntion > (result=theFunction("this either")) is Python also executing the function? > > The way I see it, I only called for the function once before printing > ARRRGH!! Then after that I declared a variable and then I print. > > This is how I expected the result to look like: > > I don't get this > reply is: I don't get this either > ARRRGH! > > - > > Can you help me understand? I can't move forward until I understand how > Python solves this code. > > Thanks in advance Function in Python work very much the same way like they do in most programming languages. When working with them you can't really "feel" the difference. There is a big difference, however, "underneath" (just always keep in mind everything in Python is an object, the book should mention that repeatedly). Since you didn't mention I have to assume you didn't do any programming before python. In the case of big C's (C, C++, C#) I was always thought that I should look at a function as a variable. In particular the exact same variable you return. I.e. if I have a function: >>> def add1(int_number): return number+1 you can look at function add1 as if it's an integer because it returns an integer. The same applies to your example, which you can see if you do some introspection: >>> type(result) So you see your result is nothing more then a string! Basically whatever you return in your function will be assigned to the variable you return it to. Because you return "AARGH" in your function and assign the return value to result: >>> result=theFunction("thisEither") the variable result will become "AARGH". So this line basically amounts to: >>> result= "AARGH" This is a special case because this function always returns the same thing. That's not usually the case with functions. I.e. let's return to my add1 function. Output (that part behind the return statement) of add1 function will change depending on the input number: >>> add1(3) 4 >>> added = add1(5) >>> print added 6 What you can also see from the above example is that when I explicitly assigned a variable to a function [added = add1(5)] the result of the function did not print out, but the function must have executed because variable added has a value of '6'. So function executes every time you call on it, but prints out value of "return" only when you don't specify a "container" that will hold it's output. I'm pretty sure you're confused here because you have a print statement in your function. Print statement calls on your standard output that works "over" anything and pretty much "whenever", it's kind of special that way. If I changed my example function add1 to: >>> def add1(int_number): print "Working, hold your horses" return number+1 then my output from before would be: >>> add1(3) Working, hold your horses 4 >>> added = add1(5) Working, hold your horses >>> print added 6 This is the main difference between return and print, think of return like it defaults to print if there is no value to which it can assign what it returned. That is also why you always see line "I don't get (this/this either)" printed every time you call on your function. >>> theFunction("this") I don't get this ARRGHHH >>> result=theFunction("this either") I don't get this either >>> print "reply is: ", result reply is AARGGG I think it should be pretty clear by now how it works. On another note apparently ugly with an f in front is a bad word around here, my apologies I'm fairly new around these places and was inquiring for your help not even a week ago and don't really know how things work. But I am willing to help out like you did me, does that count? Also I'm not a programmer so I imagine analogies when I program and may be off point sometimes, hope I didn't make too many people cringe because of that... Regards, Dino ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.pyth
Re: [Tutor] Question about Functions
On Tue, Sep 10, 2013 at 01:49:11PM -0400, novo shot wrote: > When I declare a variable to be equal as the fucntion > (result=theFunction("this either")) is Python also executing the > function? No, you have misunderstood. Equals in programming languages is not the same as equals in mathematics. Some languages, like Pascal, use := instead of = in order to avoid that misunderstanding. In mathematics, "y = 3*x - 1" declares that y is equal to the equation on the right, no matter what value x happens to have. But in programming, instead it *evaluates* the equation on the right, using the current value of x, and *assigns* the result to y. So in your example above, result=theFunction("this either"), Python evaluates the function call theFunction("this either"), collects whatever result is returned (but not what is printed!), and assigns that to the variable "result". Since theFunction prints some things, they will be printed but not assigned to anything. Only whatever is returned, using the "return" statement, will be assigned to variable "result". [...] > Can you help me understand? I can't move forward until I understand > how Python solves this code. It might help you to start with a simpler example: def print_test(): print "Hello World!" # use print("...") in Python 3 This function takes no arguments, and always prints the same thing. It returns nothing. (To be precise, it returns the special object None, but don't worry about that.) py> print_test() # Round brackets calls the function Hello World! py> result = print_test() Hello World! py> result py> So this demonstrates that printing values just prints them. You cannot access the printed result programatically. It just gets printed to the screen and that's it. def return_test(): return "And now for something completely different!" This function uses return instead of print. Here it is in use: py> return_test() 'And now for something completely different!' py> result = return_test() py> result 'And now for something completely different!' py> result.upper() 'AND NOW FOR SOMETHING COMPLETELY DIFFERENT!' So as you can see, using return is *much* more flexible. You can capture the result of the function, hold of printing it until you want, or process it further, pass it on to other functions, or so forth. Finally, let's combine the two: def test_both(): print "This is immediately printed." return "And this is returned." By now you hopefully should be able to predict what calling this function will do, but just in case you can't: py> result = test_both() This is immediately printed. py> result 'And this is returned.' py> result.upper() 'AND THIS IS RETURNED.' -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor