[Tutor] Creating Lists
Create a list of 20 unique (no number appears twice) random integers with values between 1 and 45. Print the list of random numbers with the header “Random list of 20 numbers”. Find the largest number in the list. Remove the largest number from the list. Find the smallest number in the list. Remove the smallest number from the list. Print the length of the list with the header “The length of the list is: ” Print the list with the header “The list with the largest and smallest number removed: ” Sent from Windows Mail___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Creating Lists
On 12/11/14 04:27, niyanax...@gmail.com wrote: Create a list of 20 unique (no number appears twice) random integers with values between 1 and 45. Print the list of random numbers with the header “Random list of 20 numbers”. Find the largest number in the list. Remove the largest number from the list. Find the smallest number in the list. Remove the smallest number from the list. Print the length of the list with the header “The length of the list is: ” Print the list with the header “The list with the largest and smallest number removed: ” OK, You've shown us your homework, it all looks very reasonable. Now tell us what you have tried. How did it work? Are there any bits you are stuck with? Maybe we can help. But we won't just do your homework for you. Tell us which version of Python you are using too, since that will make a difference. -- 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] Creating Lists
niyanax...@gmail.com writes: Create a list of 20 unique (no number appears twice) random integers […] Print the list with the header “The list with the largest and smallest number removed: ” That all sounds like a good assignment. Feel free to show us your complete program and we can offer advice. This is a forum for *tutoring*, which means you still have to do the work. -- \ “We must find our way to a time when faith, without evidence, | `\disgraces anyone who would claim it.” —Sam Harris, _The End of | _o__) Faith_, 2004 | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] syntax error with raw input
Hi I am new to python. I learnt (!) using raw_input a day back. Attempt to use has resulted in error. I am not able to spot a problem in syntax. I am using python 3.4.2. Kindly help a = raw_input(Write down your name: ) Output: Traceback (most recent call last): File pyshell#1, line 1, in module a = raw_input(Write down your name: ) NameError: name 'raw_input' is not defined ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] syntax error with raw input
On Wed, Nov 12, 2014 at 12:08 PM, Vaibhav Banait careendosc...@gmail.com wrote: Hi I am new to python. I learnt (!) using raw_input a day back. Attempt to use has resulted in error. I am not able to spot a problem in syntax. I am using python 3.4.2. Kindly help a = raw_input(Write down your name: ) Output: Traceback (most recent call last): File pyshell#1, line 1, in module a = raw_input(Write down your name: ) NameError: name 'raw_input' is not defined ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor In Python 3, raw_input() was renamed to input(). a = input(Write down your name: ) Note that input() is also a thing in Python 2, but you shouldn’t use that one for security reasons. Python 3’s is fine though. -- Chris Warrick https://chriswarrick.com/ PGP: 5EAAEA16 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] syntax error with raw input
On 12/11/14 11:08, Vaibhav Banait wrote: Hi I am new to python. I learnt (!) using raw_input a day back. Attempt to use has resulted in error. I am not able to spot a problem in syntax. I am using python 3.4.2. Kindly help Looks like you are reading a v2 book/tutorial but using v3. In v3 several key changes were made to Python including the renaming of raw_input() to input(). The other big change was making print a function so you now *must* put parentheses around anything you want to print. ie print 'hello' # Python v2 becomes print('hello') # python v3 There are numerous other small changes so it would be better for you to either find a v3 tutorial (you could try mine! :-) or install Python v2.7 while you are learning. -- 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
[Tutor] Creating Table
Create a table based on two user inputs. Have the user enter two integers between 2 and 6. Validate the user inputs are in the proper range as they are entered. The first integer represents the number of rows in the table. The second integer represents the number of columns. Create the table by storing the value (i **j) in each cell in the table. Print the Power table with a column header separated from the table by a line of underscores (“_”) and a row header that lists the row number, a “|” and the row of data. This is what I have. I am very confused on what to do. i = 6 j = 6 row = int(input(Row: )) column = int(input(Column: )) if row == 7 : print(Invalid entry.) elif row = 2 or row 6 : print(Invalid entry. The row has a range of 2 to 6. Try again.) if column == 7 : print(Invalid entry.) elif column = 2 or column 6: print(Invalid entry. The column has a range of 2 to 6. Try again.) for n in range(row) : print(%10d % n, end=) print() for n in range(1, row + 1) : print(%10s % x , end=) print(\n, , - * 35) for i in range(row): for j in range (column): table[i] [j] = int(table[i] [j]) # convert the string to an integer print( %3d % (table[i] [j]), end = ) print() Sent from Windows Mail___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] syntax error with raw input
Slightly hijacking this thread a bit, specifically Alan's reply, if anyone is averse to installing multiple versions of Python on their computers, you can always access a Python interpreter from a browser window. Here are a collection I've put together. Most are Python 2, but the top pair also support Python 3: - http://ideone.com (2.x 3.x) - http://colabv6.dan.co.jp/lleval.html (2.x 3.x) - http://doc.pyschools.com/console - http://repl.it/languages/Python - http://skulpt.org (pure JS implementation; allows turtle) - http://pythonwebconsole.thomnichols.org - http://shell.appspot.com (Google App Engine + libraries) - http://codepad.org - http://lotrepls.appspot.com - http://datamech.com/devan/trypython/trypython.py Cheers, --Wesley On Wed, Nov 12, 2014 at 9:11 AM, Alan Gauld alan.ga...@btinternet.com wrote: On 12/11/14 11:08, Vaibhav Banait wrote: Hi I am new to python. I learnt (!) using raw_input a day back. Attempt to use has resulted in error. I am not able to spot a problem in syntax. I am using python 3.4.2. Kindly help Looks like you are reading a v2 book/tutorial but using v3. In v3 several key changes were made to Python including the renaming of raw_input() to input(). The other big change was making print a function so you now *must* put parentheses around anything you want to print. ie print 'hello' # Python v2 becomes print('hello') # python v3 There are numerous other small changes so it would be better for you to either find a v3 tutorial (you could try mine! :-) or install Python v2.7 while you are learning. -- 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 -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - A computer never does what you want... only what you tell it. +wesley chun http://google.com/+WesleyChun : wescpy at gmail : @wescpy http://twitter.com/wescpy Python training consulting : http://CyberwebConsulting.com Core Python books : http://CorePython.com Python blog: http://wescpy.blogspot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Creating Table
On Wed, Nov 12, 2014 at 11:33 AM, niyanax...@gmail.com wrote: Create a table based on two user inputs. Have the user enter two integers between 2 and 6. Validate the user inputs are in the proper range as they are entered. The first integer represents the number of rows in the table. The second integer represents the number of columns. Create the table by storing the value (i **j) in each cell in the table. Print the Power table with a column header separated from the table by a line of underscores (“_”) and a row header that lists the row number, a “|” and the row of data. This is what I have. I am very confused on what to do. [code cut] Hi Niyana, We need to pinpoint where you're getting confused. I get confused for all sorts of different reasons myself, so forgive me if this sounds really basic. :P I will ignore your code entirely for the moment. Do you know what the output is supposed to look like for small inputs? * What should the program output look like when rows=2 and columns=2? * What should the program output look like when rows=2 and columns=3? * What should the program output look like when rows=3 and columns=2? The inputs are small enough that you should be able to write what you know the program should do, even before trying to teach the computer how to do it. Let's make sure we understand the problem we're trying to solve before rushing to code it. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Creating Table
My reply is interleaved between your comments. On Wed, Nov 12, 2014 at 07:33:50PM +, niyanax...@gmail.com wrote: This is what I have. I am very confused on what to do. What part is confusing? Try to ask *specific* questions, don't expect us to do your homework for you. i = 6 j = 6 What is the purpose of these two lines of code? row = int(input(Row: )) column = int(input(Column: )) if row == 7 : print(Invalid entry.) elif row = 2 or row 6 : print(Invalid entry. The row has a range of 2 to 6. Try again.) Why do you single out 7 as a different error? Why do you treat 2 as an error when it is supposed to be allowed? if column == 7 : print(Invalid entry.) elif column = 2 or column 6: print(Invalid entry. The column has a range of 2 to 6. Try again.) The same comments apply for column as for row -- why single out 7, and why treat 2 as an error? for n in range(row) : print(%10d % n, end=) This will print a single row that looks like: 0 1 2 and so on. Is that the result that you wanted? If not, what result did you want? print() for n in range(1, row + 1) : print(%10s % x , end=) This will print a single row that looks like: x x x and so on. Is that the result that you wanted? If not, what result did you want? print(\n, , - * 35) This will print a single row that looks like: --- Is that the result that you wanted? If not, what result did you want? for i in range(row): for j in range (column): table[i] [j] = int(table[i] [j]) # convert the string to an integer print( %3d % (table[i] [j]), end = ) print() Where does table come from? What value or values does it contain? The question states that you should show the result of i**j (i to the power of j, e.g. 2**3 = 8, 2**4 = 16, and so forth) but your code doesn't calculate i**j anywhere. Have a look at this code, and see if it makes sense to you: for i in range(3): print(%4d ::: % i, end='') for j in range(3): print(%d plus %d = %d % (i, j, i+j), end=' *** ') print() Run the code and see what it prints. Can you see what bit of the code is responsible for what part of the output? Can you change the code to calculate i**j instead of i+j? Can you change the code to stop printing the 1 plus 1 = part of each cell, and just print the result alone? Can you change the code to separate the row headers from the cells with a single | character instead of three : characters? Can you change the code to use the values entered by the user instead of a fixed 3 x 3 table? Are there any other changes you need to make to finish the assignment? -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] syntax error with raw input
On Wed, Nov 12, 2014 at 04:38:51PM +0530, Vaibhav Banait wrote: Hi I am new to python. I learnt (!) using raw_input a day back. Attempt to use has resulted in error. I am not able to spot a problem in syntax. What makes you think it is a problem with syntax? This is what happens when you have a syntax error: py 23 42 + File stdin, line 1 23 42 + ^ SyntaxError: invalid syntax Look at the error you get instead: it doesn't say SyntaxError, does it? It says NameError. NameError: name 'raw_input' is not defined The reason is that in Python 3, raw_input has been renamed to just input. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Creating Lists
On Wed, Nov 12, 2014 at 04:27:33AM +, niyanax...@gmail.com wrote: Create a list of 20 unique (no number appears twice) random integers with values between 1 and 45. Here is how I would produce a list of 7 unique random integers with values between 123 and 175. Lines starting with py are code which I have typed in the Python interactive interpreter. I don't type the py part, Python automatically shows that. The output then follows: py import random py random.sample(range(123, 176), 7) [129, 151, 147, 137, 130, 153, 152] Can you adjust that to do what you want? Print the list of random numbers with the header “Random list of 20 numbers”. This is how I would print a list of numbers with a header, separating each number with a bunch of dots: py numbers = [2, 4, 8, 16] py print(This is a header) This is a header py print(*numbers, sep=..) 2..4..8..16 Note the asterisk * in front of numbers. What happens if you leave the asterisk out? What happens if you leave the asterisk in, but remove the sep= part? Find the largest number in the list. Remove the largest number from the list. Find the smallest number in the list. Remove the smallest number from the list. Here is how I would find the largest and smallest numbers from a list: py numbers = [17, 11, 3, 99, 100, 41] py min(numbers) 3 py max(numbers) 100 And here is how I would remove a number from a list: py print(numbers) # Before [17, 11, 3, 99, 100, 41] py numbers.remove(99) py print(numbers) # And after. [17, 11, 3, 100, 41] Print the length of the list with the header “The length of the list is: ” Print the list with the header “The list with the largest and smallest number removed: ” Here is how I would find out the length of the list: py len(numbers) 5 Does this help you solve the problem? -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] “has a value of True” versus “evaluates true” (was: don't understand iteration)
On Tue, Nov 11, 2014 at 10:24:00AM +1100, Ben Finney wrote: Clayton Kirkwood c...@godblessthe.us writes: Also of confusion, the library reference says: Match objects always have a boolean value of True. Since match() and search() return None when there is no match, you can test whether there was a match with a simple if statement: match = re.search(pattern, string) if match: process(match) The documentation is incorrect, as you point out: “have a boolean value of True” implies that the value is identical to the built-in ‘True’ constant, which is never the case for these objects. I disagree with that interpretation. X has a boolean value of True does not imply that X is necessarily True, but only that bool(X) is True. Is the same way we might say: Lists have a string representation with [ and ] delimiters and , item separators I trust that you wouldn't interprete that as meaning that lists *are* strings. If the documentation said that match objects *are* the boolean value True, then you would be correct, but it doesn't. Your interpretation confuses has for is. To put it another way, X has a boolean value of True is synonymous with any of these alternative ways of saying the same thing: X evaluates like True in a boolean context X is truthy X is a truthy value X is a true-like value X is equivalent to the bool True in boolean contexts X has the quality of being interpreted as true in boolean contexts bool(X) returns True and many other ways of saying the same thing. Instead, the passage above should say “evaluates true in a boolean context”. Would you be so kind as to report a bug to that effect URL:http://bugs.python.org/? I shouldn't bother if I were you :-) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] “has a value of True” versus “evaluates true”
On Wed, Nov 12, 2014 at 01:25:15AM +, Danny Yoo wrote: Just to note; not all programming languages do it this way. Python is fairly permissive in what it allows to be truthy. See: https://plus.google.com/+ShriramKrishnamurthi/posts/4qvvKYC1R8Y for a brief survey of what many other programming languages do. It can be confusing and bug-prone as heck. I strongly disagree, especially since Python has an excellent model for deciding truthiness by default: something versus nothing. Python's treatment of true/false values matches the standard Python philosophy of duck-typing. Rather than require a fixed type of object, say a duck, Python generally allows you to use anything which behaves sufficiently duck-like for your purposes. All Python objects quack like bools, and truthiness is a fundamental property of all Python objects. Unlike some languages, which choose confusing and arbitrary sets of values that count as truthy or falsey, Python encourages a simple distinction, something versus nothing. Values which represent some kind of nothing are falsey: - numeric zeroes, e.g. 0, 0.0, 0j, Decimal(0) - empty containers, e.g. [], (), {}, set(), frozenset() - empty strings, e.g. , u - None Values with represent something are truthy: - non-zero numbers - non-empty containers - non-empty strings - arbitrary objects For myself, I usually want as restrictive an approach as possible with respect to what things are considered truthy. If I'm in a boolean context, I will explicitly make the expression being tested be either True or False, and that's it. Do you duck-type other kinds of values, or do you insist on explicitly forcing everything to be a single kind of object? Do you write len(list(sequence)), or just len(sequence)? If you allow len() to duck-type its argument, why not allow duck-typing bools? That way, I know I won't get into shaky waters. I program in multiple languages: I don't want to spend brain power remembering yet another a truth table about truth. It's your choice to program in multiple languages. Presumably you get some benefit from that. The cost of using different languages is that they are different: they have different semantics, syntax, libraries, they use different idioms, require you to write different code. If you don't like that, don't write code in different languages. Do you complain that it is confusing and bug-prone as heck that the same operation might be spelled any of these ways? len(x) laenge(x) length(x) x.len() x.len size(x) sizeof(x) x len len x x:length() All languages makes choices. Complaining that a specific choice is hard to use, inconsistent with the rest of the language, confusing or bug-prone is okay. Complaining that different languages have different semantics is, well, silly. Different programming languages are different, that's why they're different languages and not all FORTRAN. Anyone who writes: if bool(x): ... in Python instead of just if x is just exposing their own confusion. Think about it: calling bool() explicitly does *nothing* that Python doesn't already do, that's as silly as writing: result = int(23) + float(42.5) If they write this: if bool(x) == True: that's even more foolish. Why stop there? if bool(x) == True == True: if bool(x) == True == True == True: if bool(x) == True == True == True == True: # I never know where to stop... It may be acceptable to specifically accept *only* True and False under some (hopefully rare) circumstances: if x is True: ... elif x is False: ... else: raise TypeError but if you insist in trying to force Python to be a poor emulation of Pascal, one wonders why you don't just use Pascal. To quote: Let your statement be: 'Yes, yes', or no, no': anything beyond these is of evil. Have you stopped abusing small children yet? -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor