[Tutor] What has Editor X got that PyWin32 hasn't?

2008-08-13 Thread kinuthiA muchanE



On Wed, 2008-08-13 at 16:59 +0200, [EMAIL PROTECTED] wrote:
 
 Message: 5
 Date: Wed, 13 Aug 2008 09:58:52 -0500
 From: W W [EMAIL PROTECTED]
 Subject: Re: [Tutor] What has Editor X got that PyWin32 hasn't?
 To: Dick Moores [EMAIL PROTECTED]
 Cc: Python Tutor List tutor@python.org
 Message-ID:
 [EMAIL PROTECTED]
 Content-Type: text/plain; charset=iso-8859-1
 
 On Wed, Aug 13, 2008 at 9:42 AM, Dick Moores [EMAIL PROTECTED] wrote:
 
 
  Since downloading vim 7.2 yesterday, I've had some trouble
 distinguishing
  vim and gvim (both were included). Can you help me out? gvim is
 GUI-vim, I
  think. Isn't that what I want to learn? Is gvim a cut-down version
 of vim,
  but enables you to use your mouse ( :set mouse=a)?

You can subscribe to vim mailing list by sending a blank email to
[EMAIL PROTECTED] It is very friendly even for beginners, Bram
Moolenaar, also gets to answer your questions sometimes! ;)
 
 
 As far as I know/have used, gvim is simply vim, but for lack of a
 better
 comparison, in its own terminal. The same thing you would have if you
 were
 to type vim at the command line.
 
 Honestly, the only thing I know how to do with the mouse in gvim is
 paste
 (middle click in linux). Or access the menus. That's one of the main
 reasons
 I /use/ vim - so I don't have to touch the mouse :)
 
 As far as any keyboard commands that I use, I've not seen any
 difference
 between vim, vi, and gvim. The main difference is syntax highlighting.
 
 -Wayne
 
 
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 53, Issue 110

2008-07-31 Thread kinuthiA muchanE


 
 Message: 5
 Date: Thu, 31 Jul 2008 09:07:22 -0400
 From: S Python [EMAIL PROTECTED]
 Subject: [Tutor] Reading List from File
 To: tutor@python.org
 Message-ID:
 [EMAIL PROTECTED]
 Content-Type: text/plain; charset=iso-8859-1
 
 Hi Everyone,
 
 I am trying to read a comma-delimitted list (aaa,bbb,ccc) from a
 text
 file and assign those values to a list, x, such that:
 
 x = [aaa, bbb, ccc]
 
 The code that I have come up with looks like this:
 
  x = []
  f = open(r'c:\test.txt', 'r')
  x.extend(f.readlines())
  x
 ['aaa,bbb,ccc']
 
 If you look closely, there is an extra pair of single quotes (') that
 encapsulates the string.  Therefore, len(x) returns 1, instead of 3.
 Is
 there a function to separate this list out?  I hope my question
 makes
 sense.
I think you are better off using the csv module. If you have a comma
separated file you could...

import csv
reader = csv.reader(open(some.csv, rb))
for row in reader:
print row
I yanked this straight out of the Python Reference Library :)
 
 Thanks in advance.
 
 Samir
 -- next part --
 An HTML attachment was scrubbed...
 URL:
 http://mail.python.org/pipermail/tutor/attachments/20080731/9f329f25/attachment-0001.htm
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 53, Issue 99

2008-07-29 Thread kinuthiA muchanE

On Tue, 2008-07-29 at 01:12 +0200, [EMAIL PROTECTED] wrote:
 Message: 3
 Date: Mon, 28 Jul 2008 13:26:13 -0500
 From: Daniel Sarmiento [EMAIL PROTECTED]
 Subject: Re: [Tutor] Memory error - how to manage large data sets?
 To: tutor@python.org
 Message-ID:
 [EMAIL PROTECTED]
 Content-Type: text/plain; charset=ISO-8859-1
 
 Hi
 
 I tried to run your code and checked (with top) the memory ussage and
 it uses more than 2 Gb of memory.
 
 I tried to modify the code a little bit to use less memory and came up
 with this:
 
 fib = {0:0,1:1}
 even = []
 
 def fibonacci(x,y):
