Re: basic Python question
On 2020-05-08 21:19, joseph pareti wrote: yet, something is still unclear; in Python you can do things like: *clf0.fit(X_train,y_train)* which is not the way I programmed in other languages where a left-hand side and a right hand side is required. All it's doing is performing the calculation and then storing the result in the object itself. Most other languages can do that too. Am Fr., 8. Mai 2020 um 21:52 Uhr schrieb joseph pareti mailto:joeparet...@gmail.com>>: yes, it is random forest classifier from scikit learn. Thank you. Am Fr., 8. Mai 2020 um 21:50 Uhr schrieb MRAB mailto:pyt...@mrabarnett.plus.com>>: On 2020-05-08 20:02, joseph pareti wrote: > In general I prefer doing: > > > X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42) >clf = RandomForestClassifier(n_estimators = 100, max_depth= > None) *clf_f = clf.fit(X_train, y_train)* predicted_labels = clf_f.predict( > X_test) score = clf.score(X_test, y_test) score1 = metrics.accuracy_score( > y_test, predicted_labels) > > > rather than: > > X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, > random_state=42) clf0=RandomForestClassifier(n_estimators=100, max_depth= > None) *clf0.fit(X_train, y_train)* y_pred =clf0.predict(X_test) score= > metrics.accuracy_score(y_test, y_pred) > > > Are the two codes really equivalent? > You didn't give any context and say what package you're using! After searching for "RandomForestClassifier", I'm guessing that you're using scikit. From the documentation here: https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier.fit it says: Returns: self : object so it looks like clf.fit(...) returns clf. That being the case, then, yes, they're equivalent. -- https://mail.python.org/mailman/listinfo/python-list
Re: basic Python question
yet, something is still unclear; in Python you can do things like: *clf0.fit(X_train, y_train)* which is not the way I programmed in other languages where a left-hand side and a right hand side is required. Am Fr., 8. Mai 2020 um 21:52 Uhr schrieb joseph pareti < joeparet...@gmail.com>: > yes, it is random forest classifier from scikit learn. Thank you. > > Am Fr., 8. Mai 2020 um 21:50 Uhr schrieb MRAB >: > >> On 2020-05-08 20:02, joseph pareti wrote: >> > In general I prefer doing: >> > >> > >> > X_train, X_test, y_train, y_test = train_test_split(X, y, >> test_size=0.33, random_state=42) >> >clf = RandomForestClassifier(n_estimators = 100, max_depth= >> > None) *clf_f = clf.fit(X_train, y_train)* predicted_labels = >> clf_f.predict( >> > X_test) score = clf.score(X_test, y_test) score1 = >> metrics.accuracy_score( >> > y_test, predicted_labels) >> > >> > >> > rather than: >> > >> > X_train, X_test, y_train, y_test = train_test_split(X, y, >> test_size=0.33, >> > random_state=42) clf0=RandomForestClassifier(n_estimators=100, >> max_depth= >> > None) *clf0.fit(X_train, y_train)* y_pred =clf0.predict(X_test) score= >> > metrics.accuracy_score(y_test, y_pred) >> > >> > >> > Are the two codes really equivalent? >> > >> You didn't give any context and say what package you're using! >> >> After searching for "RandomForestClassifier", I'm guessing that you're >> using scikit. >> >> From the documentation here: >> >> >> https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier.fit >> >> it says: >> >> Returns: self : object >> >> so it looks like clf.fit(...) returns clf. >> >> That being the case, then, yes, they're equivalent. >> -- >> https://mail.python.org/mailman/listinfo/python-list >> > > > -- > Regards, > Joseph Pareti - Artificial Intelligence consultant > Joseph Pareti's AI Consulting Services > https://www.joepareti54-ai.com/ > cell +49 1520 1600 209 > cell +39 339 797 0644 > -- Regards, Joseph Pareti - Artificial Intelligence consultant Joseph Pareti's AI Consulting Services https://www.joepareti54-ai.com/ cell +49 1520 1600 209 cell +39 339 797 0644 -- https://mail.python.org/mailman/listinfo/python-list
Re: basic Python question
yes, it is random forest classifier from scikit learn. Thank you. Am Fr., 8. Mai 2020 um 21:50 Uhr schrieb MRAB : > On 2020-05-08 20:02, joseph pareti wrote: > > In general I prefer doing: > > > > > > X_train, X_test, y_train, y_test = train_test_split(X, y, > test_size=0.33, random_state=42) > >clf = RandomForestClassifier(n_estimators = 100, max_depth= > > None) *clf_f = clf.fit(X_train, y_train)* predicted_labels = > clf_f.predict( > > X_test) score = clf.score(X_test, y_test) score1 = > metrics.accuracy_score( > > y_test, predicted_labels) > > > > > > rather than: > > > > X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, > > random_state=42) clf0=RandomForestClassifier(n_estimators=100, max_depth= > > None) *clf0.fit(X_train, y_train)* y_pred =clf0.predict(X_test) score= > > metrics.accuracy_score(y_test, y_pred) > > > > > > Are the two codes really equivalent? > > > You didn't give any context and say what package you're using! > > After searching for "RandomForestClassifier", I'm guessing that you're > using scikit. > > From the documentation here: > > > https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier.fit > > it says: > > Returns: self : object > > so it looks like clf.fit(...) returns clf. > > That being the case, then, yes, they're equivalent. > -- > https://mail.python.org/mailman/listinfo/python-list > -- Regards, Joseph Pareti - Artificial Intelligence consultant Joseph Pareti's AI Consulting Services https://www.joepareti54-ai.com/ cell +49 1520 1600 209 cell +39 339 797 0644 -- https://mail.python.org/mailman/listinfo/python-list
Re: basic Python question
On 2020-05-08 20:02, joseph pareti wrote: In general I prefer doing: X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42) >clf = RandomForestClassifier(n_estimators = 100, max_depth= None) *clf_f = clf.fit(X_train, y_train)* predicted_labels = clf_f.predict( X_test) score = clf.score(X_test, y_test) score1 = metrics.accuracy_score( y_test, predicted_labels) rather than: X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42) clf0=RandomForestClassifier(n_estimators=100, max_depth= None) *clf0.fit(X_train, y_train)* y_pred =clf0.predict(X_test) score= metrics.accuracy_score(y_test, y_pred) Are the two codes really equivalent? You didn't give any context and say what package you're using! After searching for "RandomForestClassifier", I'm guessing that you're using scikit. From the documentation here: https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier.fit it says: Returns: self : object so it looks like clf.fit(...) returns clf. That being the case, then, yes, they're equivalent. -- https://mail.python.org/mailman/listinfo/python-list
Re: Basic python question
Jagga Soorma wrote: > Thanks again Aldwin. This seems to work, guess it is the set that is > flipping the numbers: > > x,y = (output.split()) The parens on the right are superfluous: >>> a, b = "foo bar".split() >>> a 'foo' >>> b 'bar' > inode_cmd = "/bin/df --output=pcent,ipcent /var| grep -v Use | tr '%' ' '" > output = subprocess.check_output( inode_cmd, > stderr=subprocess.STDOUT, shell=True ) You may also do the post-processing in Python, which allows you to avoid the shell completely: >>> output = subprocess.check_output(["/bin/df", "--output=pcent,ipcent", "/var"]) >>> output b'Use% IUse%\n 85% 16%\n' >>> a, b = output.replace(b"%", b"").split()[-2:] >>> a b'85' >>> b b'16' If you want integers: >>> a, b = [int(v) for v in output.replace(b"%", b"").split()[-2:]] >>> a 85 >>> b 16 -- https://mail.python.org/mailman/listinfo/python-list
Re: Basic python question
Thanks again Aldwin. This seems to work, guess it is the set that is flipping the numbers: x,y = (output.split()) Much appreciated! On Wed, Oct 2, 2019 at 9:19 PM Aldwin Pollefeyt wrote: > > Seems to work also: > > >>> [x,y] = output.split() > > On Thu, Oct 3, 2019 at 12:17 PM Aldwin Pollefeyt > wrote: >> >> Oh, sorry .. please try this: >> >> >>> x,y = tuple(output.split()) >> >> On Thu, Oct 3, 2019 at 12:11 PM Jagga Soorma wrote: >>> >>> Thanks Aldwin that helps but it looks like it is reversing the numbers >>> for some reason: >>> >>> the df command returns the following: >>> 7 2 >>> >>> I used your example and did: >>> x,y = set(output.split()) >>> >>> My assumption would be that x should be 7 and y should be 2. However, >>> when I print x and y it seems to be reversed (x is 2 and y is 7). Am >>> I missing something? >>> >>> Thanks >>> >>> On Wed, Oct 2, 2019 at 8:49 PM Aldwin Pollefeyt >>> wrote: >>> > >>> > You could use: >>> > >>> > >>> x, y = set(output.split()) >>> > >>> > On Thu, Oct 3, 2019 at 11:44 AM Jagga Soorma wrote: >>> >> >>> >> Hello, >>> >> >>> >> I am new to python and trying to do some basic things with python. I >>> >> am writing a script that runs a df command and I need parts of that >>> >> output saved in 2 different variables. Is this something that can be >>> >> done? I can do this by running multiple df commands but would prefer >>> >> to make only one call: >>> >> >>> >> -- >>> >> inode_cmd = "/bin/df --output=pcent,ipcent /var| grep -v Use | tr '%' ' >>> >> '" >>> >> output = subprocess.check_output( inode_cmd, >>> >> stderr=subprocess.STDOUT, shell=True ) >>> >> -- >>> >> >>> >> But this would end up giving me the following: >>> >> >>> >> #df --output=pcent,ipcent /var | grep -v Use | tr '%' ' ' >>> >>5 1 >>> >> >>> >> I would like variable x to be 5 and variable y to be 1. Is there a >>> >> easier way to do this? >>> >> >>> >> Thanks in advance for your guidance. >>> >> -- >>> >> https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Basic python question
Seems to work also: >>> [x,y] = output.split() On Thu, Oct 3, 2019 at 12:17 PM Aldwin Pollefeyt wrote: > Oh, sorry .. please try this: > > >>> x,y = tuple(output.split()) > > On Thu, Oct 3, 2019 at 12:11 PM Jagga Soorma wrote: > >> Thanks Aldwin that helps but it looks like it is reversing the numbers >> for some reason: >> >> the df command returns the following: >> 7 2 >> >> I used your example and did: >> x,y = set(output.split()) >> >> My assumption would be that x should be 7 and y should be 2. However, >> when I print x and y it seems to be reversed (x is 2 and y is 7). Am >> I missing something? >> >> Thanks >> >> On Wed, Oct 2, 2019 at 8:49 PM Aldwin Pollefeyt >> wrote: >> > >> > You could use: >> > >> > >>> x, y = set(output.split()) >> > >> > On Thu, Oct 3, 2019 at 11:44 AM Jagga Soorma wrote: >> >> >> >> Hello, >> >> >> >> I am new to python and trying to do some basic things with python. I >> >> am writing a script that runs a df command and I need parts of that >> >> output saved in 2 different variables. Is this something that can be >> >> done? I can do this by running multiple df commands but would prefer >> >> to make only one call: >> >> >> >> -- >> >> inode_cmd = "/bin/df --output=pcent,ipcent /var| grep -v Use | tr '%' >> ' '" >> >> output = subprocess.check_output( inode_cmd, >> >> stderr=subprocess.STDOUT, shell=True ) >> >> -- >> >> >> >> But this would end up giving me the following: >> >> >> >> #df --output=pcent,ipcent /var | grep -v Use | tr '%' ' ' >> >>5 1 >> >> >> >> I would like variable x to be 5 and variable y to be 1. Is there a >> >> easier way to do this? >> >> >> >> Thanks in advance for your guidance. >> >> -- >> >> https://mail.python.org/mailman/listinfo/python-list >> > -- https://mail.python.org/mailman/listinfo/python-list
Re: Basic python question
Oh, sorry .. please try this: >>> x,y = tuple(output.split()) On Thu, Oct 3, 2019 at 12:11 PM Jagga Soorma wrote: > Thanks Aldwin that helps but it looks like it is reversing the numbers > for some reason: > > the df command returns the following: > 7 2 > > I used your example and did: > x,y = set(output.split()) > > My assumption would be that x should be 7 and y should be 2. However, > when I print x and y it seems to be reversed (x is 2 and y is 7). Am > I missing something? > > Thanks > > On Wed, Oct 2, 2019 at 8:49 PM Aldwin Pollefeyt > wrote: > > > > You could use: > > > > >>> x, y = set(output.split()) > > > > On Thu, Oct 3, 2019 at 11:44 AM Jagga Soorma wrote: > >> > >> Hello, > >> > >> I am new to python and trying to do some basic things with python. I > >> am writing a script that runs a df command and I need parts of that > >> output saved in 2 different variables. Is this something that can be > >> done? I can do this by running multiple df commands but would prefer > >> to make only one call: > >> > >> -- > >> inode_cmd = "/bin/df --output=pcent,ipcent /var| grep -v Use | tr '%' ' > '" > >> output = subprocess.check_output( inode_cmd, > >> stderr=subprocess.STDOUT, shell=True ) > >> -- > >> > >> But this would end up giving me the following: > >> > >> #df --output=pcent,ipcent /var | grep -v Use | tr '%' ' ' > >>5 1 > >> > >> I would like variable x to be 5 and variable y to be 1. Is there a > >> easier way to do this? > >> > >> Thanks in advance for your guidance. > >> -- > >> https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Basic python question
Thanks Aldwin that helps but it looks like it is reversing the numbers for some reason: the df command returns the following: 7 2 I used your example and did: x,y = set(output.split()) My assumption would be that x should be 7 and y should be 2. However, when I print x and y it seems to be reversed (x is 2 and y is 7). Am I missing something? Thanks On Wed, Oct 2, 2019 at 8:49 PM Aldwin Pollefeyt wrote: > > You could use: > > >>> x, y = set(output.split()) > > On Thu, Oct 3, 2019 at 11:44 AM Jagga Soorma wrote: >> >> Hello, >> >> I am new to python and trying to do some basic things with python. I >> am writing a script that runs a df command and I need parts of that >> output saved in 2 different variables. Is this something that can be >> done? I can do this by running multiple df commands but would prefer >> to make only one call: >> >> -- >> inode_cmd = "/bin/df --output=pcent,ipcent /var| grep -v Use | tr '%' ' '" >> output = subprocess.check_output( inode_cmd, >> stderr=subprocess.STDOUT, shell=True ) >> -- >> >> But this would end up giving me the following: >> >> #df --output=pcent,ipcent /var | grep -v Use | tr '%' ' ' >>5 1 >> >> I would like variable x to be 5 and variable y to be 1. Is there a >> easier way to do this? >> >> Thanks in advance for your guidance. >> -- >> https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Basic python question
You could use: >>> x, y = set(output.split()) On Thu, Oct 3, 2019 at 11:44 AM Jagga Soorma wrote: > Hello, > > I am new to python and trying to do some basic things with python. I > am writing a script that runs a df command and I need parts of that > output saved in 2 different variables. Is this something that can be > done? I can do this by running multiple df commands but would prefer > to make only one call: > > -- > inode_cmd = "/bin/df --output=pcent,ipcent /var| grep -v Use | tr '%' ' '" > output = subprocess.check_output( inode_cmd, > stderr=subprocess.STDOUT, shell=True ) > -- > > But this would end up giving me the following: > > #df --output=pcent,ipcent /var | grep -v Use | tr '%' ' ' >5 1 > > I would like variable x to be 5 and variable y to be 1. Is there a > easier way to do this? > > Thanks in advance for your guidance. > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: basic python question about for loop
jmDesktop [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] So what is n and x in the first iteration? Sorry. I'm trying. Remember how Python's range operator works. range(n, x) constructs a list that consists of all elements starting with n and up to, but /not including/, x. For example, range(3, 7) constructs the list [3, 4, 5, 6]. So what happens if you try to construct the list range(2, 2)? In this case, Python will construct an empty list -- this is its interpretation of up to, but not including n for an argument like range(n, n). This is precisely what is happening in the first iteration of the first enclosing for loop. Trace through the code; see that 2 is bound to n in the first iteration; see that the inner loop then tries to construct the list range(2, n), which is range(2, 2), which is an empty list. And a for x in [] statement will not execute even once. As it happens, your loop is perfectly correct. You are testing for divisors for a number excluding 1 and the number itself. For the number 2, the number of possible divisors satisfying this condition is an empty set. So 2 is, quite correctly, adjudged to be prime. -- http://mail.python.org/mailman/listinfo/python-list
Re: basic python question about for loop
jmDesktop wrote: [...] So what is n and x in the first iteration? Sorry. I'm trying. Somewhat feebly, if you don't mind my saying so, but don't worry. The usual way to proceed in the face of such ignorance is to insert some form of output that will tell you the answer to your question. So: for n in range(2, 20): ... print range(2, n) ... for x in range(2, n): ... if n % x == 0: ...print n, 'equals', x, '*', n/x ...break ... else: ... print n, is prime ... [] 2 is prime [2] 3 is prime [2, 3] 4 equals 2 * 2 [2, 3, 4] 5 is prime [2, 3, 4, 5] 6 equals 2 * 3 [2, 3, 4, 5, 6] 7 is prime [2, 3, 4, 5, 6, 7] 8 equals 2 * 4 [2, 3, 4, 5, 6, 7, 8] 9 equals 3 * 3 and so on! This is the value of the interactive interpreter. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: basic python question about for loop
jmDesktop schrieb: From the Python.org tutorial: for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print n, 'equals', x, '*', n/x ... break ... else: ... # loop fell through without finding a factor ... print n, 'is a prime number' ... 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3 first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong? Why did it fall through? print out what range(2, n) for n == 2 is. And if you didn't know - 2 *IS* a prime. Diez -- http://mail.python.org/mailman/listinfo/python-list
RE: basic python question about for loop
-Original Message- From: [EMAIL PROTECTED] [mailto:python- [EMAIL PROTECTED] On Behalf Of jmDesktop Sent: Wednesday, April 09, 2008 4:51 PM To: python-list@python.org Subject: basic python question about for loop From the Python.org tutorial: for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print n, 'equals', x, '*', n/x ... break ... else: ... # loop fell through without finding a factor ... print n, 'is a prime number' ... 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3 first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong? Why did it fall through? a) 2 is prime, so nothing is wrong. b) Range isn't doing what you think it's doing: print range(2,2) [] print range(2,3) [2] print range(2,4) [2, 3] print range(2,5) [2, 3, 4] print range(1,1) [] print range(1,2) [1] print range(1,3) [1, 2] * The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA622 -- http://mail.python.org/mailman/listinfo/python-list
Re: basic python question about for loop
jmDesktop wrote: From the Python.org tutorial: for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print n, 'equals', x, '*', n/x ... break ... else: ... # loop fell through without finding a factor ... print n, 'is a prime number' ... 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3 first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong? Why did it fall through? range(2, 2) [] The loop body executes zero times. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: basic python question about for loop
On Apr 9, 4:58 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote: jmDesktop schrieb: From the Python.org tutorial: for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print n, 'equals', x, '*', n/x ... break ... else: ... # loop fell through without finding a factor ... print n, 'is a prime number' ... 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3 first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong? Why did it fall through? print out what range(2, n) for n == 2 is. And if you didn't know - 2 *IS* a prime. Diez- Hide quoted text - - Show quoted text - I do not understand. -- http://mail.python.org/mailman/listinfo/python-list
Re: basic python question about for loop
On Apr 9, 4:59 pm, Reedick, Andrew [EMAIL PROTECTED] wrote: -Original Message- From: [EMAIL PROTECTED] [mailto:python- [EMAIL PROTECTED] On Behalf Of jmDesktop Sent: Wednesday, April 09, 2008 4:51 PM To: [EMAIL PROTECTED] Subject: basic python question about for loop From the Python.org tutorial: for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print n, 'equals', x, '*', n/x ... break ... else: ... # loop fell through without finding a factor ... print n, 'is a prime number' ... 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3 first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong? Why did it fall through? a) 2 is prime, so nothing is wrong. b) Range isn't doing what you think it's doing: print range(2,2) [] print range(2,3) [2] print range(2,4) [2, 3] print range(2,5) [2, 3, 4] print range(1,1) [] print range(1,2) [1] print range(1,3) [1, 2] * The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA622- Hide quoted text - - Show quoted text - So what is n and x in the first iteration? Sorry. I'm trying. -- http://mail.python.org/mailman/listinfo/python-list
RE: basic python question about for loop
-Original Message- From: [EMAIL PROTECTED] [mailto:python- [EMAIL PROTECTED] On Behalf Of jmDesktop Sent: Wednesday, April 09, 2008 5:04 PM To: python-list@python.org Subject: Re: basic python question about for loop for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print n, 'equals', x, '*', n/x ... break ... else: ... # loop fell through without finding a factor ... print n, 'is a prime number' ... first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong? Why did it fall through? So what is n and x in the first iteration? Sorry. I'm trying. You're never getting to n and x in the first iteration, because the 'for x in range(2, n)' loop isn't looping. This: for x in range(2, n) is equivalent in C/Perl/etc. to: for(x=2; xn; x++) which for the first iteration is: for(x=2; x2; x++) Since (2 2) is false, you never call 'if n %x == 0:' in the first iteration. Or to put it another way: Range(2, n) starts at 2, and stops _before_ n. Range(2, n) starts at 2, and stops _before_ 2. * The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA622 -- http://mail.python.org/mailman/listinfo/python-list
Re: basic python question about for loop
jmDesktop schrieb: On Apr 9, 4:58 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote: jmDesktop schrieb: From the Python.org tutorial: for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print n, 'equals', x, '*', n/x ... break ... else: ... # loop fell through without finding a factor ... print n, 'is a prime number' ... 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3 first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong? Why did it fall through? print out what range(2, n) for n == 2 is. And if you didn't know - 2 *IS* a prime. Diez- Hide quoted text - - Show quoted text - I do not understand. for variable in sequence loops over a sequence. And of course it doens't if the sequence is empty, because you can't loop over something that is empty, can't you? and range(2,2) is the empty sequence. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: basic python question about for loop
|So what is n and x in the first iteration? Sorry. I'm trying. When n == 2, the inner loop executes 0 times (the length of range(2,n)) and then falls thru to the else clause, printing the correct answer. -- http://mail.python.org/mailman/listinfo/python-list