[Tutor] anomaly

2018-04-19 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
I have a situation in which the same code gives an error in idle but works
in qtconsole
regards,

*​in idle*
v = np.zeros(len(x))

for i in range(len(x)):
if x[i] < 1.0:
v[i] = 0
else:
v[i] = V
print v​

 RESTART: C:\Users\SHARMA\Documents\Python Scripts\sqwell.py


Traceback (most recent call last):
  File "C:\Users\SHARMA\Documents\Python Scripts\sqwell.py", line 45, in

if x[i] < 1.0:
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()

​*in qtconsole​*

v = np.zeros(len(x))

for i in range(len(x)):
if x[i] < 1.0:
v[i] = 0
else:
v[i] = V
print v

[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. 20. 20. 20. 20.
 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20.
 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20.
 20. 20. 20. 20. 20. 20. 20. 20. 20. 20.]

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


Re: [Tutor] coding help with maxwell-boltzmann distribution

2017-10-13 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Yes we can vectorize.

regards,
Sarma.

On Fri, Oct 13, 2017 at 9:43 PM, Peter Otten <__pete...@web.de> wrote:

> D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
> > f = np.zeros(40)
> > v = np.arange(0,4,0.1)
> > for i in np.arange(0, 40):
> > f[i] = v[i]**2*(np.exp(-v[i]**2))
>
> Note that you can write this without Python loop as
>
> v = np.arange(0, 4, 0.1)
> f = v**2 * np.exp(-v**2)
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] coding help with maxwell-boltzmann distribution

2017-10-13 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Except for some constants the essential behaviour of Maxweell-Boltzmann
distribution is determined by
v**2 * exp(-v**2)
The following code will gove you
a plot of the shape of the curve.

from matplotlib import pyplot as plt
import numpy as np
f = np.zeros(40)
v = np.arange(0,4,0.1)
for i in np.arange(0, 40):
f[i] = v[i]**2*(np.exp(-v[i]**2))
plt.plot(v,f)
plt.show()

regards,
Sarma.

On Fri, Oct 13, 2017 at 11:28 AM, Mark Lawrence via Tutor 
wrote:

> On 12/10/17 21:22, Cameron McKay wrote:
>
>> Hello,
>>
>> I've never used python trying to plot a graph. Thus I am having
>> difficulties trying to plot the maxwell-boltzmann distribution. right now
>> i've defined the y-axis given the probability, but the difficult part is
>> trying to plot x in the form of:
>>
>> x = v/(2kT/m)^(1/2)
>>
>> before i used the linspace function but i believe that was wrong as it
>> just
>> gave me an exponential growth function as i need a bellcurve.
>>
>> Thanks for looking into this,
>>
>> Cameron
>>
>>
> Hopefully this helps https://docs.scipy.org/doc/sci
> py/reference/generated/scipy.stats.maxwell.html
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
>
> Mark Lawrence
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Require help for a script

2017-09-19 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
First fill the first row and first column of the matrix with seperate loops.
Then the rest of the elements of the matrix can be filled with nested loops.
regards,
Sarma.


On Tue, Sep 19, 2017 at 1:18 PM, Clara Chua  wrote:
> Hi Python tutor, I require help for a script that asks user for number of
> rows, r and number of columns c, and generates a r x c matrix with the
> following values:
>
> - The value of each element in the first row is the number of the column
> - The value of each element in the first column is the number of the row
> - The rest of the elements each has a value equal to the sum of the element
> above it and the element to the left.
>
> Example of such matrix if a user inputs r=4 and c=5
>
> 12 3 4 5
> 24 711   16
> 3714   25   41
> 4   1125   50   91
>
> We were told to use the for loops as well as numpy for this.
> Thank you in advance!
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Image i/o in python

2017-07-16 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
In python we have a set of imread and imshow in skimage. In matplotlib.image
we again have imreadand imshow functions. In scipy.misc we again have
another set imread and imshow. Are there anyfunctional differences between
these multiple sets to justify their presence? …

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


Re: [Tutor] reg. list update

2017-04-17 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
This is an aliasing problem. Change the code to

super = []
sub = [""]*3
other = ["a","b","c","d"]
sub[0] = "hi"
sub[1] = "hello"
for item in other:
l = sub[:]
l[2] = item
super.append(l)
for item in super:
print item

regards,
Sarma.

On Tue, Apr 18, 2017 at 2:16 AM, Mats Wichmann  wrote:

> On 04/17/2017 12:41 PM, Rasika Sapate via Tutor wrote:
> > Dear Python group,
> > I had written following code.
> >
> > super = []
> > sub = [""]*3
> > other = ["a","b","c","d"]
> > sub[0] = "hi"
> > sub[1] = "hello"
> > for item in other:
> > sub[2] = item
> > super.append(sub)
> > for item in super:
> > print item
> >
> >
> > Output :
> > ['hi', 'hello', 'd']
> > ['hi', 'hello', 'd']
> > ['hi', 'hello', 'd']
> > ['hi', 'hello', 'd']
> >
> >
> > Expected output:
> > ['hi', 'hello', 'a]
> > ['hi', 'hello', 'b']
> > ['hi', 'hello', 'c']
> > ['hi', 'hello', 'd']
> >
> >
> > Is there anything wrong in this code or any feature of python?
>
> yeah, feature of Python.  you could google for "deep copy".
>
> in short, sub[2] ends up with a reference to, not a copy of, the object
> referenced by "item" in the first for loop. all four lists hold this
> reference. by the time you go to print, that's a reference to the value
> "item" held when the first loop exited, or 'd'.  item itself no longer
> refers to that, you assign new things to it.
>
> You can see this by adding a couple of debug print lines
>
> super = []
> sub = [""]*3
> other = ["a","b","c","d"]
> sub[0] = "hi"
> sub[1] = "hello"
> for item in other:
> sub[2] = item
> print "item id:", id(item)
> super.append(sub)
> for item in super:
> print item
> print "item[2] id:", id(item[2])
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorted function

2017-04-14 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Change your code to

def front_x(words):
  # +++your code here+++
  ls=[]
  ls1=[]
  for str in words:
  if str[0]=='x':
  ls.append(str)
  else:
  ls1.append(str);
  print ls
  print ls1
  ls =  sorted(ls)
  ls1 = sorted(ls1)
  ls.extend(ls1)
  return ls

regards,
Sarma.

On Fri, Apr 14, 2017 at 11:59 PM, shubham goyal 
wrote:

> Dear mentors,
> sorted function is not working when i am trying to sort the list of strings
> but list.sort() is working. can you please help me understand.In this
> question i was trying to sort the list but first sorting the letter
> starting from x and taking them first.
>
> def front_x(words):
>   # +++your code here+++
>   ls=[]
>   ls1=[]
>   for str in words:
>   if str[0]=='x':
>   ls.append(str)
>   else:
>   ls1.append(str);
>   print ls
>   print ls1
>   sorted(ls)
>   sorted(ls1)
>   ls.extend(ls1)
>   return ls
>
> front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa'])
>
> Output:['xzz', 'xaa', 'bbb', 'ccc', 'axx']
> Output should be:  ['xaa', 'xzz', 'axx', 'bbb', 'ccc']
>
> Thankyou. Sorry for bad writing skills.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-12 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
A lot of confusion is caused by the print function converting an integer or
float
to a string before printing to console. thus both '1234 and '1234' are
shown as
1234 on the console. Similarly '15.4' and 15.4 are displayed as 15.4. There
is no way
to tell which is a string, which is an int and which is a float by looking
at the display on
console. In order to find which is which one has to type the variables.


>>> s = '1234'

>>> print(s)
1234

>>> s = 1234

>>> print(s)
1234

>>> s = '15.4'

>>> print(s)
15.4

>>> s = 15.4

>>> print(s)
15.4

regards,
Sarma.

On Wed, Apr 12, 2017 at 11:11 AM, eryk sun  wrote:

> On Wed, Apr 12, 2017 at 4:03 AM, boB Stepp  wrote:
> >
> > I have not used the decimal module (until tonight).  I just now played
> > around with it some, but cannot get it to do an exact conversion of
> > the number under discussion to a string using str().
>
> Pass a string to the constructor:
>
> >>> d = decimal.Decimal('3.141592653589793238462643383279
> 50288419716939')
> >>> str(d)
> '3.14159265358979323846264338327950288419716939'
>
> When formatting for printing, note that classic string interpolation
> has to first convert the Decimal to a float, which only has 15 digits
> of precision (15.95 rounded down).
>
> >>> '%.44f' % d
> '3.14159265358979311599796346854418516159057617'
> >>> '%.44f' % float(d)
> '3.14159265358979311599796346854418516159057617'
>
> The result is more accurate using Python's newer string formatting
> system, which allows types to define a custom __format__ method.
>
> >>> '{:.44f}'.format(d)
> '3.14159265358979323846264338327950288419716939'
> >>> format(d, '.44f')
> '3.14159265358979323846264338327950288419716939'
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-03 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Small correction.

file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt"
with open(file_path) as a:
b = a.read()

get_year = input("What year were you born? ")

count = 0
b= '3'+b[2:]
n = len(b)
for i in range(n-3):
if b[i:i+4] == get_year:
count += 1
print("Your birth date occurs %s times in PI!" % (count))


regards,
Sarma.

On Tue, Apr 4, 2017 at 5:07 AM, D.V.N.Sarma డి.వి.ఎన్.శర్మ <
dvnsa...@gmail.com> wrote:

> I will go for this modification of the original code.
>
> file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt"
> with open(file_path) as a:
> b = a.read()
>
> get_year = input("What year were you born? ")
>
> count = 0
> b= '3'+b[2:]
> n = len(b)
> for i in range(n-4):
> if b[i:i+4] == get_year:
> count += 1
> print("Your birth date occurs %s times in PI!" % (count))
>
> regards,
> Sarma.
>
> On Tue, Apr 4, 2017 at 12:54 AM, Mats Wichmann  wrote:
>
>> On 04/03/2017 10:16 AM, Alan Gauld via Tutor wrote:
>> > On 03/04/17 16:42, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
>> >> Sorry. That was  stupid of me. The loop does nothing.
>> >
>> > Let me rewrite the code with some different variable names...
>> >
>>  with open(file_path) as a:
>>  b = a.read()
>> >
>> > with open (file_path) as PI_text:
>> >  PI_as_a_long_string = PI_text.read()
>> >
>>  for year in b:
>> >
>> > for each_char in PI_as_a_long_string:
>> >
>>  if get_year in b:
>>  count += 1
>> >
>> >   if get_year in PI_as_a_long_string:
>> >   count += 1
>> >
>>  print("Your birth date occurs %s times in PI!" % (count))
>> >
>> > if count:
>> >print("Your birth date occurs in PI, I checked, count, "times!")
>> >
>> >
>> > Aren't meaningful variable names great? We should all do
>> > them more often.
>>
>>
>> So the takeaways here are:
>>
>> in the first ("non-counting") sample, there's no need to use a loop,
>> because you're going to quit after the outcome "in" or "not in" right
>> away - there's no loop behavior at all.
>>
>> for year in b:
>> if get_year in b:
>> print("Your year of birth occurs in PI!")
>> break
>> else:
>> print("Your year of birth does not occur in PI.")
>> break
>>
>> for the counting version you could do something that searches for the
>> year repeatedly, avoiding starting over from the beginning so you're
>> actually finding fresh instances, not the same one (hint, hint). There
>> are several ways to do this, including slicing, indexing, etc.  Alan's
>> suggestion looks as good as any.
>>
>> Or, if you don't care about overlapping cases, the count method of a
>> string will do just fine:
>>
>> PI_as_a_long_string.count(year)
>>
>> If you're talking about 4-digit year numbers using a Western calendar in
>> digits of PI, the overlap effect seems unlikely to matter - let's say
>> the year is 1919, do we think PI contains the sequence 191919? count
>> would report back one instead of two in that case. In other cases it
>> might matter; count is written specifically to not care about overlaps:
>> "Return the number of (non-overlapping) occurrences"  So that's worth
>> keeping in mind when you think about what you need from
>> substrings-in-strings cases.
>>
>>
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-03 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
I will go for this modification of the original code.

file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt"
with open(file_path) as a:
b = a.read()

get_year = input("What year were you born? ")

count = 0
b= '3'+b[2:]
n = len(b)
for i in range(n-4):
if b[i:i+4] == get_year:
count += 1
print("Your birth date occurs %s times in PI!" % (count))

regards,
Sarma.

On Tue, Apr 4, 2017 at 12:54 AM, Mats Wichmann  wrote:

> On 04/03/2017 10:16 AM, Alan Gauld via Tutor wrote:
> > On 03/04/17 16:42, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
> >> Sorry. That was  stupid of me. The loop does nothing.
> >
> > Let me rewrite the code with some different variable names...
> >
>  with open(file_path) as a:
>  b = a.read()
> >
> > with open (file_path) as PI_text:
> >  PI_as_a_long_string = PI_text.read()
> >
>  for year in b:
> >
> > for each_char in PI_as_a_long_string:
> >
>  if get_year in b:
>  count += 1
> >
> >   if get_year in PI_as_a_long_string:
> >   count += 1
> >
>  print("Your birth date occurs %s times in PI!" % (count))
> >
> > if count:
> >print("Your birth date occurs in PI, I checked, count, "times!")
> >
> >
> > Aren't meaningful variable names great? We should all do
> > them more often.
>
>
> So the takeaways here are:
>
> in the first ("non-counting") sample, there's no need to use a loop,
> because you're going to quit after the outcome "in" or "not in" right
> away - there's no loop behavior at all.
>
> for year in b:
> if get_year in b:
> print("Your year of birth occurs in PI!")
> break
> else:
> print("Your year of birth does not occur in PI.")
> break
>
> for the counting version you could do something that searches for the
> year repeatedly, avoiding starting over from the beginning so you're
> actually finding fresh instances, not the same one (hint, hint). There
> are several ways to do this, including slicing, indexing, etc.  Alan's
> suggestion looks as good as any.
>
> Or, if you don't care about overlapping cases, the count method of a
> string will do just fine:
>
> PI_as_a_long_string.count(year)
>
> If you're talking about 4-digit year numbers using a Western calendar in
> digits of PI, the overlap effect seems unlikely to matter - let's say
> the year is 1919, do we think PI contains the sequence 191919? count
> would report back one instead of two in that case. In other cases it
> might matter; count is written specifically to not care about overlaps:
> "Return the number of (non-overlapping) occurrences"  So that's worth
> keeping in mind when you think about what you need from
> substrings-in-strings cases.
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-03 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Sorry. That was  stupid of me. The loop does nothing.




regards,
Sarma.

On Mon, Apr 3, 2017 at 8:44 PM, Alan Gauld via Tutor 
wrote:

> On 03/04/17 16:07, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
> > Modifying the code as shown below may work.
>
> I doubt it.
>
> > with open(file_path) as a:
> > b = a.read()
> >
> > get_year = input("What year were you born? ")
> >
> > count = 0
> > for year in b:
>
> Once more I ask, what does this loop do?
>
> > if get_year in b:
> > count += 1
> > print("Your birth date occurs %s times in PI!" % (count))
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-03 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Modifying the code as shown below may work.

file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt"
with open(file_path) as a:
b = a.read()

get_year = input("What year were you born? ")

count = 0
for year in b:
if get_year in b:
count += 1
print("Your birth date occurs %s times in PI!" % (count))

regards,
Sarma.

On Mon, Apr 3, 2017 at 8:22 PM, Alan Gauld via Tutor 
wrote:

> On 03/04/17 13:22, Rafael Knuth wrote:
>
> > with open (file_path) as a:
> > b = a.read()
> >
> > get_year = input("What year were you born? ")
> >
> > for year in b:
>
> Can you explain what you think this loop line is doing?
> I'm pretty sure it's not doing what you expect.
>
> > if get_year in b:
> > print("Your year of birth occurs in PI!")
> > break
> > else:
> > print("Your year of birth does not occur in PI.")
> > break
> >
> > As a next challenge, I wanted to check how often a person's birth year
> > occurs in PI. Unfortunately, I wasn't able to figure out how to use
> > the loop count properly.
>
> What loop count?
> There is none, its a for loop, no counter needed.
> (OK I just spotted your code below...)
>
> But there is a count() method on a string object that should help.
>
>
> > count = 0
> > for year in b:
> > if get_year in b:
> > count += 1
> > else:
> > print("Your birth date does not occur in PI.")
> > break
>
> > sum_count = sum(count)
>
> sum() sums a sequence, but count is an integer. You have been
> incrementing it as you go, the final value is already there.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calculate 4**9 without using **, was Re: QUESTION

2017-03-04 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Much simpler is

4*4*4*4*4*4*4*4*4

regards,
Sarma.

On Sat, Mar 4, 2017 at 2:20 PM, Peter Otten <__pete...@web.de> wrote:

> Tasha Burman wrote:
>
> > Hello python tutors,
> > I am having difficulty with a power function; what is another way I can
> do
> > 4**9 without using **? Thanks,
>
> Hello Tasha,
>
> "what is another way I can do 4**9 without using **?"
>
> sounds a lot like a homework question -- so you really have to think about
> it yourself a bit. A few hints:
>
> How would you do the calculation with pen and paper?
>
> Can you translate that into a Python loop? What kind of loop would you
> choose to repeat something 9 times?
>
> If you are still having difficulties come back here and show us the code
> you
> have tried, even if it doesn't work yet.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Adding numbers within a string

2016-10-12 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Then Isaac's function does that(but you have to leave a space after
last number in the string).

def foo(x):
a, b = 0, ""
for i in x:
if i != " ":
b += i
else:
print ("this is b", b)
a  += int(b)
print ("this is a",a)
b = ""
print ("final a ",a)

foo("10 5 8 ")
regards,
Sarma.


On Wed, Oct 12, 2016 at 11:35 PM, Alan Gauld via Tutor  wrote:
> On 12/10/16 17:58, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
>> Sorry, I did not read the conditions that split and for should not be used.
>
> It is even more confusing - split() may NOT be used
> but 'for' MUST be used...
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Adding numbers within a string

2016-10-12 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Sorry, I did not read the conditions that split and for should not be used.
regards,
Sarma.


On Wed, Oct 12, 2016 at 10:20 PM, D.V.N.Sarma డి.వి.ఎన్.శర్మ
 wrote:
> def string_sum(s):
>   total = 0
>   items = s.split(" ")
>   for item in items:
>total += int(item)
>   print(total)
>
> You can call the function as
>
> string_sum("10 4 5 ")
>
> Output will be 19.
> regards,
> Sarma.
>
>
> On Wed, Oct 12, 2016 at 3:22 PM, hell gates  wrote:
>>You can write your own str.split or just use while loop.
>>12.10.2016, 15:12, "LQ Soh" :
>>
>>  To whom it may concern,
>>  Can someone enlighten me as to how you can create a function such
>>  that sum_numbers('10 5 8'), when run, will give an answer of 23, without
>>  using str.split() and using a for loop
>>  ___
>>  Tutor maillist - [1]Tutor@python.org
>>  To unsubscribe or change subscription options:
>>  [2]https://mail.python.org/mailman/listinfo/tutor
>>
>> References
>>
>>Visible links
>>1. mailto:Tutor@python.org
>>2. https://mail.python.org/mailman/listinfo/tutor
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Adding numbers within a string

2016-10-12 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
def string_sum(s):
  total = 0
  items = s.split(" ")
  for item in items:
   total += int(item)
  print(total)

You can call the function as

string_sum("10 4 5 ")

Output will be 19.
regards,
Sarma.


On Wed, Oct 12, 2016 at 3:22 PM, hell gates  wrote:
>You can write your own str.split or just use while loop.
>12.10.2016, 15:12, "LQ Soh" :
>
>  To whom it may concern,
>  Can someone enlighten me as to how you can create a function such
>  that sum_numbers('10 5 8'), when run, will give an answer of 23, without
>  using str.split() and using a for loop
>  ___
>  Tutor maillist - [1]Tutor@python.org
>  To unsubscribe or change subscription options:
>  [2]https://mail.python.org/mailman/listinfo/tutor
>
> References
>
>Visible links
>1. mailto:Tutor@python.org
>2. https://mail.python.org/mailman/listinfo/tutor
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Code Not Working

2016-10-11 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
An easier solution is simply to type 'print' in place of 'test' in the
program. It will work.
regards,
Sarma.


On Mon, Oct 10, 2016 at 2:09 AM,   wrote:
> Everytime I run this it says test is not defined . I don’t understand. Can 
> someone please help correct?
>
> #Question 10
>
> def reverse(mystr):
> reversed = ''
> for char in mystr:
> reversed = char + reversed
> return reversed
>
> def is_palindrome(myStr):
> if myStr in reverse(myStr):
> return True
> else:
> return False
>
> test(is_palindrome("abba"))
> test(not is_palindrome("abab"))
> test(is_palindrome("tenet"))
> test(not is_palindrome("banana"))
> test(is_palindrome("straw warts"))
> test(is_palindrome("a"))
> test(is_palindrome(""))
>
> -Jeremy Gainey
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] maximum recursion error

2016-09-16 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Can somebody tell me why my IDLE hangs when asked setrecursionlimit.

>>> x=GetSet(40)
>>> x.var
40
>>> x.__dict__
{'var': 40}
>>> del x.var
>>> x.__dict__
{}
>>> import sys
>>> sys.setrecursionlimit(7)



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


Re: [Tutor] Problem

2016-08-29 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
The following code should do.

for a in range(1,10):
for b in range(1,5):
for c in range(1,5):
for mc in range(50, 55):
if mc ==(6*a)+(9*b)+(20*c):
print "mc= ",mc,"a= ",a,"b= ",b,"c=",c
regards,
Sarma.


On Mon, Aug 29, 2016 at 3:34 PM, Alan Gauld via Tutor  wrote:
> On 28/08/16 23:53, shahan khan wrote:
>>
>> I changed the code a bit and this is the result:
>> for a in range(1,11):
>> for b in range(1,6):
>> for c in range(1,6):
>> mc=(6*a)+(9*b)+(20*c)
>> if mc==50:
>> print 'For 50 McNuggets:''a=',a,'b=',b,'c=',c
>> if mc==51:
>> print 'Fpr 51 McNuggets:''a=',a,'b=',b,'c=',c
>> if mc==52:
>> print 'For 52 McNuggets:''a=',a,'b=',b,'c=',c
>> if mc==53:
>> print 'For 53 McNuggets:''a=',a,'b=',b,'c=',c
>> if mc==54:
>> print 'For 54 McNuggets:''a=',a,'b=',b,'c=',c
>> if mc==55:
>> print 'For 55 McNuggets:''a=',a,'b=',b,'c=',c
>> Result:
>> For 55 McNuggets:a= 1 b= 1 c= 2
>> For 53 McNuggets:a= 1 b= 3 c= 1
>> For 50 McNuggets:a= 2 b= 2 c= 1
>> For 53 McNuggets:a= 4 b= 1 c= 1
>>
>> Two questions:
>> 1) why is it printing backwards?
>
>
> It is printing in the order it finds them based on your loop values.
> So the first two have a=1, the 3rd has a=2 and the last a=4
>
> But please post in plain text not HTML since otherwise your code
> format gets messed up.
>
> Alan G
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fw: URGENT: PYTHON QUESTION

2016-03-31 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
This is working fine. What is the problem?


def manipulate_data(kind, data):
if kind == 'list':
return list(data)[::-1]
elif kind == 'set':
#data=set({"a", "b", "c", "d", "e", "ANDELA", "TIA", "AFRICA"})
return set(data)
elif kind == 'dictionary':
return dict.keys(data)
print manipulate_data("list", range(1,6))
print manipulate_data("set", {"a", "b", "c", "d", "e", "ANDELA",
"TIA",  "AFRICA"})
print manipulate_data("dictionary", {"apples": 23, "oranges": 15,
"mangoes": 3, "grapes": 45})
regards,
Sarma.


On Thu, Mar 31, 2016 at 10:15 PM, Steven D'Aprano  wrote:
> On Thu, Mar 31, 2016 at 12:47:21PM +, accessmuah via Tutor wrote:
>> Hi,
>> Pls, assist with this QuestionKindly stroll to the end
>
>> DS LABCreate a function manipulate_data that does the followingAccepts
>> as the first parameter a string specifying the data structure to be
>> used "list", "set" or "dictionary" Accepts as the second parameter the
>> data to be manipulated based on the data structure specified e.g [1,
>> 4, 9, 16, 25] for a list data structure Based off the first parameter
>>- return the reverse of a list or
>>- add items `"ANDELA"`, `"TIA"` and `"AFRICA"` to the set and return the 
>> resulting set
>>- return the keys of a dictionary
>>
>>
>> My Answer below - Kindly helped reviewed it
>> def manipulate_data(kind, data): if kind == 'list': return
>> list(data)[::-1] elif kind == 'set': data=set({"a", "b",
>> "c", "d", "e", "ANDELA", "TIA", "AFRICA"})return set(data)
>> elif kind == 'dictionary': return
>> dict.keys(data) manipulate_data("list",
>> range(1,6)) manipulate_data("set", {"a", "b", "c", "d", "e", "ANDELA",
>> "TIA", "AFRICA"})manipulate_data("dictionary", {"apples": 23,
>> "oranges": 15, "mangoes": 3, "grapes": 45})
>
> Unfortunately your code has been mangled by the email. Can you try
> sending it again WITHOUT using Yahoo's "Rich Text" (HTML mail)?
>
> I will try to reverse-engineer what you meant:
>
> def manipulate_data(kind, data):
> if kind == 'list':
> return list(data)[::-1]
> elif kind == 'set':
> data=set({"a", "b", "c", "d", "e", "ANDELA", "TIA", "AFRICA"})
> return set(data)
> elif kind == 'dictionary':
> return dict.keys(data)
>
> manipulate_data("list", range(1,6))
> manipulate_data("set", {"a", "b", "c", "d", "e", "ANDELA", "TIA", "AFRICA"})
> manipulate_data("dictionary", {"apples": 23, "oranges": 15, "mangoes": 3, 
> "grapes": 45})
>
>
> Is that what you intended?
>
>
>
>> CODE RESULT
>>
>>
>>  But i cant proceed to next stage...see error below   Pls assist
>
> I looked below, but you have not included the error. Perhaps you
> included it as an attachment? In that case, it would probably be
> deleted, and if it is a screen-shot, it will be useless to anyone who is
> blind or visually impaired and reading this using a screen reader.
>
> Please COPY and PASTE the text of the error into your email.
>
>
>
>
>
>
>>
>>
>>
>> Thanks
>> Regards
>> Shittu
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Citing Python

2016-03-16 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
-- Forwarded message --
From: D.V.N.Sarma డి.వి.ఎన్.శర్మ 
Date: Wed, Mar 16, 2016 at 3:39 PM
Subject: Re: [Tutor] Citing Python
To: Steven D'Aprano 


"Python 2.7.11 |Anaconda 2.5.0 (64-bit)| (default, Jan 29 2016,
14:26:21) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information."

Is not this we get on IDLE a citation?
regards,
Sarma.


On Wed, Mar 16, 2016 at 8:32 AM, Steven D'Aprano  wrote:
> On Tue, Mar 15, 2016 at 06:43:44PM +, Alan Gauld wrote:
>> On 15/03/16 11:45, Holderness, Ellie wrote:
>>
>> > How do I cite Python for my dissertation bibliography?
>> > I used version 3.5.1.
>>
>> I'm not sure a citation is strictly necessary for a programming
>> language, but if you want to you could cite the Python web site.
>> Would you cite JavaScript, CSS or HTML if you built a website?
>> Or SQL if you built a database?
>
> If the dissertation was *about* the database, or website, certainly you
> would.
>
> It is normal to cite the software used to generate results, so that
> others can replicate your work. If Ellie's dissertation depends on
> Python for her results, or if the dissertation is specifically about
> Python, then she will probably need to cite the specific version used.
>
> One approach is to cite the reference manual for the specific version:
>
> http://academia.stackexchange.com/questions/5482/how-do-i-reference-the-python-programming-language-in-a-thesis-or-a-paper
>
> See also Brett Cannon's thesis:
>
> https://www.researchgate.net/publication/213879590_Localized_Type_Inference_of_Atomic_Types_in_Python
>
> (Brett is one of the core developers of Python, particularly well known
> for his work on the new import system.)
>
> It's also common to reference Mathematica:
>
> http://support.wolfram.com/kb/472
>
> This paper:
>
> "Julia: A Fast Dynamic Language for Technical Computing"
> http://arxiv.org/abs/1209.5145
>
> references PyPy, Octave and R, but strangely not Julia!
>
>
>> If you used a particular tutorial to learn the language
>> you could cite that (either as a book or web site).
>
> I don't think that is appropriate unless you are specifically referring
> to the tutorial in the dissertation.
>
>
>
> --
> Steve
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Programming

2014-08-27 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
change the line

if answera == ["Oslo" or "oslo"]:

to

if answera == "Oslo" or  answera == "oslo":

and see if it works.

regards,
Sarma.


On Thu, Aug 28, 2014 at 12:27 AM, Alan Gauld 
wrote:

> On 27/08/14 14:40, Jake wrote:
>
>> To whom it may concern,
>> My name is Jake and I have recently started the GCSE computing course
>> with school.
>>
>
>  answera = input()
>> if answera == ["Oslo" or "oslo"]:
>>
>
> This doesn't do what you think it does.
>
> ["Oslo" or "oslo"]  is a list
>
> "Oslo" or "oslo"   is the content of the list and
> is a boolean expression which evaluates to True.
> (Each non-empty string is considered True by Python)
>
> So your 'if' line looks to Python like:
>
> if answera == [True]:
>
> But answera is a string so it will never equal a list
> with a single boolean value so you go to the else
> clause.
>
> A better way to do what you want is to convert the input
> to lowercase using the string lower() method and compare
> that to the string you want, like so:
>
> if answera.lower() == "oslo":
>
> If you need to check multiple possible answers you can use
> a list of strings and the 'in' operator like this:
>
> if answera.lower() in ['amsterdam', 'london', 'oslo']:
>
> HTH
> --
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] re module

2014-08-14 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
I tested it on IDLE. It works.

regards,
Sarma.


On Thu, Aug 14, 2014 at 7:37 PM, Chris “Kwpolska” Warrick <
kwpol...@gmail.com> wrote:

>
> On 14 Aug 2014 15:58 "Sunil Tech"  wrote:
> >
> > Hi,
> >
> > I have string like
> > stmt = 'Patient name: Upadhyay Shyam style="font-family: times new roman,times;">  Date of
> birth:   08/08/1988 Issue(s) to be
> analyzed:  tes style="font-size: 11pt;">Nurse Clinical summary:  test1 style="font-family: times new roman,times;"> Date of
> injury:   12/14/2013Diagnoses:   723.4 - 300.02 - 298.3
> - 780.50 - 724.4 Brachial neuritis or radiculitis nos - Generalized
> anxiety disorder - Acute paranoid reaction - Unspecified sleep disturbance
> - Thoracic or lumbosacral neuritis or radiculitis, unspecified />Requester
> name:   Demo Spltycdtestt style="font-family: times new roman,times;">Phone #:   (213)
> 480-9000Medical records reviewed __ pages of medical and
> administrative records were reviewed including:Criteria
> used in analysis  Reviewer comments  />DeterminationBased on the clinical information submitted for this
> review and using the evidence-based, peer-reviewed guidelines referenced
> above, this request is Peer Reviewer
> Name/Credentials  Solis, Test, PhD style="font-family: times new roman,times;">Internal Medicine />  />Attestation />Contact Information  roman,times\' size=\'3\'>Peer to Peer contact attempt 1: 08/13/2014 02:46
> PM, Central, Incoming Call, Successful, No Contact Made, Peer Contact Did
> Not Change Determination'
> >
> >
> > i am trying to find the various font sizes and font face from this
> string.
> >
> > i tried
> >
> > print re.search(" >
> >
> > Thank you.
> >
> >
> >
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> >
> Don't use regular expressions for HTML. Use lxml instead.
>
> Also, why would you need that exact thing? It's useless. Also, this code
> is very ugly, with too many s and — worse — s which should not
> be used at all.
>
> --
> Chris “Kwpolska” Warrick 
> Sent from my SGS3.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] A mergesort

2013-08-31 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
I have been searching for mergesort implimentations in python and came
across
this.

def merge(a, b):
if len(a)*len(b) == 0:
return a+b

v = (a[0] < b[0] and a or b).pop(0)
return [v] + merge(a, b)

def mergesort(lst):
if len(lst) < 2:
return lst

m = len(lst)/2
return merge(mergesort(lst[:m]), mergesort(lst[m:]))

mlst = [10, 9, 8, 4, 5, 6, 7, 3, 2, 1]
sorted = mergesort(mlst)
print sorted

Besides using recursion in merge function also, it has somethings
intresting.

Especially the statement

v = (a[0] < b[0] and a or b).pop(0)

gives a.pop(0), if a[0] < b[0] otherwise b.pop(0).

We have to look at the statement as

v = ((a[0] < b[0] and a) or b).pop(0)


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


Re: [Tutor] python help!

2013-02-09 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Remove also 'continue' statement under the 'if denary2 not in
range(0,256)' clause.


On Sun, Feb 10, 2013 at 7:06 AM, D.V.N.Sarma డి.వి.ఎన్.శర్మ <
dvnsa...@gmail.com> wrote:

> I have altered the program to run on Python 2.7.3
> Perhaps it will run on Python 3 with small/or no alterations.
>
> def show_menu():
>  print("===")
>  print("1-binary to denary")
>  print("2-denary to binary")
>  print("3-exit")
>  print("===")
>
>
> while True:
> show_menu()
>
> choice = raw_input("please enter an option: ")
>
> if choice == '1':
> binary = raw_input("Please enter a binary number: ")
> denary = 0
> place_value = 1
>
> for i in binary [::-1]:
> denary += place_value * int(i)
> place_value *= 2
>
> print("The result is",denary)
>
>
> elif choice == '2':
>  denary2 = int(raw_input("Please enter a denary number: "))
>  remainder = ''
>  if denary2 not in range(0,256):
>  denary2 = int(input("Please enter a denary number: "))
>  continue
>  while denary2 > 0:
> remainder = str(denary2 % 2) + remainder
> denary2 /= 2
>  print("The result is",remainder)
>
>
>
>
> elif choice == '3': break
>
>
> elif choice == '1' or choice == '2' or choice == '3':
> print("Invalid input-try again!")
>
>
>
> On Sun, Feb 10, 2013 at 12:50 AM, Peter Otten <__pete...@web.de> wrote:
>
>> Ghadir Ghasemi wrote:
>>
>> > Hi guys can you tell me what is wrong with the second part of this
>> > code(elif choice == '2'). When I type something other than 0-255, it
>> > correctly asks again for an input but when I enter a number from 0-255
>> it
>> > does nothing :
>>
>> It doesn't do nothing, it keeps running the while loop. Add a print() call
>>
>> > elif choice == '2':
>> >  denary2 = int(input("Please enter a denary number: "))
>> >  remainder = ''
>> >  while denary2 not in range(0,256):
>> >  denary2 = int(input("Please enter a denary number: "))
>> >  continue
>> >  while denary2 in range(0,256):
>>
>>   print("XXX denary2 =",  denary2)
>>
>> > remainder = str(denary2 % 2) + remainder
>> > denary2 >>= 1
>> >  print("The result is",remainder)
>>
>> to see what's happening.
>>
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
> --
> regards,
> Sarma.
>



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


Re: [Tutor] python help!

2013-02-09 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
I have altered the program to run on Python 2.7.3
Perhaps it will run on Python 3 with small/or no alterations.

def show_menu():
 print("===")
 print("1-binary to denary")
 print("2-denary to binary")
 print("3-exit")
 print("===")


while True:
show_menu()

choice = raw_input("please enter an option: ")

if choice == '1':
binary = raw_input("Please enter a binary number: ")
denary = 0
place_value = 1

for i in binary [::-1]:
denary += place_value * int(i)
place_value *= 2

print("The result is",denary)


elif choice == '2':
 denary2 = int(raw_input("Please enter a denary number: "))
 remainder = ''
 if denary2 not in range(0,256):
 denary2 = int(input("Please enter a denary number: "))
 continue
 while denary2 > 0:
remainder = str(denary2 % 2) + remainder
denary2 /= 2
 print("The result is",remainder)




elif choice == '3': break


elif choice == '1' or choice == '2' or choice == '3':
print("Invalid input-try again!")



On Sun, Feb 10, 2013 at 12:50 AM, Peter Otten <__pete...@web.de> wrote:

> Ghadir Ghasemi wrote:
>
> > Hi guys can you tell me what is wrong with the second part of this
> > code(elif choice == '2'). When I type something other than 0-255, it
> > correctly asks again for an input but when I enter a number from 0-255 it
> > does nothing :
>
> It doesn't do nothing, it keeps running the while loop. Add a print() call
>
> > elif choice == '2':
> >  denary2 = int(input("Please enter a denary number: "))
> >  remainder = ''
> >  while denary2 not in range(0,256):
> >  denary2 = int(input("Please enter a denary number: "))
> >  continue
> >  while denary2 in range(0,256):
>
>   print("XXX denary2 =",  denary2)
>
> > remainder = str(denary2 % 2) + remainder
> > denary2 >>= 1
> >  print("The result is",remainder)
>
> to see what's happening.
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



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


Re: [Tutor] Musical note on python

2012-09-16 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
winsound.Beep() takes only integral values for frequency.
Therefore you cannot use it if you want either just intonation or
equal temperment scales exactly.
-- 
regards,
Sarma.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Musical note on python

2012-09-13 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
This worked on my computor. Thank you. It will take me sometime to
digest the program.


-- 
regards,
Sarma.

On Fri, Sep 14, 2012 at 12:33 AM, eryksun  wrote:

>
> I've modified my previous script to add simple polyphonic sound. I
> included a basic square wave and a sawtooth wave, plus a random
> polyphonic wave. It's the kind of sound you might hear in a cheesy
> greeting card. Also, since you're looking to use winsound.PlaySound(),
> I changed the byte packing to use a little endian signed short
> (int16). I think that's what Windows wave files use.
>
> My demo below still uses pyaudio, which wraps the cross-platform
> PortAudio library. To me it seems like a better solution than relying
> on the standard library (e.g. the standard lib uses OSS on Linux; I
> don't even have that installed).
>
> Hopefully it performs OK on your computer. Using NumPy arrays would
> speed up the number crunching.
>
>
> import pyaudio
> from math import sin, pi
> from struct import pack
> from random import sample, randrange
>
> def polywave(freqs, amps, f0, fs):
> """create a polyphonic waveform
>
> freqs: sequence of frequencies (Hz)
> amps: sequence of amplitudes
> f0: fundamental frequency (Hz)
> fs: samples/second
>
> output is normalized to the range [-1,1].
> """
> N = int(fs / f0)
> rad_step = 2 * pi / fs  # radians / (Hz * sample)
> wave = [0.0] * N
> for f, a in zip(freqs, amps):
> for n in xrange(N):
> wave[n] += a * sin(rad_step * f * n)
> maxamp = abs(max(wave, key=lambda x: abs(x)))
> return [samp / maxamp for samp in wave]
>
> def packwave(wave, vol=1, duration=None, fs=None):
> """pack a waveform as bytes (int16)
>
> wave: sample sequence, |mag| <= 1
> vol: optional volume scale, |mag| <= 1
> duration: optional loop time (seconds)
> fs: samples/second
>
> fs is required to set duration.
> """
> scale = min(vol * 32767, 32767)
> ncycles = 1
> if not (duration is None or fs is None):
> ncycles = int(round(1.0 * duration * fs / len(wave)))
> data = b''.join(pack(' return data * ncycles
>
> def make_square(num, f0, fs):
> """generate square wave components
>
> num: number of harmonics
> f0: funamental frequency (Hz)
> fs: samples/second
>
> fs/2 is the upper bound.
> """
> stop = min(2*num, int(fs / (2 * f0)))
> freqs = [n * f0 for n in xrange(1, stop, 2)]
> amps = [1.0 / n for n in xrange(1, stop, 2)]
> return freqs, amps
>
> def make_saw(num, f0, fs):
> """generate sawtooth wave components
>
> num: number of harmonics
> f0: funamental frequency (Hz)
> fs: samples/second
>
> fs/2 is the upper bound.
> """
> stop = min(num + 1, int(fs / (2 * f0)))
> freqs = [n * f0 for n in xrange(1, stop)]
> amps = [(-1.0) ** (n + 1) / n for n in xrange(1, stop)]
> return freqs, amps
>
> def make_rand(num, f0, fs):
> """generate wave with random harmonics/amplitudes"""
> ftop = min(fs // 2, 12000)
> nmax = int(ftop / f0)
> num = min(num, nmax)
> freqs = [n * f0 for n in sample(xrange(1, nmax+1), num)]
> amps = [randrange(32768)/32767.0 for n in xrange(num)]
> return freqs, amps
>
> def play(data, stream):
> chunks = (data[i:i+1024] for i in xrange(0, len(data), 1024))
> for chunk in chunks:
> stream.write(chunk)
>
> if __name__ == "__main__":
> from time import sleep
>
> fs = 48000
>
> p = pyaudio.PyAudio()
> stream = p.open(
> format=pyaudio.paInt16,
> channels=1,
> rate=fs,
> frames_per_buffer=fs//4,
> output=True)
>
> # http://en.wikipedia.org/wiki/
> # Equal_temperament#Calculating_absolute_frequencies
>
> note = lambda n: 440 * (2**(1/12.)) ** (-21 + n)
>
> scale = [note(n) for n in range(12)]
> rscale = [note(13-n) for n in range(12)]
> vols = [0.2 + 0.05*n for n in range(len(scale))]
> rvols = list(reversed(vols))
>
> def play_scale(scale, vols, wave_func, master_vol=1):
> duration = 0.5
> nharmonics = 30
> for f0, vol in zip(scale, vols):
> freqs, amps = wave_func(nharmonics, f0, fs)
> wave = polywave(freqs, amps, f0, fs)
> data = packwave(wave, master_vol * vol, duration, fs)
> play(data, stream)
> sleep(0.5)
>
> play_scale(scale, vols, make_square, 0.5)
> play_scale(rscale, rvols, make_saw, 0.5)
> play_scale(scale, vols, make_rand, 0.75)
> play_scale(rscale, rvols, make_rand, 0.75)
>
> stream.clos

Re: [Tutor] Musical note on python

2012-09-13 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Error: "name data undefined"


On Fri, Sep 14, 2012 at 12:33 AM, eryksun  wrote:

> On Thu, Sep 13, 2012 at 11:48 AM, D.V.N.Sarma డి.వి.ఎన్.శర్మ
>  wrote:
> >
> > As far as programming volume is concerned winsound.Beep has only
> frequency
> > and duration. pyaudio module appears to have provision for volume
> control.
>
> You should be able to add a wave header to a raw byte string. For example:
>
>
> import wave
> import winsound
> from cStringIO import StringIO
>
> def get_wave(data):
> f = StringIO()
> w = wave.open(f, 'w')
> w.setnchannels(1) # mono
> w.setsampwidth(2) # 2 bytes
> w.setframerate(48000) # samples/second
> w.writeframes(data)
> return f.getvalue()
>
>
> Then play the sound like this (_untested_):
>
>
> wave_data = get_wave(data)
> windsound.PlaySound(wave_data, winsound.SND_MEMORY)
>
>
>
>
-- 
regards,
Sarma.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Musical note on python

2012-09-13 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
As far as programming volume is concerned winsound.Beep has only frequency
and duration.
pyaudio module appears to have provision for volume control. Please refer
to eryksun's post.

-- 
regards,
Sarma.

On Thu, Sep 13, 2012 at 8:42 PM, Mark Lawrence wrote:

> On 13/09/2012 13:29, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
>
>> I want to thank all of you who have come forward to help me.
>>
>> __**_
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/**mailman/listinfo/tutor
>>
>>
> No problem :)  If you have found a solution could you please publish it
> here for the benefit of others, or at least provide a link.
>
>
> --
> Cheers.
>
> Mark Lawrence.
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



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


Re: [Tutor] Musical note on python

2012-09-13 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
I want to thank all of you who have come forward to help me.


-- 
regards,
Sarma.

On Thu, Sep 13, 2012 at 10:46 AM, D.V.N.Sarma డి.వి.ఎన్.శర్మ <
dvnsa...@gmail.com> wrote:

> Yes. As far as I can see it does not contain any thing which
> concerns the volume of sound.
>
>
> --
> regards,
> Sarma.
>
> On Thu, Sep 13, 2012 at 5:42 AM, Mark Lawrence wrote:
>
>> On 13/09/2012 00:57, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
>>
>>> The Beep attribute of winsound module is useful. But the volume is
>>> feeble.
>>> Is there anyway to control the loudness.
>>>
>>>
>> Have you read 
>> http://docs.python.org/**library/winsound.html?
>>
>> --
>> Cheers.
>>
>> Mark Lawrence.
>>
>>
>> __**_
>> 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


Re: [Tutor] Musical note on python

2012-09-12 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Yes. As far as I can see it does not contain any thing which
concerns the volume of sound.


-- 
regards,
Sarma.

On Thu, Sep 13, 2012 at 5:42 AM, Mark Lawrence wrote:

> On 13/09/2012 00:57, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
>
>> The Beep attribute of winsound module is useful. But the volume is feeble.
>> Is there anyway to control the loudness.
>>
>>
> Have you read 
> http://docs.python.org/**library/winsound.html?
>
> --
> Cheers.
>
> Mark Lawrence.
>
>
> __**_
> 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


Re: [Tutor] Musical note on python

2012-09-12 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
One can ofcourse increase the volume by adjusting master volume.
But one needs ability to program volume level in order to produce
sounds of different volume levels in a piece of music.

-- 
regards,
Sarma.

On Thu, Sep 13, 2012 at 5:27 AM, D.V.N.Sarma డి.వి.ఎన్.శర్మ <
dvnsa...@gmail.com> wrote:

> The Beep attribute of winsound module is useful. But the volume is feeble.
> Is there anyway to control the loudness.
>
> --
> regards,
> Sarma.
> On Thu, Sep 13, 2012 at 4:45 AM, Alan Gauld wrote:
>
>> On 12/09/12 14:53, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
>>
>>> My OS is Windows XP. I have Python 2.7.3(32 bit). My question is
>>> are there any commands in Python which directly allow me to produce a
>>> pure note
>>>
>>
>> Have a look at the audioop, wave and winsound modules.
>>
>> Also PyGame has some tools that try to do platform independent audio...
>>
>> Access to the sound card is another matter since that is non standard and
>> determined by the sound drivers exposed by the manufacturer. The drivers
>> may well vary in capability depending ion the OS in use etc.
>>
>> You can also use ctypes for direct access to the Windows audio libraries
>> but that's a whole lot more complicated!
>>
>> --
>> Alan G
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>>
>>
>> __**_
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/**mailman/listinfo/tutor
>>
>
>
>
>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Musical note on python

2012-09-12 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
The Beep attribute of winsound module is useful. But the volume is feeble.
Is there anyway to control the loudness.

-- 
regards,
Sarma.
On Thu, Sep 13, 2012 at 4:45 AM, Alan Gauld wrote:

> On 12/09/12 14:53, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
>
>> My OS is Windows XP. I have Python 2.7.3(32 bit). My question is
>> are there any commands in Python which directly allow me to produce a
>> pure note
>>
>
> Have a look at the audioop, wave and winsound modules.
>
> Also PyGame has some tools that try to do platform independent audio...
>
> Access to the sound card is another matter since that is non standard and
> determined by the sound drivers exposed by the manufacturer. The drivers
> may well vary in capability depending ion the OS in use etc.
>
> You can also use ctypes for direct access to the Windows audio libraries
> but that's a whole lot more complicated!
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Musical note on python

2012-09-12 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
My OS is Windows XP. I have Python 2.7.3(32 bit). My question is
are there any commands in Python which directly allow me to produce a pure
note
of a given frequency, given volume and given duration. Further can we access
the different sound channels(sound card) available through Python.

-- 
regards,
Sarma.


On Wed, Sep 12, 2012 at 6:47 PM, Mark Lawrence wrote:

> On 12/09/2012 13:43, Dwight Hutto wrote:
>
>> Also, you try eryksun's solution/example, pyaudio, as well, since it's
>> a mpdule, using different forms for cross compatibility.
>>
>>
> You have again snipped the entire context so nobody has a clue what you're
> replying to.
>
> --
> Cheers.
>
> Mark Lawrence.
>
>
> __**_
> 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] Musical note on python

2012-09-11 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
How to produce  a musical note of given frequency,volume and duration in
Python.


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