return x+y
 
 for j in xrange (2,100):
 i = fib[j-1] + fib[j-2]
 if i % 2 == 0:
 even.append(i)
 fib = {j-1:fib[j-1], j:i}
 
 total = reduce(fibonacci,even)
 print total
 
 First, I replaced range with xrange.
 I figured that you only need the last two values in the fibonnaci
 series to calculate the next value, so I replaced the fib list with a
 dictionary to only store the last two values instead of the whole
 series.
 
 It looks like the progam still hangs and I did not notice any memory
 imrovements when running it with 1 000 000
 
 Am I wrong thinking that the modifications I made help use less
 memory?
 
I have realised that when you need to work with large numbers, lists are
slow and tend to gobble up bytes. Another thing try to be as simple as
possible :). This is how I would approach the problem, without using
lists. It produces the result instantly, even for large numbers like a
100, 000, 000.

def fibonacci()
a = 0
b = 1
evenTotal = 0
while a  1:
a,b = b,a+b
if a%2 == 0:
evenTotal += a
print evenTotal

Does this help?
Kinuthia...

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Memroy Error - how to manage large data sets?

2008-07-29 Thread kinuthiA muchanE

On Tue, 2008-07-29 at 12:00 +0200, [EMAIL PROTECTED] wrote:
 Message: 2
 Date: Tue, 29 Jul 2008 08:44:40 +0100
 From: Alan Gauld [EMAIL PROTECTED]
 Subject: Re: [Tutor] Tutor Digest, Vol 53, Issue 99
 To: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; format=flowed; charset=iso-8859-1;
 reply-type=original
 
 kinuthiA muchanE [EMAIL PROTECTED] wrote
 
  I have realised that when you need to work with large numbers,
 lists 
  are
  slow and tend to gobble up bytes.
 
 The lists are not particularly slow (obviously if you want to
 traverse them they take longer for more members) and they
 don't take up much more memory that the sum of the sizes
 of whats in them.
 
 The problem here is that the data items themselves are huge.
 Around 20K per item. Thus a million times 20K is 20G!
 
  This is how I would approach the problem, without using
  lists.
 
 Yep, we all seem to be pretty much in agreement :-)
 
  It produces the result instantly, even for large numbers like a
  100, 000, 000.
 
 Umm, not instantly on my PC... 1 million took 5 minutes,
 I've no idea how long 100 million would take!
I think the question is to calculate the sum of all even numbers in the
Fibonacci series which do not exceed a million, not the millionth term. 
Number 25 on Project Euler which asks for first term in the Fibonacci
series to contain 1000 digits, is the term-wise question ;)
 
  def fibonacci()
 a = 0
 b = 1
 evenTotal = 0
 while a  1:
 a,b = b,a+b
 if a%2 == 0:
 evenTotal += a
 print evenTotal
 
 Alan G 
 
 
 
 
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newbie

2008-07-25 Thread kinuthiA muchanE

On Fri, 2008-07-25 at 03:47 +0200, [EMAIL PROTECTED] wrote:
 Message: 9
 Date: Thu, 24 Jul 2008 18:47:32 -0700 (PDT)
 From: Sam Last Name [EMAIL PROTECTED]
 Subject: [Tutor]  Newbie
 To: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; charset=us-ascii
 
 mmmkay its me again
 Iv'e been learning alot and have found so much joy in making programs
 that solve equations using the input function.
 I need Help on this though.
 Im trying to make a program where it solves the quadratic formula when
 you put in the variables. 
 Here wats i got so far. :) and also, is there a function for square
 root? 
 
 a  =  input(What is the variable a?)
 b  =  input(What is the variable b?)
 c  =  input(What is the variable c?)
 # this is where i need help :(
 print -b + /sqrt (b*b - 4*a*c)/(2*a) 
 # this of course doesn't work i believe because i don't have the
 square root function and don know how to make one
 import math
 print math.sqrt(6)
2.44948974278
 print math.sqrt(8)
2.82842712475
 
math is a module which contains most of  the mathematical functions that
you need most of the time. Here we used just one of them, sqrt, to find
the square root of an integer. The Python Library Reference lists all of
them and their usage.
Does this help?
Kinuthia...




 
 Feedback appreciated :)

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 53, Issue 18

2008-07-05 Thread kinuthiA muchanE

On Sat, 2008-07-05 at 20:23 +0200, [EMAIL PROTECTED] wrote:
 Message: 7
 Date: Sat, 05 Jul 2008 12:23:36 -0600
 From: Nathan Farrar [EMAIL PROTECTED]
 Subject: [Tutor] Exploring the Standard Library
 To: Python Tutor tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; charset=us-ascii
 
 I'd like to spend some time exploring the standard library.  I'm
 running
 python on Ubuntu.  How would I find the location of the modules
 (find /
 -name os.py does not yield results)?
In Ubuntu, you can find all the documentation in the /usr/share/doc
directory. Therefore, for python you will find it
in /usr/share/doc/python. Furthermore, to find the location of some
documentation, in the terminal enter dpkg -L package-name, in your
case, just type dpkg -L python and you will be rewarded with paths for
all python related documentation.

Does this help?
Kinuthia...
 
 Thanks!
 Nathan

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 52, Issue 87

2008-06-29 Thread kinuthiA muchanE

On Sun, 2008-06-29 at 10:20 +0200, [EMAIL PROTECTED] wrote:
 Message: 1
 Date: Sat, 28 Jun 2008 21:53:44 -0400
 From: Kirk Z Bailey [EMAIL PROTECTED]
 Subject: [Tutor] arrays in python
 To: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; charset=ISO-8859-1; format=flowed
 
 Just wondering, if I can find a way to do a 2 dimensional array in 
 python. 1 dimension would be a list it would seem; for 2, I could use
 a 
 list of lists?
 
 Strange how I can't think of ever needing one since I discovered
 snake 
 charming, but so many languages do foo dimensional arrays, it would
 seem 
 like there ought to be a way to do it in python.
 
 -- 

 from numpy import *  # import the necessary module

 arry = array((1,2,3,4)) # create a rank-one array
 print arry
[1 2 3 4]
 print arry.shape
(4,) # this means it is a rank 1 array with a length of 4  (the trailing
comma means it is a tuple)

To get to the first element in the array:
 print arry[0]
1
 
 To get to the last element:
 print arry[-1]
 4
 arry2 = array(([5,6,7,8],[9,10,11,12])) # create a a rank-two array,
two-dimensional, if wish
 print arry2
 [[ 5  6  7  8]
  [ 9 10 11 12]]
 print arry2.shape #
 (2, 4) # this means that it is a rank 2 (ie 2-dimensional) array, with
each axis having a length of 4
 
 To get to the first element in the first axis:
 print arry2[0,0]
 5
To get to the last element in the second axis:
 print arry2[1,-1]
 12

You can slice it, reshape it, literally you can contort in way you want!
 
 Does this help?
 Kinuthia...


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Problem Euler 26

2008-06-29 Thread kinuthiA muchanE
Hi,
I am trying to solve Problem Number 26
(http://projecteuler.net/index.php?section=problemsid=26) on project
Euler but apparently the answer I am submitting is wrong.

Here is the problem:


A unit fraction contains 1 in the numerator. The decimal representation
of the unit fractions with denominators 2 to 10 are given:

1/2
= 
0.5
1/3
= 
0.(3)
1/4
= 
0.25
1/5
= 
0.2
1/6
= 
0.1(6)
1/7
= 
0.(142857)
1/8
= 
0.125
1/9
= 
0.(1)
1/10
= 
0.1

Where 0.1(6) means 0.16..., and has a 1-digit recurring cycle. It
can be seen that 1/7 has a 6-digit recurring cycle.

Find the value of d  1000 for which 1/d contains the longest recurring
cycle in its decimal fraction part.

I am giving the answer 38, because 1/38 = 0.0263157894. It seems I have
misunderstood the question or I cant solve it! Here is the code that I
came up with:

def aux(num):
import re
pattern = re.compile(r^0?1?2?3?4?5?6?7?8?9?$)

frac =%.9f%(1.0/num) 
fracSlice = frac[2:]# get the decimal fractional 
part, ie remove
'0.'

fracList = list(fracSlice)  #convert string to a list
fracList.sort() # I need to sort , 
because I will be searching by
increasing order

testFrac  = .join(fracList)   # convert list back to a string, phew!
if re.match(pattern,testFrac):  # if the pattern matches, the number is
our candidate
print (num,fracSlice)


for b in xrange(1,1000):
aux(b)

Er... er, that does not exactly work as expected but it narrows the
search to only 3 candidates because of the inclusion of the zero:

 (28, '035714286')
 (38, '026315789')
 (81, '012345679')

For 28, the digit, in the fractional part, after 8 is 5, so 5 is
repeated and as for, 81 the next digit after 7 is 0, so again 0 occurs
twice. But for 38, the next digit after 9 is 4, and because it has NOT
occurred before, I assume 38 is the correct answer... and I am wrong! 

I suspect I have completely misunderstood the question.

Any ideas?
Thanks!

Kinuthia...
 

   




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Invoking Python

2008-06-26 Thread kinuthiA muchanE

On Thu, 2008-06-26 at 12:00 +0200, [EMAIL PROTECTED] wrote:
 Or more commonly add a first line like:
 
 #! /path/to/python/executable
 
 Then you can simply make the file executable and run it by typing its 
 name
 
 $ threeplusfour.py

On my computer, running Linux Ubuntu, I always have to type
./threePlusFour.py to get it to run. Otherwise I get a not found
command.
 
 or double clicking it in your favourite file manager GUI tool.
 
 See the topic Add a Little style in my tutorial,  in the box
 Note for Unix users
 for more details.
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 52, Issue 69

2008-06-25 Thread kinuthiA muchanE

On Wed, 2008-06-25 at 01:49 +0200, [EMAIL PROTECTED] wrote:
  ... or you could start you fire up a text editor (something like 
  Notepad
  in Windows, or nano in Linux and type 3+4(without the quotes!),
 
 Actually it would need to be
 
 print 3+4
 
 otherwise Python would silently evaluate the expression but

 not display the result.

Oh my, I was in the middle of something, something like
http://projecteuler.net/index.php?section=problemsid=74 
 
  One of the indefatigable contributors to this mailing list, Alan 
  Gauld
  (where do you get the time?),
 
 With increasing difficulty! :-)
I can understand that.
 
  Check it out at http://www.freenetpages.co.uk/hp/alan.gauld 
  (correct?)
 
 Correct, thanks for the plug!
 
 Sadly it will need to move soon since Freenet have
 announced that they will soon be decommissioning
 their free web site(*). I'm trying to decide whether to go to
 another free site or spend the money for a proper
 hosted site with dedicated domain name etc...
Here (in Kenya) the cheapest unlimited internet access option costs you
around 9600 Kenya Shillings, about $146 a month! I am already stumped!
 
 (*) They have already blocked ftp so I can't post updates
 anymore :-(
Dommage, dommage...
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Invoking Python

2008-06-25 Thread kinuthiA muchanE

On Tue, 2008-06-24 at 11:11 -0700, Danny Laya wrote:
 ... or you could start you fire up a text editor (something like
  Notepad
 in Windows, or nano in Linux and type 3+4(without the quotes!),
 hmmm..., and save the file as anything you want, lets say for  now you
 save the file as threePlusFour. Every time you invoke the python
 interpreter (do you know how to do that?) with threePlusFour, you
  will
 get the value seven! 
 
 Well HE..HE i don't know. Let say I write the threePlusFour file in
 /home/danny/threePlusFour.py
 How I can invoke that file
  ???
From the forward slashes in the file path I assume you are using a Linux based 
OS, Ubuntu perhaps? Well, to use python 
you need to to start the terminal or the shell. In Ubuntu, go to Main
Menu == Accessories and click on Terminal, you will now have a new
window open with something like this --- [EMAIL PROTECTED]:~$ . 

Enter the name python followed by the name of your file. In this case
you should enter python /home/danny/threePlusFour.py(without the
quotes!). If you want to enter into the Python interactive prompt,
simply type python and you should be rewarded with something like: 


Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32) 
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type help, copyright, credits or license for more information.
 

The  there means python is waiting for you to enter commands.
Try 3+4, or 4/3

Does this  help?
Kinuthia...


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 52, Issue 66

2008-06-24 Thread kinuthiA muchanE

On Tue, 2008-06-24 at 15:26 +0200, [EMAIL PROTECTED] wrote:
 Message: 5
 Date: Tue, 24 Jun 2008 05:47:29 -0700 (PDT)
 From: Danny Laya [EMAIL PROTECTED]
 Subject: [Tutor] fibonacci.py task ???
 To: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; charset=iso-8859-1
 
 Hi all, can you explain me what this code mean :
 
 Fibonacci.py
   
 # This program calculates the Fibonacci sequence
 a = 0
 b = 1
 count = 0
 max_count = 20
 while count  max_count:
 count = count + 1
 # we need to keep track of a since we change it
 old_a = a
 old_b = b
 a = old_b
 b = old_a + old_b
 # Notice that the , at the end of a print statement keeps it
 # from switching to a new line
 print old_a,
 
 
  Output:
  
 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
 
 Do you understand it ??? Can you explain meahhh you know
 i'm a newbie so please explain it with a simple expalanation.
 
 And I got many tutorial with title *.py(e.g: Fibonacci.py and
 Password.py),
 can you explain me what *.py mean? Thank's for helping me.
 
If you have an algorithm to calculate the sum of exactly two(!) numbers
you could do it in  the Python prompt by:

 3+4
7


... or you could start you fire up a text editor (something like Notepad
in Windows, or nano in Linux and type 3+4(without the quotes!),
hmmm..., and save the file as anything you want, lets say for  now you
save the file as threePlusFour. Every time you invoke the python
interpreter (do you know how to do that?) with threePlusFour, you will
get the value seven! 

Because there are many programming languages, such as C, java, perl,
ruby, haskell(!), you might want to be more specific as to what
programming language you save saved your code in. .c for C, .rb for
Ruby, .java for java and, of course .py for python. 

... or you could define a function...

One of the indefatigable contributors to this mailing list, Alan Gauld
(where do you get the time?), has an excellent tutorial for beginners.
Check it out at http://www.freenetpages.co.uk/hp/alan.gauld (correct?)

Kinuthia... 




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] RegEx to search for the '$' symbol

2008-06-11 Thread kinuthiA muchanE


 --
 
 Message: 4
 Date: Wed, 11 Jun 2008 10:35:30 +0100 (GMT+01:00)
 From: [EMAIL PROTECTED] [EMAIL PROTECTED]
 Subject: [Tutor] RegEx to search for the '$' symbol
 To: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain;charset=UTF-8
 
 Hi,
 Silly question but how do you use python's re module to find 
 dictionary items that contain the '$' symbol.
Hi, 
I no expert but here is my dime's worth...

  import re
  d = {'k':'$',1:'2','p':'$','j':'$dollar','l': 'dol$lar'}
  for i in d.values():
 ... re.findall(r'.*\$.*',i)
 ... 
 
 []
 ['$']
 ['$dollar']
 ['dol$lar']
 []
 
 NB:
 Empty matches are included in the result unless they touch the beginning of 
 another match. 
 
  
 
 Kinuthia...


 Thanks
 David
 
 
 
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 51, Issue 51

2008-05-23 Thread Kinuthia Muchane

[EMAIL PROTECTED] wrote:

Send Tutor mailing list submissions to
tutor@python.org

To subscribe or unsubscribe via the World Wide Web, visit
http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
[EMAIL PROTECTED]

You can reach the person managing the list at
[EMAIL PROTECTED]

When replying, please edit your Subject line so it is more specific
than Re: Contents of Tutor digest...


Today's Topics:

   1. Re: Equivalent 'case' statement (Alan Gauld)
   2. Re: Equivalent 'case' statement (inhahe)
   3. Reading only a few specific lines of a file (Jason Conner)
   4. Re: Reading only a few specific lines of a file (John Fouhy)
   5. Re: String Replacement question (Faheem)


--

Message: 1
Date: Fri, 23 May 2008 00:25:23 +0100
From: Alan Gauld [EMAIL PROTECTED]
Subject: Re: [Tutor] Equivalent 'case' statement
To: tutor@python.org
Message-ID: [EMAIL PROTECTED]
Content-Type: text/plain; format=flowed; charset=iso-8859-1;
reply-type=original


Dinesh B Vadhia [EMAIL PROTECTED] wrote

Is there an equivalent to the C/C++ 'case' (or 'switch') statement 
in Python?


No, just if/elif

However you can often achieve similar results with a dictionary:

def func1(v): return v

def func2(v): return v*2


switch = { 'val1': func1, # use a function for each value
'val2': func2,
 'val3': lambda v: this is three! } # or use 
lambda if preferred


val = raw_input(Value? (val1,val2,val3))


Something small here. This

print switch.[val](val)


should be: print switch[val](val)



### which is equivalent to:

if val == 'val1': print func1(val)
elif val == 'val2': print func2(val)
elif val == 'val3': print this is three

HTH,



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Equivalent 'case' Statement

2008-05-23 Thread Kinuthia Muchane

Hi,
I messed in earlier message, my apologies.

Message: 1
Date: Fri, 23 May 2008 00:25:23 +0100
From: Alan Gauld [EMAIL PROTECTED]
Subject: Re: [Tutor] Equivalent 'case' statement
To: tutor@python.org
Message-ID: [EMAIL PROTECTED]
Content-Type: text/plain; format=flowed; charset=iso-8859-1;
reply-type=original


Dinesh B Vadhia [EMAIL PROTECTED] wrote

Is there an equivalent to the C/C++ 'case' (or 'switch') statement 
in Python?


No, just if/elif

However you can often achieve similar results with a dictionary:

def func1(v): return v

def func2(v): return v*2


switch = { 'val1': func1, # use a function for each value
'val2': func2,
 'val3': lambda v: this is three! } # or use 
lambda if preferred


val = raw_input(Value? (val1,val2,val3))


This:

print switch.[val](val)


should be: print switch[val](val)



### which is equivalent to:

if val == 'val1': print func1(val)
elif val == 'val2': print func2(val)
elif val == 'val3': print this is three

HTH,



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String Replacement Question

2008-05-21 Thread Kinuthia Muchane



Moishy Gluck wrote:

  %s  %st*3
'String String String '
^
If you look closely at the end of the string there is an extra space.




   .join((st, )*3)
'String String String'
   ^
No extra space.

Ah...Okay!


On Wed, May 21, 2008 at 9:42 AM, Kinuthia Muchane [EMAIL PROTECTED] 
mailto:[EMAIL PROTECTED] wrote:


st = String
print %s  %st*3

String String String



Does this help?

Kinuthia...

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Else Clause In A Loop

2008-05-12 Thread kinuthia muchane
Hi,

I learnt that a loop can have an else clause. And that this clause
executes when the loop TERMINATES. In a while loop when the condition
becomes false, and in a for loop when a sequence is exhausted. When I
write the following code it seems to work:

for n in [1,2,3,4,5]:
print 'we are in the loop',n
else:
print 'we are now EXITING the loop',n

which results in:

we are in the loop 1
we are in the loop 2
we are in the loop 3
we are in the loop 4
we are in the loop 5
we are now EXITING the loop 5

Or:

n = 1
while n = 5:
print 'we are in the loop',n
n += 1
else:
print 'we are now EXITING the loop',n

...which gives:

we are in the loop 1
we are in the loop 2
we are in the loop 3
we are in the loop 4
we are in the loop 5
we are now EXITING the loop 6 (it spills over here!)

This has served to confuse me more. Would someone please kindly explain
how all this fits into the code below which searches (and finds!) for
prime numbers...


def prime():
   number = int(raw_input(Enter a number :))
   for i in range(2,number):
for j in range(2,i):
if i%j == 0:
break
else:
print is a prime number, i
prime()

...especially in the instance when number is 2 in the first for
statement, for then we will have for j range(2,2)! Or what is going
on?? 

Thanks,
Kinuthia...


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Else Clause In A Loop

2008-05-12 Thread kinuthia muchane

On Mon, 2008-05-12 at 14:08 -0400, Simón A. Ruiz wrote:
 I'll try my hand at this:
 
 The outside for loop is looking through every number up to the variable 
 number.
No quarrel here.
 
 For each of those numbers, it checks to see if any number between 2 and 
 i is divisible into i. If it finds anything, we know it's not a prime, 
 and so it breaks out of that second loop without completing it, which 
 means the else block isn't executed.

This is where I am getting lost. When the variable 'number' is 3, it
means that in that instance the inner 'for' statement  will be 'for j in
range(2,3)', hmmm which means that we will be dividing each element by 2
in the first 'for' statement and checking whether it is true , no? But
2%2 is zero, so, in my warped logic, the inner 'for' loop should break
and the else clause will not execute!
 
 If it can't find anything that i is divisible by, then that inside for 
 loop finishes without breaking, we know that i is a prime number, and 
 the else clause is executed.

This is where it gets even more interesting for me. Wont 'i' in one
instance be 8, so at that particular moment there will be a 7 in the
inner 'for' loop which will be divisible by one of our 'prime' numbers
ie 7?! I know I am wrong but for some reason I cannot see the light! :-)
 
 range(2,2) is is an empty list, so the loops ends uneventfully without 
 doing anything and the else clause is executed.
 
 I'm not an expert, and this seems to me to be what's happening there.
 
 Simón

All the same, thanks...

 
 kinuthia muchane wrote:
  Hi,
  
  I learnt that a loop can have an else clause. And that this clause
  executes when the loop TERMINATES. In a while loop when the condition
  becomes false, and in a for loop when a sequence is exhausted. When I
  write the following code it seems to work:
  
  for n in [1,2,3,4,5]:
  print 'we are in the loop',n
  else:
  print 'we are now EXITING the loop',n
  
  which results in:
  
  we are in the loop 1
  we are in the loop 2
  we are in the loop 3
  we are in the loop 4
  we are in the loop 5
  we are now EXITING the loop 5
  
  Or:
  
  n = 1
  while n = 5:
  print 'we are in the loop',n
  n += 1
  else:
  print 'we are now EXITING the loop',n
  
  ...which gives:
  
  we are in the loop 1
  we are in the loop 2
  we are in the loop 3
  we are in the loop 4
  we are in the loop 5
  we are now EXITING the loop 6 (it spills over here!)
  
  This has served to confuse me more. Would someone please kindly explain
  how all this fits into the code below which searches (and finds!) for
  prime numbers...
  
  
  def prime():
 number = int(raw_input(Enter a number :))
 for i in range(2,number):
  for j in range(2,i):
  if i%j == 0:
  break
  else:
  print  i, is a prime number
  prime()
  
  ...especially in the instance when number is 2 in the first for
  statement, for then we will have for j in range(2,2)! Or what is going
  on?? 
  
  Thanks,
  Kinuthia...
  
  
  ___
  Tutor maillist  -  Tutor@python.org
  http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Project Euler Problem 6

2008-04-24 Thread kinuthia muchane
Hi,

I am trying to solve problem 6 on the Project Euler, but when I submit
my answer, I am told that it is wrong. Here is the problem:


The sum of the squares of the first ten natural numbers is,


   1² + 2² + ... + 10² = 385
The square of the sum of the first ten natural numbers is,


(1 + 2 + ... + 10)² = 55² = 3025
Hence the difference between the sum of the squares of the first ten
natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one
hundred natural numbers and the square of the sum.

And here is my code:

def aux():
return sum([k*k for k in range(1,111)])# sum of the squares of the
first one hundred numbers

def aux1():
inter = sum([k for k in range(1,111))# square of the sum of the first
one hundred numbers
return inter**2

def aux2():
 return aux1() - aux()# the difference of the two sums

print aux2()

...which gives a result of 36821290.

It worked fine with the example they have given but not with the hundred
one... What am I missing?

Thanks!
Kinuthia...




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Euler Problem 6

2008-04-24 Thread kinuthia muchane
Hi,

I think I need to be more careful in the future. This line 
 
return sum([k*k for k in range(1,111)])

should be:

return sum([k*k for k in range(1,101)]).

... and this one

sum([k for k in range(1,101))

also changes to:

sum([k for k in range(1,101))

Kinuthia...


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Computing factorial...

2008-04-21 Thread kinuthia muchane
Hi,

I wanted to calculate the factorial of a given number without using
recursion. I came up with the following code, although it is not very
elegant it works. 

def factorial(*args):
 
 product = args[0]
 for item in args[1:]:
 product *= item
 return product
 
number = int(raw_input('Enter value of number to compute factorial  '))
seq = range(1,number + 1)

if number = 0:
  print -1

else:
 print factorial(*seq)  

When I change that code a bit to (In fact, this is what I started with,
it almost drove me crazy trying to figure out what was wrong!) :

def factorial(*args):
 
 temp = args[0]
 for item in args[1:]:
 product = temp * item
 return product
 
number = int(raw_input('Enter value of number to compute factorial  '))
seq = range(1,number + 1)

if number = 0:
  print -1

else:
 print factorial(*seq)  

... it just echoes back the number you were prompted to enter.
My confusion is, aren't the variables 'temp' and 'product' storing the
same value ie args[0]. So why would they return different values, the
one with temp giving a wrong answer?

Thanks!

Kinuthia...



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Executing from Python prompt

2008-04-20 Thread kinuthia muchane

On Sat, 2008-04-19 at 16:06 -0400, bob gailer wrote:
 kinuthia muchane wrote:
  Hi,
 
  I do not know what I am doing wrong. When I run the following code from
  the Python prompt it executes without a murmur. But when I save it as
  a .py file and try to execute it from the shell, it just returns the
  prompt...actually it is all scripts that return a value which  are
  behaving in this manner.
 
 
  def factorial(n):
  if n = 1:
return 1
  else:
return n * factorial(n-1)
 
  factorial(some number here)
 

 print factorial(some number here)
 
 When you enter an expression at the interactive prompt you see the 
 value. Not true when you run a program. Expression values must be 
 printed. So you are doing nothing wrong.
 
Thanks, Bob, it worked the first time. It was staring me in the face the
whole time! Thanks once again.

Kinuthia...

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 50, Issue 63

2008-04-20 Thread kinuthia muchane

 kinuthia muchane [EMAIL PROTECTED] wrote
 
  I do not know what I am doing wrong. When I run the following code 
  from
  the Python prompt it executes without a murmur. But when I save it 
  as
  a .py file and try to execute it from the shell, it just returns the
  prompt...actually it is all scripts that return a value which  are
  behaving in this manner.
 
 The  prompt evaluates expressions and prionts the value.
 The interpreter on the other hand does not automatically print
 values you ghave to explicitly tell it to print using the print
 statement.
 
 So in your case just add the print command in front of the
 function call and all will be well.
 
  factorial(some number here)
 
 print factorial(n)
 
 HTH,
 
 
 -- 
 Alan Gauld

  Thanks, that worked.
  
  Kinuthia...


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Executing from Python prompt

2008-04-19 Thread kinuthia muchane
Hi,

I do not know what I am doing wrong. When I run the following code from
the Python prompt it executes without a murmur. But when I save it as
a .py file and try to execute it from the shell, it just returns the
prompt...actually it is all scripts that return a value which  are
behaving in this manner.


def factorial(n):
if n = 1:
  return 1
else:
  return n * factorial(n-1)

factorial(some number here)

The same happens when I use IDLE.

Thanks!
Kinuthia...



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 50, Issue 56

2008-04-18 Thread kinuthia muchane

 
 --
 
 Message: 3
 Date: Thu, 17 Apr 2008 10:58:00 -0700
 From: Dick Moores [EMAIL PROTECTED]
 Subject: Re: [Tutor] python assignments
 To: Python Tutor List tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; charset=us-ascii; format=flowed
 
 At 04:42 AM 4/17/2008, you wrote:
 Dear friends,
 
 I covered few introductory books on python. B4 going for an advanced 
 book, i want to take up small, small assignments and try to solve 
 with python. Can someone please suggest me any url where i can have 
 assignments and solutions.
 
 http://projecteuler.net/index.php?section=problems
 
Hi, this link is not loading.
 
 Dick Moores
 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 50, Issue 43

2008-04-14 Thread kinuthia muchane

 
 Message: 1
 Date: Mon, 14 Apr 2008 01:31:41 -0700
 From: Dinesh B Vadhia [EMAIL PROTECTED]
 Subject: [Tutor] encode unicode strings from pysqlite
 To: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; charset=iso-8859-1
 
 Here is a program that SELECT's from a pysqlite database table and encode's 
 the returned unicode strings:
 
 import sys
 import os
 import sqlite3
 
 con = sqlite3.connect(testDB.db)
 cur = con.cursor()
 
 a = u'99 Cycling Swords'
 b = a.encode('utf-8')
 print b
 
 q = '%wor%'
 limit = 25
 query = SELECT fieldB FROM testDB WHERE fieldB LIKE '%s' LIMIT '%s' %(q, 
 limit)
 for row in cur.execute(query):
 r = str(row)
 print r.encode('utf-8')

Why not change this to:

 for row in cur.execute(query):
 for item in row:
   print item.encode('utf-8')
which will return a string ?


Kinuthia.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor