Re: [Tutor] Odds and even exercise

2010-03-27 Thread Steven D'Aprano
On Sun, 28 Mar 2010 09:33:23 am yd wrote:

> I find it easy to do all this stuff with list comprehensions, but
> i am a beginner so this might not be the most efficient way to do it
>
> numbers=[]
> for x in range(1,101):
> numbers.append(x)

That certainly isn't efficient! In Python 2.x, this is what it does:

(1) Create an empty list and call it "numbers".
(2) Create a list [1, 2, ... 100]
(3) Set up a for-loop.
(4) Read the first number from the list (1) and call it x.
(5) Add x to the end of numbers.
(6) Read the second number from the list (2) and call it x.
(7) Add x to the end of numbers.
(8) Read the third number from the list and call it x.
(9) Add x to the end of numbers.
...
(202) Read the 100th number from the list and call it x.
(203) Add x to the end of numbers.
(204) Finish up the for-loop.

Better to just say:

(1) Create a list [1, 2, ... 100] and call it "numbers".

numbers = range(1, 101)

In Python 3.x, it is exactly the same except for step 2, which creates a 
lazy range-object which only stores one item at a time. So the solution 
in Python 3.x is to convert it into a list:

numbers = list(range(1, 101))


> > #A way to display all odd numbers
> > odd = numbers[::2]
>
>   instead i do this:
>   odd=[]
>   for y in range(1,101,2):
> odd.append(y)


This does just as much unnecessary work as above.



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Odds and even exercise

2010-03-27 Thread yd
Hi, just started python at Uni and think i am in for a rough ride with zero
> prior experience in programming.
> Anyway my problem that i can't fix my self after googling.
>
> The exercise is to generate a list of odd numbers between 1-100 and the
> same
> for even numbers.
>
> So far this is what i have
> way to display numbers 1 - 100
> numbers = range(100)



> this gives me numbers= range(0,100) as an output in python 3.0
>
   I find it easy to do all this stuff with list comprehensions, but i am a
beginner so this might not be the most efficient way to do it
  numbers=[]
  for x in range(1,101):
numbers.append(x)

> #A way to display all odd numbers
> odd = numbers[::2]
>
  instead i do this:
  odd=[]
  for y in range(1,101,2):
odd.append(y)


> #A way to display all even numbers
>

  even=[]
  for z in range(2,101,2):
even.append(z)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Prime numbers

2010-03-27 Thread Shashwat Anand
>>> [x for x in range(3,int((n**0.5)),2)]
[]
your while loop is an infinite loop. Had you read the range documentations ?

>>> range(3,int((n**0.5)),2)
[]
>>> n**0.5
1.7320508075688772
>>> n = 3
>>> n ** 0.5
1.7320508075688772
>>> int ( n ** 0.5)
1
>>> range ( 3, 1, 2)
[]


On Sun, Mar 28, 2010 at 3:38 AM, yd  wrote:

>
> Having a problem finding the first 1000 prime numbers, here is my code:-
>
> print(2)
> n =3
> counter =1
> while counter <=1000:
>   for x in range(3,int((n**0.5)),2):
> if n%x != 0:
>   print(n)
>   n+=1
>   counter+=1
> else:
>   n+=1
>
> The problem is, it prints 2 and then does nothing, yet if i try and close,
> it says program is still running do you want to kill it, is there a way to
> do this with lists, i know python has a prime function but i am not going to
> use it because i want to solve this problem without 'cheating'.Thanks.
>
>
> ___
> 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] Prime numbers

2010-03-27 Thread yd
Having a problem finding the first 1000 prime numbers, here is my code:-

print(2)
n =3
counter =1
while counter <=1000:
  for x in range(3,int((n**0.5)),2):
if n%x != 0:
  print(n)
  n+=1
  counter+=1
else:
  n+=1

The problem is, it prints 2 and then does nothing, yet if i try and close,
it says program is still running do you want to kill it, is there a way to
do this with lists, i know python has a prime function but i am not going to
use it because i want to solve this problem without 'cheating'.Thanks.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Introduction to modelling with Python

2010-03-27 Thread AG

Hi List

I apologise in advance for the vagueness of this query, but I am looking 
for a decent modern introduction to modelling using Python.  
Specifically, I want something that is a good introduction (i.e. doesn't 
expect one to already be a maths/ statistics or a programming guru) and 
that has an ecology/ environmental science orientation.  The latter is 
desirable but not essential, as I suspect that once one understands the 
process of data abstraction and the other steps involved in modelling 
processes and scenarios, the thinking and skill sets are likely 
transferable.  However, if my assumption about this is incorrect, please 
let me know.


If anyone knows of any resource (book or on-line) with a Python bent, 
please let me know.  I am preparing to begin applications to Ph.D. 
programs and most of what I am interested in doing requires some 
knowledge of modelling and Python also seems to be widely accepted as a 
programming language, so I am happy with that as I am in the process of 
teaching myself Python anyway.


Thanks for any help, advice, etc.

Cheers

AG
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Odds and even exercise

2010-03-27 Thread Kelly Netterville
On Sat, Mar 27, 2010 at 8:55 AM, TJ Dack  wrote:

> Hi, just started python at Uni and think i am in for a rough ride with zero
> prior experience in programming.
> Anyway my problem that i can't fix my self after googling.
>
> The exercise is to generate a list of odd numbers between 1-100 and the
> same for even numbers.
>
> So far this is what i have CODE: SELECT 
> ALL #A
> way to display numbers 1 - 100
> numbers = range(100)
> #A way to display all odd numbers
> odd = numbers[::2]
> #A way to display all even numbers
>
>
> I can't find a way to easily list the even numbers, i really need a easier
> way to find answers my self but using the help docs in idle didn't get me
> far, any tips that you guys have when you come across something you can't
> solve?
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
Sorry, didn't use 'reply all' in my initial response. Sending again to
entire list.

This is an exercise that is more about thinking programatically than it is
about writing Python code. However, consider that even numbers are  evenly
divisible by 2 and odds are not. Now try to use the basics you've learned so
far about Python to solve your problem. A while loop, if, and print
statement along with basic mathematic operators are all you really need to
solve this.

Good luck!
Kelly
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Odds and even exercise

2010-03-27 Thread R. Alan Monroe
> odd = numbers[::2]

> I can't find a way to easily list the even numbers,

Hint: You can designate a start number before the first colon.

Alan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Odds and even exercise

2010-03-27 Thread Steven D'Aprano
On Sat, 27 Mar 2010 11:55:01 pm TJ Dack wrote:
> Hi, just started python at Uni and think i am in for a rough ride
> with zero prior experience in programming.
> Anyway my problem that i can't fix my self after googling.
>
> The exercise is to generate a list of odd numbers between 1-100 and
> the same for even numbers.
>
> So far this is what i haveCODE: SELECT
> ALL#A
> way to display numbers 1 - 100
> numbers = range(100)
> #A way to display all odd numbers
> odd = numbers[::2]
> #A way to display all even numbers

Nice try! Sadly you *just* missed getting it though. Here's an example, 
using 1-10 instead of 1-100:

>>> numbers = range(10)
>>> odd = numbers[::2]
>>> print numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print odd
[0, 2, 4, 6, 8]

So you have two small errors: you have the numbers 0-99 instead of 
1-100, and consequently the list you called "odd" is actually even. 
This is what programmers call an "off by one" error. Don't worry, even 
the best programmers make them!

The first thing to remember is that Python's range function works on 
a "half-open" interval. That is, it includes the starting value, but 
excludes the ending value. Also, range defaults to a starting value of 
0, but you need a starting value of 1. So you need:

numbers = range(1, 101)  # gives [1, 2, 3, ... 99, 100]

Now your odd list will work:

odd = numbers[::2]  # gives [1, 3, 5, ... 99]

How to get even numbers? Consider:

Position: 0, 1, 2, 3, 4, 5, ...  # Python starts counting at zero
Number:   1, 2, 3, 4, 5, 6, ...  # The values of list "numbers"
Odd/Even: O, E, O, E, O, E, ...

Every second number is even, just like for the odd numbers, but instead 
of starting at position zero you need to start at position one. Do you 
know how to do that?

Hint: numbers[::2] means:

start at 0, finish at the end of the list, and return every 2nd value.

You want:

start at 1, finish at the end of the list, and return every 2nd value.



> I can't find a way to easily list the even numbers, i really need a
> easier way to find answers my self but using the help docs in idle
> didn't get me far, any tips that you guys have when you come across
> something you can't solve?

So far you seem to be doing well: think about the problem, google it, 
think about it some more, ask for help.

Also, try solving the problem with pencil and paper first, then repeat 
what you did using Python.

Finally, experiment! Open up the Python interpreter, and try things. See 
what they do. See if you can predict what they will do before you do 
them. For example, what would these give?

range(1, 101, 3)
range(2, 101, 4)

Try it yourself and see if you predicted correctly.



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Odds and even exercise

2010-03-27 Thread Shashwat Anand
On Sat, Mar 27, 2010 at 6:25 PM, TJ Dack  wrote:

> Hi, just started python at Uni and think i am in for a rough ride with zero
> prior experience in programming.
> Anyway my problem that i can't fix my self after googling.
>
> The exercise is to generate a list of odd numbers between 1-100 and the
> same for even numbers.
>
> So far this is what i have CODE: SELECT 
> ALL #A
> way to display numbers 1 - 100
> numbers = range(100)
>

Are you sure it displays 1 - 100. ?

>  #A way to display all odd numbers
> odd = numbers[::2]
>

Are you sure this shows odd :-/ ?
Had you read 'range' documentation ?


>  #A way to display all even numbers
>
>
> I can't find a way to easily list the even numbers, i really need a easier
> way to find answers my self but using the help docs in idle didn't get me
> far, any tips that you guys have when you come across something you can't
> solve?
> ___
> 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] Odds and even exercise

2010-03-27 Thread TJ Dack
Hi, just started python at Uni and think i am in for a rough ride with zero
prior experience in programming.
Anyway my problem that i can't fix my self after googling.

The exercise is to generate a list of odd numbers between 1-100 and the same
for even numbers.

So far this is what i haveCODE: SELECT
ALL#A
way to display numbers 1 - 100
numbers = range(100)
#A way to display all odd numbers
odd = numbers[::2]
#A way to display all even numbers


I can't find a way to easily list the even numbers, i really need a easier
way to find answers my self but using the help docs in idle didn't get me
far, any tips that you guys have when you come across something you can't
solve?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] "IOError: [Errno 32] Broken pipe" when running python with cron (alternatives?)

2010-03-27 Thread Karjer Jdfjdf
I have made an extensive script that runs fine when started from the command 
line or IDLE.

When I try to run it with cron it keeps giving errors:

Error in sys.exitfunc:
Traceback (most recent call last):
  File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/usr/lib/python2.6/logging/__init__.py", line 1508, in shutdown
    h.flush()
  File "/usr/lib/python2.6/logging/__init__.py", line 754, in flush
    self.stream.flush()
IOError: [Errno 32] Broken pipe


The script has the following structure:
1. retrieves data from database 1
2. modifies the data
3. inserts the modified data in another database

In a previous script I solved this problem with directing the stdout and stderr 
to /dev/null, but doing this isn't possible because I use pickle and write some 
data to files. It seems to have anything to do with the stdout/stderr and cron, 
but after a lot of googling I don't have any clues how to fix this.

How can I solve this? Does anybody know how to solve this or is there a 
python-friendly alternative to cron that I can use.



  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess

2010-03-27 Thread Sander Sweers
On 27 March 2010 09:30, David Abbott  wrote:
> Here is an example using subprocess.call
> http://dwabbott.com/code/index8.html
>
> and some more here with subprocess.Popen
> http://asterisklinks.com/wiki/doku.php?id=wiki:subprocess

On top of that we have the excelent PyMOTW from Doug on subprocess.
http://blog.doughellmann.com/2007/07/pymotw-subprocess.html

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess

2010-03-27 Thread kevin parks

Thanks David. Those are excellent short clear examples. I will look those over. 
Super! Thanks for that.

-kp

On Mar 27, 2010, at 5:30 PM, David Abbott wrote:

> On Sat, 2010-03-27 at 16:55 +0900, kevin parks wrote:
> 

> Here is an example using subprocess.call
> http://dwabbott.com/code/index8.html
> 
> and some more here with subprocess.Popen
> http://asterisklinks.com/wiki/doku.php?id=wiki:subprocess
> 
> HTH
> David
> 
> 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python magazine

2010-03-27 Thread Dave Angel



Lowell Tackett wrote:
>From the virtual desk of Lowell Tackett  



--- On Fri, 3/26/10, Benno Lang  wrote:

From: Benno Lang 
Subject: Re: [Tutor] python magazine
To: "Lowell Tackett" 
Cc: tutor@python.org, "Bala subramanian" 
Date: Friday, March 26, 2010, 8:38 PM

On 27 March 2010 00:33, Lowell Tackett  wrote:
  

The Python Magazine people have now got a Twitter site--which includes a 
perhaps [telling] misspelling.


Obviously that's why they're looking for a chief editor - maybe it's
even a deliberate ploy.

I'm not sure if this affects others, but to me your replies appear
inside the quoted section of your mail, rather than beneath it. Would
you mind writing plain text emails to avoid this issue?

Thanks,
benno

Like this...?


  
No, there's still a problem.  You'll notice in this message that there 
are ">" symbols in front of your lines and benno's, and ">>" symbols in 
front of Lowell's.  (Some email readers will turn the > into vertical 
bar, but the effect is the same).  Your email program should be adding 
those upon a reply, so that your own message has one less > than the one 
to which you're replying.  Then everyone reading can see who wrote what, 
based on how many ">" or bars precede the respective lines.  Quotes from 
older messages have more of them.


Are you using "Reply-All" in your email program?  Or are you 
constructing a new message with copy/paste?


What email are you using?  Maybe it's a configuration setting somebody 
could help with.


DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess

2010-03-27 Thread David Abbott
On Sat, 2010-03-27 at 16:55 +0900, kevin parks wrote:
> I tried readings some toots and tried reading alan's thing. I just still 
> can't grok how to use subprocess.
> 
> I am trying to call sox (fun fact: an early contributer to sox was none other 
> than Guido van Rossum)
> 
> In the old days you would just use os i guess, like:
> 
> import os
> os.system('sox -V3 -D -S St.01.aif -b16 Stout-01.aif rate -s -v 44100')
> 
> to call a unix executable that you would ordinarily run from the terminal.
> 
> what would the equivalent of this in python's new subprocess be? perhaps if i 
> saw an example it would click..
> 
> additionally.. sox is a sound conversion tool. I plan to batch process a 
> bunch of files. Will subprocess start all the jobs as it finds the files? or 
> will it process one job and que the next? If it opened a thread and started a 
> bunch of jobs it would likely bog down the system no?
> 
> anyway  I have the os walk and other stuff that i am working on and i was 
> hoping to get some help on subprocess. There are a few pages on subprocess 
> but they all might just as well be in chinese and none of them, none that i 
> can see are equivalent to what i am trying to do (they all seem to be doing 
> os-y things)
> 
> An example might help. Not sure why i am finding this so hard to get my head 
> around.

Here is an example using subprocess.call
http://dwabbott.com/code/index8.html

and some more here with subprocess.Popen
http://asterisklinks.com/wiki/doku.php?id=wiki:subprocess

HTH
David


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] subprocess

2010-03-27 Thread kevin parks
I tried readings some toots and tried reading alan's thing. I just still can't 
grok how to use subprocess.

I am trying to call sox (fun fact: an early contributer to sox was none other 
than Guido van Rossum)

In the old days you would just use os i guess, like:

import os
os.system('sox -V3 -D -S St.01.aif -b16 Stout-01.aif rate -s -v 44100')

to call a unix executable that you would ordinarily run from the terminal.

what would the equivalent of this in python's new subprocess be? perhaps if i 
saw an example it would click..

additionally.. sox is a sound conversion tool. I plan to batch process a bunch 
of files. Will subprocess start all the jobs as it finds the files? or will it 
process one job and que the next? If it opened a thread and started a bunch of 
jobs it would likely bog down the system no?

anyway  I have the os walk and other stuff that i am working on and i was 
hoping to get some help on subprocess. There are a few pages on subprocess but 
they all might just as well be in chinese and none of them, none that i can see 
are equivalent to what i am trying to do (they all seem to be doing os-y things)

An example might help. Not sure why i am finding this so hard to get my head 
around.






___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor