[Tutor] Non programmer wanting to become programmer
First of all, hello! I want to start learning programming. I'm looking into becoming more than a hobbyist programmer. I searched a lot on Google on what programming language should I learn first and I see a lot of good words about Python so I decided to go for it but have some questions: 1)What book should I start with? ( I have checked Python for non programmers but there are a lot of titles there, what should I pick first?I was thinking about Invent your own computer games with Python.) 2)Version 2 or version 3? What should I go for as a beginner and why? ( I ask because some books in the Python for non programmers section are for python 2, Invent your own computer games with Python is version 3.) 3)Algorithms, memory management, data structures, when is the right time to learn them? Regards, amt. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Non programmer wanting to become programmer
Thank you all for the replies. They helped me a lot. Have a great weekend! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Floating point exercise 3 from Learn python the hard way
Hello, I am a python beginner currently learning from "Learn python the hard way". I'm stuck at exercise 3 and I would like if it's possible to get some help so I can move on. Here is the source code: 1 print "I will now count my chickens:" 2 print "Hens", 25 + 30 /6 3 print "Roosters", 100 - 25 * 3 % 4 #Output is 97 4 print "Now I will count the eggs:" 5 print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 #Output needs to be 6,83 but Python give me 7 6 print "Is it true that 3 + 2 < 5 - 7? " 7 print 3 + 2 < 5 - 7 8 print "What is 3 + 2?", 3 + 2 9 print "What is 5 - 7?", 5-7 10 print "Oh, that's why it's False." 11 print "How about some more. " 12 print "Is it greater?", 5 > -2 13 print "Is it greater or equal?", 5 >= -2 14 print "Is it less or equal?", 5 <= -2 The author says " Notice the math seems “wrong”? There are no fractions, only whole numbers. Find out why by researching what a “floating point” number is." I have to rewrite it to use floating point numbers so it's more accurate. I have read the lines of code and only line 3 and 5 needs to be changed. I came up with: print "Roosters", 100 - float(25) * 3 % 4 This is for line 3 so it is more precised. Is it correct what I did? What do I have to do for line 5? Thanks in advance! Regards, amt. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Floating point exercise 3 from Learn python the hard way
Hello, my version is Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39). The book only talks about Python 2.x. So, how do I solve the exercise? 3. print "Roosters", 100 - 25 * 3 % 4.00 5. print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4.00 + 6 Is this correct? I'm a bit confused at line 5 because python returns 6,75 and wolfram alpha tells me the result of that expression is 6,83. Sorry, my math is VERY rusty. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Floating point exercise 3 from Learn python the hard way
> > > Can you explain your reasoning? Why do you think line 3 needs > to be changed? Why line 5? Well the exercise says: "Rewrite ex3.py to use floating point numbers so it’s more accurate (hint: 20.0 is floating point)." I am honestly confused. I have read the exercise and found three lines that could use the floating point(2,3 and line 5). This is were the confusion is appearing. I can understand why at line 5 I use floating point. 6,75 is more precise than saying 7. But I can't understand at line 2 and 3. I mean it makes no difference for me. Saying 30 or 30.0 is the same thing. As well as saying 97 or 97.0. > > I came up with: >> print "Roosters", 100 - float(25) * 3 % 4 >> >> This is for line 3 so it is more precised. >> > > In what way do you think this is more precise? > As I said, I am confused when it comes to line 2 and 3. But I think having more digits after the " ." makes it more precise. > > I don't understand your comment on line 5. > Why would the answer be 6.83? > Can you add parentheses to the expression > showing how you arrive at 6.83? > The answer at line 5 is 7 because I have integers and I think Python approximates. I have done line 5 with pencil and paper: 3 + 2 + 1 - 5 = 1, 1 + 4%2 = 1, 1 - (1/4)= 1, 1 + 6 = 7 If I use floating point the right answer will be 6,75 but I don't know how to get to this answer. On paper I got 7 not 6,75. I'm horrible at math right now, so please forgive me. > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Thanks for helping me out! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Floating point exercise 3 from Learn python the hard way
On Tue, Jun 14, 2011 at 12:27 AM, Alan Gauld wrote: > > I can understand why at line 5 I use floating point. 6,75 is more >> precise than saying 7. >> > > Exactly, no problem with line 5 (except the odd comment about 6.83) The comment on line 5 was a mistake. > > But I can't understand at line 2 and 3. I mean it makes no difference >> for me. Saying 30 or 30.0 is the same thing. >> As well as saying 97 or 97.0. >> > > Precisely, thats why I asked the question. > > As a beginner at line 2 and 3 I see no point of using floating numbers except the fact to serve as an example that if I have a floating point number it affects the rest of the expression evaluation. So, should I keep them or not?It's unclear to me and sadly the author of the book doesn't provide the solutions to the exercises. The only way I can verify myself is using this list. Regards, amt. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Floating point exercise 3 from Learn python the hard way
Everything is clear now. Thank you for your replies. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] What's the difference between %s and %r?
Hello! I'm having troubles understanding what is the difference between %s and %r(format characters). I did google and found something on StackOverflow but I don't understand the explanation as it's not beginner orientated. Also, I have this code from learn python the hard way. Why at line 9 does he uses %r? Why did he didn't wrote print "I said: %s." %x ? 1x = "There are %d types of people." % 10 2binary = "binary" 3do_not = "don't" 4y = "Those who know %s and those who %s." % (binary, do_not) 5 6print x 7print y 8 9print "I said: %r." % x 10 print "I also said: '%s'." % y 11 12 hilarious = False 13 joke_evaluation = "Isn't that joke so funny?! %r" 14 15 print joke_evaluation % hilarious 16 17 w = "This is the left side of..." 18 e = "a string with a right side." 19 20 print w + e Thanks in advance! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's the difference between %s and %r?
Hello! Thank you all for writing and helping me out. I now understand the difference between the two format characters. -amt ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Other ways to use raw_input function
Greetings! I got stuck at an exercise from the book learn python the hard way about raw_input function.(exercise 11.2) After reading from docs.python.org I found out that this function can perform standard output without a trailing newline.Also it can take an input line, convert it as a string( stripping a trailing newline) and return it. The exercise question is : Can you find other ways to use it? Here is the original code from the book: print "How old are you?", age = raw_input() print "How tall are you?", height = raw_input() print "How much do you weigh?", weight = raw_input() print "So, you're %r old, %r tall and %r heavy." % ( age, height, weight) Now, the only different way I was able to use raw_input is: age = raw_input("How old are you? ") height = raw_input("How tall are you? ") weight = raw_input("How much do you weigh? ") print "So, you're %r old, %r tall and %r heavy." % (age, height, weight) Are there any other ways of using it? If yes can you please give me a detailed example so I can understand. Thanks in advance, amt. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Other ways to use raw_input function
Thanks a lot Alan for helping me out every time. Also thanks Wayne! Regards, amt. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Suggest Book
Hi Pankaj! Have a look at : http://wiki.python.org/moin/BeginnersGuide http://norvig.com/21-days.html I'm currently learning from this book: http://learnpythonthehardway.org/book/ Have a look. It's a good book for starting out. Just pick one that you like and start learning. I wish you good luck! Regards, amt. On Fri, Nov 11, 2011 at 4:29 PM, Pankaj Jakhar wrote: > Hello > > Please suggest me the best book for Python from which I can learn basics > to advanced Python. > > Thank you. > * > PankaJ **Jakhar** > * > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Are there other ways of solving this exercise?
Exercise 16, extra credit 3: There's too much repetition in this file. Use strings, formats, and escapes to print out line1, line2, and line3 with just one target.write() command instead of 6. Code from the book: from sys import argv script, filename = argv print "We're going to erase %r." % filename print "If you don't want that, hit CTRL-C (^C)." print "If you do want that, hit RETURN." raw_input("?") print "Opening the file..." target = open(filename,'w') print "Truncating the file. Goodbye!" target.truncate() print "Now I'm going to ask you for three lines." line1 = raw_input("line 1: ") line2 = raw_input("line 2: ") line3 = raw_input("line 3: ") print "I'm going to write these to the file." target.write(line1) target.write("\n") target.write(line2) target.write("\n") target.write(line3) target.write("\n") print "And finally, we close it." target.close() How I solved it after trial and error: from sys import argv script, filename = argv print "We're going to erase %r." % filename print "If you don't want that, hit CTRL-C (^C)." print "If you do want that, hit RETURN." raw_input("?") print "Opening the file..." target = open(filename, 'w') print "Truncating the file. Goodbye!" target.truncate() print "Now I'm going to ask you for three lines." line1 = raw_input("line 1: ") line2 = raw_input("line 2: ") line3 = raw_input("line 3: ") print "I'm going to write these to the file." target.write("%s\n%s\n%s\n" %(line1, line2, line3)) print "And finally, we close it." target.close() This is the only method I was able to figure out of solving the exercise. Are there other ways of solving this exercise using strings, formats and escapes like the author mentioned in the exercise question? If yes, please write them. Regards,amt. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Are there other ways of solving this exercise?
On Thu, Jan 12, 2012 at 9:40 AM, Walter Prins wrote: > Hi, > > On 12 January 2012 14:24, amt <0101...@gmail.com> wrote: >> >> target.write("%s\n%s\n%s\n" %(line1, line2, line3)) >> >> This is the only method I was able to figure out of solving the exercise. >> >> Are there other ways of solving this exercise using strings, formats >> and escapes like the author mentioned in the exercise question? If >> yes, please write them. > > > Firstly for those interested, I've tracked this down to "Learn Python > The Hard Way, 2nd Edition". (Amt, please include a reference if > possible (especially when online) when you ask questions.) The > question is available here: > http://learnpythonthehardway.org/book/ex16.html > > As for your question, I suppose using the string.format() method is > another way that involves "strings, formats and escapes". See here: > http://docs.python.org/library/stdtypes.html (and see the section on > str.format therein. ) > > Walter Ok, I will keep that in mind. After reading from http://docs.python.org/library/stdtypes.html I came up with this: from sys import argv script, filename = argv print "We're going to erase %r." % filename print "If you don't want that, hit CTRL-C (^C)." print "If you do want that, hit RETURN." raw_input("?") print "Opening the file..." target = open(filename, 'w') print "Truncating the file. Goodbye!" target.truncate() print "Now I'm going to ask you for three lines." line1 = raw_input("line 1: ") line2 = raw_input("line 2: ") line3 = raw_input("line 3: ") print "I'm going to write these to the file." bag = "%s\n%s\n%s\n".format(line1,line2,line3) target.write(bag) print "And finally, we close it." target.close() Is this how it is supposed to look like using str.format? Thanks,amt. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Are there other ways of solving this exercise?
I'll give it another try: On Thu, Jan 12, 2012 at 10:26 AM, Walter Prins wrote: > Hi amt, > > On 12 January 2012 15:11, amt <0101...@gmail.com> wrote: >> After reading from http://docs.python.org/library/stdtypes.html I came >> up with this: >> >> bag = "%s\n%s\n%s\n".format(line1,line2,line3) >> target.write(bag) >> >> Is this how it is supposed to look like using str.format? > > Not quite. The documentation states: > > "str.format(*args, **kwargs): Perform a string formatting operation. > The string on which this method is called can contain literal text or > replacement fields delimited by braces {}. Each replacement field > contains either the numeric index of a positional argument, or the > name of a keyword argument. Returns a copy of the string where each > replacement field is replaced with the string value of the > corresponding argument." > > So, this is different from the % operator, where format specifiers are > indicated with %. Instead you need to use, as per the documentation, > curly braces e.g. { and }. > > You can easily test this in the Python interpreter e.g.: > >>>> print "%s\n%s\n%s".format('aaa', 'bbb', 'ccc') > %s > %s > %s > > (Hmm, does not work...) > >>>> print '{0}\n{1}\n{2}'.format('aaa','bbb','ccc') > aaa > bbb > ccc > > (Hmm, that does work!...) So the code should look like this: bag = "{0}\n{1}\n{2}".format(line1,line2,line3) target.write(bag) > > Final comment, you can get rid of the variable "bag" by directly > printing the result of the call to format() like you did in your > previous solution. > > Cheers, > > Walter You mean print "{0}\n{1}\n{2}\n".format(line1,line2,line3)? Ok, but if I drop the variable bag and print directly,how will I write line1,line2,line3 in the .txt file since I have no parameter to give to the write method.(target.write() ) ? Walter, thanks a lot for taking your time to help me out. Cheers, amt. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Are there other ways of solving this exercise?
Wow!!! Thanks so much for your reply. It was so nicely written and I understood everything you said. Thanks again, have a nice weekend. Regards, Amt. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Why do you have to close files?
Exercise 17, extra credit 6 Learn python the hard way: Find out why you had to do output.close() in the code. Code: from sys import argv from os.path import exists script, from_file, to_file = argv print "Copying from %s to %s" % (from_file, to_file) input = open(from_file) indata = input.read() print "The input file is %d bytes long" % len(indata) print "Does the output file exist? %r" % exists(to_file) print "Ready, hit RETURN to continue, CTRL-C to abort." raw_input() output = open(to_file, 'w') output.write(indata) print "Alright, all done." output.close() input.close() I don't get it. If you don't close input and output it works exactly the same as if you would close them, so why do you have to do output.close() and input.close()? Also does it matter if you do: input.close() and then output.close()? Is there an order to follow? Thanks in advance, amt. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why do you have to close files?
All the replies were very helpful! Thank you very much for helping me out! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Learn Python The Hard Way, Ex19-3
Hello! I'm currently stuck at the Extra Credit 3 from LPTHW. Link to the actual exercise:http://learnpythonthehardway.org/book/ex19.html The exercise: Write at least one more function of your own design, and run it 10 different ways. Code from the book: def cheese_and_crackers(cheese_count, boxes_of_crackers): print "You have %d cheeses!" % cheese_count print "You have %d boxes of crackers!" % boxes_of_crackers print "Man that's enough for a party!" print "Get a blanket.\n" print "We can just give the function numbers directly:" cheese_and_crackers(20, 30) print "OR, we can use variables from our script:" amount_of_cheese = 10 amount_of_crackers = 50 cheese_and_crackers(amount_of_cheese, amount_of_crackers) print "We can even do math inside too:" cheese_and_crackers(10 + 20, 5 + 6) print "And we can combine the two, variables and math:" cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) I wrote a function similar to cheese_and_crackers and it works just fine but I can't figure out more ways of calling a function other than the ones presented in the code(with integers as arguments,variables as arguments, two integer additions as arguments and with arguments in the form of variable+integer). The author states that there are 10 different ways to run it.(in a comment he states that: "You can run it a lot of different ways, far too many to enumerate.). So, what other ways are there aside the ones already presented in the above code? Thanks in advance, amt. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string integers?
Hello William and welcome to the Python list. I'm a beginner but I'll give it a shot. Problem is, you use raw_input and it returns a string, not an int. Try this code: str1 = raw_input("Type in a String: ") str2 = raw_input("Type in a String: ") int1 = int(raw_input("Type in a integer variable: ")) int2 = int(raw_input("Type in a integer variable: ")) print "{0}{1}{2}".format(str1, str2, int1*int2) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Learn Python The Hard Way, Ex19-3
Hello! I managed in the end to have more than 10 ways of doing it, moving now to Exercise 20. Thank you so much for helping me out every time. Regards, amt ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor