Re: [Tutor] List Python Question..Please help

2013-09-27 Thread Amit Saha
On Sat, Sep 28, 2013 at 3:36 PM, Jacqueline Canales
 wrote:
> Thank you guys so much i was able to figure it out. I definitely thought to
> much into the the problem and made it harder on myself. Cant thank you
> enough for assisting me. I have one more problem with the coding tho.
>
> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
> new_list = []
> person = new_list
> for person in composers:
> if person[0].lower() == person[-1].lower():
> print(person)
>
> Output:
> Saint-Saens
> Easdale
> Nielsen

Great work!

>
> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
> new_list = []
> person = new_list
> for person in composers:
> if person[0].lower() == person[-1].lower():
> new_list.append(person)
> print(new_list)
>
> output:
> ['Saint-Saens']
> ['Saint-Saens', 'Easdale']
> ['Saint-Saens', 'Easdale', 'Nielsen']



>
> How can i make the output of the names into just one individual list.

You mean, just print it once? The last line of your output?

Just print after the loop is over.





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


Re: [Tutor] Help on class

2013-09-27 Thread Steven D'Aprano
On Fri, Sep 27, 2013 at 10:07:39PM +0800, bharath ks wrote:
> Hello,
> 
> May i know why object 'c' does not prompt for employee name and 
> employee id in the following code i get out put as 

Default values in Python functions and methods are evaluated once only, 
when the function or method is defined ("compile time"), not each time 
it is run.

You can test this yourself:

import time
def long_function():
# Simulate a long-running calculation
print("Calculation started at %s" % time.ctime())
time.sleep(30)
print("Calculation completed at %s" % time.ctime())
return 42


def test(value=long_function()):
print(value)


If you run this code, as I did, you will get something like:

py> def test(value=long_function()):
... print(value)
...
Calculation started at Sat Sep 28 14:37:38 2013
Calculation completed at Sat Sep 28 14:38:08 2013

only once, when the test function is created. Then, you can run that 
function as often as you like without the lengthy calculation being 
repeated:


py> test()
42
py> test()
42
py> test(23)
23
py> test()
42


This is called "eager evaluation of default arguments", as opposed to 
"lazy evaluation of default arguments". To get lazy evaluation, stick 
the code inside the function, with a sentinel value:

py> def test2(value=None):
... if value is None:
... value = long_function()
... print(value)
...
py> test2()
Calculation started at Sat Sep 28 14:41:08 2013
Calculation completed at Sat Sep 28 14:41:38 2013
42
py> test2(23)
23
py> test2()
Calculation started at Sat Sep 28 14:42:13 2013
Calculation completed at Sat Sep 28 14:42:43 2013
42


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


Re: [Tutor] (no subject)

2013-09-27 Thread Steven D'Aprano
On Fri, Sep 27, 2013 at 03:56:59PM -0400, Katie wrote:

> I am trying to write a program using Python v. 2.7.5 that will compute 
> the area under the curve y=sin(x) between x = 0 and x = pi. Perform 
> this calculation varying the n divisions of the range of x between 1 
> and 10 inclusive and print the approximate value, the true value, and 
> the percent error (in other words, increase the accuracy by increasing 
> the number of trapezoids). Print all the values to three decimal 
> places.


There are four parts to this:

1) Determine the true value of the area. You'll need some calculus 
   for that. Do you know how to work out the area under a graph?

   A = integral of sin(x) between 0 and pi

2) Write some code to use the trapezoid method to calculate the 
   approximate area under the graph, using N steps.

   Do you know how the trapezoid method works? I really need to 
   draw a picture here, which I can't do. Imagine if you sliced 
   the graph up into N equal-width slices. Each slice will be
   pi/N in width, since the whole graph is pi units across. 
   (Starting at 0, going to pi, so there are pi units in total;
   diving into N pieces means each one in pi/N across.)

   Each slice will look like a trapezoid, or specifically, a
   rectangle with a triangle at the top. I'm going to try 
   "drawing" the trapezoid, but rotated side-on because it is 
   easier:


   x1 +-y1
   .  | \
   .  |  \
   x2 +---  y2


   You should notes about the trapezoid method from class, or 
   from your text book. If all else fails, you can google for
   it and find *dozens* of websites that talk about it:

   https://duckduckgo.com/html/?q=trapezoid+method


3) Then you need some code to calculate the total area from the 
   area of all those slices.

4) And finally you need to calculate the difference between actual 
   area and the calculated area, and print to three decimal places.


Your over-all code will look something like this:


from math import pi, sin
actual_area = ***something goes here***
for N in range(1, 11):
# calculate the approximate area
total_area = 0
width = pi/N  # width of each trapezoid  
x1 = 0  # start at zero each time
x2 = x1+width
while x2 <= pi:
   y1 = ***something goes here***
   y2 = ***something goes here***
   area_of_slice = ***something goes here***
   total_area += area_of_slice
   x1, x2 = ***something goes here***
# now print the results
error = ***something goes here***
print ("N = %d; Actual = %.3f; Calculated = %.3; Error = %.3f"
   % (N, actual_area, total_area, error))


Oh, there's an even better way to calculate the slices, instead of using 
a while-loop you can use a for-loop. But give it a go with the 
while-loop first, see if you can work out what needs to be done to 
change it to a for-loop. As usual, check your notes from class.

> I am not sure what the code should look like. I was told that I should 
> only have about 12 lines of code for these calculations to be done.

Well, I could probably squeeze everything into 12 lines if I really 
wanted to. But in my opinion, certainly 20 lines (not counting comments 
or blank lines) is plenty.



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


Re: [Tutor] List Python Question..Please help

2013-09-27 Thread Steven D'Aprano
On Fri, Sep 27, 2013 at 12:04:38PM -0500, Jacqueline Canales wrote:
> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
> x = 'Antheil'
> s = 'Saint-Saens'
> h = 'Beethoven'
> y = 'Easdale'
> k = 'Nielsen'

This is a step backwards from what you had in your first post. You had 
the right idea in the first case:

for name in composers:
...

Then you just write a bit of code that checks `name`, rather than a bit 
of code to check `x`, another bit of code to check `s`, a third bit of 
code to check `h`, a fourth to check `y`, ...  What if you had ten 
thousand names to check? A for-loop doesn't care if there's one name or 
ten thousand names, computers are really good at that sort of tedius 
grunt-work. 

So, go back to where you started:

for name in composers:
# check whether name starts and ends with the same letter


You want an "if" test. In English:

if first letter of name equals last letter of name:
print name


How do you get the first letter of name? 

name[0]


How do you get the last letter of name?

name[-1]


How do you test if two things are equal?

# replace x and y with the first and last letter of name, as above
x == y  

is *almost* right. It's not quite what you want, because it is 
case-sensitive. 'A' == 'a' will return False, since they are not 
precisely them same. You need to make them the same case:

x.lower() == y.lower()  # or upper() if you prefer


Putting it all together:


for name in composers:
if  ==  :
print name


where you have to replace the stars  with:

first letter of name, converted to lower/upper case

last letter of name, converted to lower/upper case


respectively.



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


Re: [Tutor] List Python Question..Please help

2013-09-27 Thread Amit Saha
Hi Jacqueline,

On Sat, Sep 28, 2013 at 3:04 AM, Jacqueline Canales
 wrote:
> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
> x = 'Antheil'
> s = 'Saint-Saens'
> h = 'Beethoven'
> y = 'Easdale'
> k = 'Nielsen'
>
> if s[0] == 'S' or s[0] == 's' == s[-1] == 'S' or s[-1] == 's':
> if y[0] == 'E' or y[0] == 'e' == y[-1] == 'E' or y[-1] == 'e':
> if k[0] == 'N' or k[0] == 'n' == k[-1] == 'N' or k[-1] == 'n':
> print(s,k,y)
> else:
> print(" ")
>
> Answer i Got Below

> Saint-Saens Nielsen Easdale

>
> Is this what i was going for in the direction i was a bit confused if we
> were suppose create loops or if statements that are verified in the actual
> composers list. I don't know i feel as if i know what i need to do i just
> cant put it together.

Nothing to worry. Let us break the problem down.

In your first post, you mentioned this for loop:

composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
for person in composers:
print(person)

What does this do? It prints all the composers' names. However, what
you want is to print *only* the composer whose name starts and ends
with the first letter. So, what can you do?

I shall try to explain with an example from every day life. Let us
say, the management in your local cinema theatre says that you can
choose to see all of the films playing there. So, you can see all of:
'Turbo', 'Planes' and 'The Smurfs 2'. Now, let is say that your cinema
management became a little shrewd and tells you that you can only
watch the films starting with 'P'. So what do you do? In your mind,
you think which of 'Turbo',' Planes' and 'The Smurfs 2' starts with
'P'. So, you first check, 'Turbo' and you see that it fails the
condition, but 'Planes' agree with the condition and 'The Smurfs 2'
also fails. Thus, you choose 'Planes'.

So, your above program is in the right direction. What you have to now
do is, before printing the 'person', you need to check if the person's
name starts and ends with the same letter. I already showed you how
you can do so.

You may find the lower() method helpful here. It returns you a capital
letter into a lower one:

>>> 'A'.lower()
'a'

Does that make it easier?

Good luck.
-Amit


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


Re: [Tutor] List Python Question..Please help

2013-09-27 Thread Mark Lawrence

top posting fixed



On Fri, Sep 27, 2013 at 10:04 AM, Jacqueline Canales
mailto:jackiexxd...@gmail.com>> wrote:

composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale',
'Nielsen']
x = 'Antheil'
s = 'Saint-Saens'
h = 'Beethoven'
y = 'Easdale'
k = 'Nielsen'

if s[0] == 'S' or s[0] == 's' == s[-1] == 'S' or s[-1] == 's':
 if y[0] == 'E' or y[0] == 'e' == y[-1] == 'E' or y[-1] == 'e':
 if k[0] == 'N' or k[0] == 'n' == k[-1] == 'N' or k[-1] == 'n':
 print(s,k,y)
 else:
 print(" ")

Answer i Got Below
 >>>
Saint-Saens Nielsen Easdale
 >>>

Is this what i was going for in the direction i was a bit confused
if we were suppose create loops or if statements that are verified
in the actual composers list. I don't know i feel as if i know what
i need to do i just cant put it together.



On 28/09/2013 00:59, wesley chun wrote:> hello,
>
> well, i have to say that you've at least made a good start at a
> solution. right now you're thinking about it very much like a human. try
> to put yourself into the shoes of a computer: how can we solve this task
> for just ONE name?
>
> once you have that solution, then you can apply the same solution for
> all names by looping over or iterating through them. in your solution,
> you tried to do everything at once using brute force.
>
> i recommend you take the lessons learned you borrow some of that code
> and solve it for a single name. for example, take a look at this 
pseudocode:

>
> name = 'Guido'
> if name first letter == name last letter: # turn this into real Python
> using what you have
>  print 'match'
> else:
>  print 'not a match'
>
> then add the collection and a loop, and you'll be at your solution!
>
> best of luck!
> --wesley
>

I'd like to see the above work, e.g. how do you correctly compare the 
letters in 'Amanda'?


--
Cheers.

Mark Lawrence

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


Re: [Tutor] List Python Question..Please help

2013-09-27 Thread wesley chun
hello,

well, i have to say that you've at least made a good start at a solution.
right now you're thinking about it very much like a human. try to put
yourself into the shoes of a computer: how can we solve this task for just
ONE name?

once you have that solution, then you can apply the same solution for all
names by looping over or iterating through them. in your solution, you
tried to do everything at once using brute force.

i recommend you take the lessons learned you borrow some of that code and
solve it for a single name. for example, take a look at this pseudocode:

name = 'Guido'
if name first letter == name last letter: # turn this into real Python
using what you have
print 'match'
else:
print 'not a match'

then add the collection and a loop, and you'll be at your solution!

best of luck!
--wesley


On Fri, Sep 27, 2013 at 10:04 AM, Jacqueline Canales  wrote:

> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
> x = 'Antheil'
> s = 'Saint-Saens'
> h = 'Beethoven'
> y = 'Easdale'
> k = 'Nielsen'
>
> if s[0] == 'S' or s[0] == 's' == s[-1] == 'S' or s[-1] == 's':
> if y[0] == 'E' or y[0] == 'e' == y[-1] == 'E' or y[-1] == 'e':
> if k[0] == 'N' or k[0] == 'n' == k[-1] == 'N' or k[-1] == 'n':
> print(s,k,y)
> else:
> print(" ")
>
> Answer i Got Below
> >>>
> Saint-Saens Nielsen Easdale
> >>>
>
> Is this what i was going for in the direction i was a bit confused if we
> were suppose create loops or if statements that are verified in the actual
> composers list. I don't know i feel as if i know what i need to do i just
> cant put it together.
>
>
> On Fri, Sep 27, 2013 at 3:14 AM, 罗彭  wrote:
>
>> Maybe the length of each name matters, too. You should consider whether
>> to skip null('') and single-character('a').
>>
>>
>> On Fri, Sep 27, 2013 at 3:57 PM, Dharmit Shah wrote:
>>
>>> Also, comparison is case sensitive. Meaning, 'A' and 'a' are not the
>>> same.
>>>
>>> Hope that helps. :)
>>>
>>> On Fri, Sep 27, 2013 at 1:14 PM, Amit Saha 
>>> wrote:
>>> > On Fri, Sep 27, 2013 at 3:48 PM, Jacqueline Canales
>>> >  wrote:
>>> >> So I have been trying to do this program using ifs and or loops.
>>> >> I am having a hard time solving this question, If you could please
>>> assist me
>>> >> in the right direction.
>>> >>
>>> >> Write a program that lists all the composers on the list ['Antheil',
>>> >> 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen'] whose name starts
>>> and ends
>>> >> with the same letter (so Nielsen gets lsited, but Antheil doesn't).
>>> >>
>>> >> I know below it prints the entire list of composers but i dont know
>>>  how to
>>> >> do the program above. I think I am thinking to much into but ive
>>> looked at
>>> >> all my notes and online resources and having a hard time coming up
>>> with
>>> >> anything.
>>> >> Please help!
>>> >>
>>> >> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale',
>>> 'Nielsen']
>>> >> for person in composers:
>>> >> print(person)
>>> >
>>> > So, here you are printing every compose as you rightly state above.
>>> > What you now need to do is:
>>> >
>>> > For each of the composers (`person'), you need to check if the first
>>> > letter and the last letter are the same. Here;s a hint:
>>> >
>>>  s='abba'
>>> >
>>> > The first letter:
>>> >
>>>  s[0]
>>> > 'a'
>>> >
>>> > The last letter:
>>> >
>>>  s[-1]
>>> > 'a'
>>> >
>>> >
>>> > If you now compare these, you will know if they are the same and hence
>>> > you print him/her.
>>> >
>>> > Hope that helps.
>>> > -Amit
>>>
>>
-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A computer never does what you want... only what you tell it."
+wesley chun  : wescpy at gmail :
@wescpy
Python training & consulting : http://CyberwebConsulting.com
"Core Python" books : http://CorePython.com
Python blog: http://wescpy.blogspot.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2013-09-27 Thread Dave Angel
On 27/9/2013 15:56, Katie wrote:


>
> Hello,
> 
> 

Please post your messages in text mode, not html.

>
> I am trying to write a program using Python v. 2.7.5 that will compute 
> the area under the curve y=sin(x) between x = 0 and x = pi. Perform this 
> calculation varying the n divisions of the range of x between 1 and 10 
> inclusive and print the approximate value, the true value, and the percent 
> error (in other words, increase the accuracy by increasing the number of 
> trapezoids). Print all the values to three decimal places. 
>
> 
> 
>
> I am not sure what the code should look like. I was told that I should 
> only have about 12 lines of code for these calculations to be 
> done. 
>
> 
> 
>
> I am using Wing IDE. I have very basic knowledge in Python.
>
> 
> 
>
> I would appreciate help. 
>
> 
> 
>
> Thank you. 
> 
>

What part do you NOT understand?  Don't worry yet about the line count. 
Just write one function that represents some aspect of the problem that
you do understand.

When that works, write a second function for some other aspect.

-- 
DaveA


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


Re: [Tutor] Help on class

2013-09-27 Thread bob gailer

On 9/27/2013 10:07 AM, bharath ks wrote:

> Hello,
>
Hi welcome to the tutor list.
Please post in plain text rather than tiny hard-to-read formatted text.

> May i know why object 'c' does not prompt for employee name and 
employee id in the following code

You may - see comment below

> i get out put as
>
> Enter employee name:john
> Enter employee id:56
> Employee name is: john
> Employee id is: 56
> 
> Employee name is: test
> Employee id is: 1003
> 
> Employee name is: john
> Employee id is: 56
>
> class employee:
> emp_name=""
> emp_id=0
> def collect_name():
> return(input("Enter employee name:"))
>
>
> def collect_id():
> return(input("Enter employee id:"))
>
> def show(self):
> print("Employee name is:",self.emp_name)
> print("Employee id is:",self.emp_id)
>
> def __init__(self,name=collect_name(),id=collect_id()):

name=collect_name() and id=collect_id() are executed when the def is 
executed.

The values are saved as the default values of name and id.
That happens once, and the default values are used whenever __init__ is 
called without arguments.


> self.emp_name=name
> self.emp_id=id
>
>
>
> a=employee()
> a.show()
> print("")
> b=employee("test",1003)
> b.show()
> print("")
> c=employee()
> c.show()

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] List Python Question..Please help

2013-09-27 Thread Mark Lawrence

On 27/09/2013 18:04, Jacqueline Canales wrote:

composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
x = 'Antheil'
s = 'Saint-Saens'
h = 'Beethoven'
y = 'Easdale'
k = 'Nielsen'

if s[0] == 'S' or s[0] == 's' == s[-1] == 'S' or s[-1] == 's':
 if y[0] == 'E' or y[0] == 'e' == y[-1] == 'E' or y[-1] == 'e':
 if k[0] == 'N' or k[0] == 'n' == k[-1] == 'N' or k[-1] == 'n':
 print(s,k,y)
 else:
 print(" ")

Answer i Got Below
 >>>
Saint-Saens Nielsen Easdale
 >>>

Is this what i was going for in the direction i was a bit confused if we
were suppose create loops or if statements that are verified in the
actual composers list. I don't know i feel as if i know what i need to
do i just cant put it together.



Yuck :(  What happens if the list changes?  I suggest that you reread 
the post from Steven D'Aprano but slightly restate your original post 
such that you are looking for names where the final letter is the lower 
case equivalent of the first letter.  In that way your code should 
consist of four lines, the list of names, one for loop, one if statement 
and one print function, the latter assuming Python 3.


Hope this helps.

--
Cheers.

Mark Lawrence

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


Re: [Tutor] Help on class

2013-09-27 Thread Dave Angel
On 27/9/2013 10:07, bharath ks wrote:

> Hello,
>
> May i know why object 'c' does not prompt for employee name and employee id 
> in the following code
> i get out put as 
>
> Enter employee name:john
> Enter employee id:56
> Employee name is: john
> Employee id is: 56
> 
> Employee name is: test
> Employee id is: 1003
> 
> Employee name is: john
> Employee id is: 56
>
> class employee:
>
>     emp_name=""
>     emp_id=0

Those 2 class attributes are never used.  Good thing, since you really
want each employee to have his own name.

>     def collect_name():
>         return(input("Enter employee name:"))
>    
>     
>     def collect_id():
>         return(input("Enter employee id:"))
>             
>     def show(self):
>         print("Employee name is:",self.emp_name)
>         print("Employee id is:",self.emp_id)
>         
>     def __init__(self,name=collect_name(),id=collect_id()):

Those 2 function calls will happen only once, long before you
instantiate any instance of employee.  Put those inside the method, not
as default values.

>         self.emp_name=name
>         self.emp_id=id
>      
>      
>
> a=employee()
> a.show()
> print("")
> b=employee("test",1003)
> b.show()
> print("")
> c=employee()
> c.show()
>

The __init__() method should look something like this.  That way,
collect_name() and/or collect_id() will be called if the corresponding
argument is not given.

    def __init__(self,name = None, id=None):
if name:
        self.emp_name=name
else:
name=collect_name()
if id:
        self.emp_id=id
else:
self.id=collect_id()

>
>  size="2">Hello, size="2">May i know why object 'c' does not prompt for employee name and 
> employee id in the following codei get out 
> put as  size="2">Enter employee name:johnEnter 
> employee id:56Employee name is: 
> johnEmployee id is: 
> 56 size="2">  size="2">Employee name is: testEmployee id 
> is: 1003 size="2">  size="2">Employee name is:

Please switch to text mail, as the html will mess things up, sooner or
later.


-- 
DaveA


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


Re: [Tutor] List Python Question..Please help

2013-09-27 Thread Dave Angel
On 27/9/2013 13:04, Jacqueline Canales wrote:

> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
> x = 'Antheil'
> s = 'Saint-Saens'
> h = 'Beethoven'
> y = 'Easdale'
> k = 'Nielsen'
>
> if s[0] == 'S' or s[0] == 's' == s[-1] == 'S' or s[-1] == 's':
> if y[0] == 'E' or y[0] == 'e' == y[-1] == 'E' or y[-1] == 'e':
> if k[0] == 'N' or k[0] == 'n' == k[-1] == 'N' or k[-1] == 'n':
> print(s,k,y)
> else:
> print(" ")
>
> Answer i Got Below

> Saint-Saens Nielsen Easdale

>
> Is this what i was going for in the direction i was a bit confused if we
> were suppose create loops or if statements that are verified in the actual
> composers list. I don't know i feel as if i know what i need to do i just
> cant put it together.
>

At this stage of your experience, don't try to solve the whole thing in
one monolithic piece of code.

Those names shouldn't be in separate variables.  For now, just write a
function that takes a name as a parameter, and attempts to return either
True or False, to indicate whether it matches.

Then after you've debugged that, put those names back into a list, and
write a loop which calls the function you just wrote.





> --
> Dharmit Shah
> http://www.about.me/dharmit"; 
> target="_blank">www.about.me/dharmit
> ___
> Tutor maillist  -  mailto:Tutor@python.org"; 
> target="_blank">Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor"; 
> target="_blank">https://mail.python.org/mailman/listinfo/tutor
> 
> 
>

Please post in text mode.  html mail will mess you up, sooner or later. 
Besides, it adds unnecessary bulk to the message, which matters to those
of us who pay by the byte.

-- 
DaveA


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


[Tutor] (no subject)

2013-09-27 Thread Katie
Hello,


I am trying to write a program using Python v. 2.7.5 that will compute the area 
under the curve y=sin(x) between x = 0 and x = pi. Perform this calculation 
varying the n divisions of the range of x between 1 and 10 inclusive and print 
the approximate value, the true value, and the percent error (in other words, 
increase the accuracy by increasing the number of trapezoids). Print all the 
values to three decimal places. 


I am not sure what the code should look like. I was told that I should only 
have about 12 lines of code for these calculations to be done. 


I am using Wing IDE. I have very basic knowledge in Python.


I would appreciate help. 


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


Re: [Tutor] List Python Question..Please help

2013-09-27 Thread Jacqueline Canales
composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
x = 'Antheil'
s = 'Saint-Saens'
h = 'Beethoven'
y = 'Easdale'
k = 'Nielsen'

if s[0] == 'S' or s[0] == 's' == s[-1] == 'S' or s[-1] == 's':
if y[0] == 'E' or y[0] == 'e' == y[-1] == 'E' or y[-1] == 'e':
if k[0] == 'N' or k[0] == 'n' == k[-1] == 'N' or k[-1] == 'n':
print(s,k,y)
else:
print(" ")

Answer i Got Below
>>>
Saint-Saens Nielsen Easdale
>>>

Is this what i was going for in the direction i was a bit confused if we
were suppose create loops or if statements that are verified in the actual
composers list. I don't know i feel as if i know what i need to do i just
cant put it together.


On Fri, Sep 27, 2013 at 3:14 AM, 罗彭  wrote:

> Maybe the length of each name matters, too. You should consider whether to
> skip null('') and single-character('a').
>
>
> On Fri, Sep 27, 2013 at 3:57 PM, Dharmit Shah wrote:
>
>> Also, comparison is case sensitive. Meaning, 'A' and 'a' are not the same.
>>
>> Hope that helps. :)
>>
>> On Fri, Sep 27, 2013 at 1:14 PM, Amit Saha  wrote:
>> > On Fri, Sep 27, 2013 at 3:48 PM, Jacqueline Canales
>> >  wrote:
>> >> So I have been trying to do this program using ifs and or loops.
>> >> I am having a hard time solving this question, If you could please
>> assist me
>> >> in the right direction.
>> >>
>> >> Write a program that lists all the composers on the list ['Antheil',
>> >> 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen'] whose name starts
>> and ends
>> >> with the same letter (so Nielsen gets lsited, but Antheil doesn't).
>> >>
>> >> I know below it prints the entire list of composers but i dont know
>>  how to
>> >> do the program above. I think I am thinking to much into but ive
>> looked at
>> >> all my notes and online resources and having a hard time coming up with
>> >> anything.
>> >> Please help!
>> >>
>> >> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale',
>> 'Nielsen']
>> >> for person in composers:
>> >> print(person)
>> >
>> > So, here you are printing every compose as you rightly state above.
>> > What you now need to do is:
>> >
>> > For each of the composers (`person'), you need to check if the first
>> > letter and the last letter are the same. Here;s a hint:
>> >
>>  s='abba'
>> >
>> > The first letter:
>> >
>>  s[0]
>> > 'a'
>> >
>> > The last letter:
>> >
>>  s[-1]
>> > 'a'
>> >
>> >
>> > If you now compare these, you will know if they are the same and hence
>> > you print him/her.
>> >
>> > Hope that helps.
>> > -Amit.
>> >
>> >
>> > --
>> > http://echorand.me
>> > ___
>> > Tutor maillist  -  Tutor@python.org
>> > To unsubscribe or change subscription options:
>> > https://mail.python.org/mailman/listinfo/tutor
>>
>>
>>
>> --
>> Dharmit Shah
>> www.about.me/dharmit
>> ___
>> 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] List Python Question..Please help

2013-09-27 Thread 罗彭
Maybe the length of each name matters, too. You should consider whether to
skip null('') and single-character('a').


On Fri, Sep 27, 2013 at 3:57 PM, Dharmit Shah  wrote:

> Also, comparison is case sensitive. Meaning, 'A' and 'a' are not the same.
>
> Hope that helps. :)
>
> On Fri, Sep 27, 2013 at 1:14 PM, Amit Saha  wrote:
> > On Fri, Sep 27, 2013 at 3:48 PM, Jacqueline Canales
> >  wrote:
> >> So I have been trying to do this program using ifs and or loops.
> >> I am having a hard time solving this question, If you could please
> assist me
> >> in the right direction.
> >>
> >> Write a program that lists all the composers on the list ['Antheil',
> >> 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen'] whose name starts and
> ends
> >> with the same letter (so Nielsen gets lsited, but Antheil doesn't).
> >>
> >> I know below it prints the entire list of composers but i dont know
>  how to
> >> do the program above. I think I am thinking to much into but ive looked
> at
> >> all my notes and online resources and having a hard time coming up with
> >> anything.
> >> Please help!
> >>
> >> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale',
> 'Nielsen']
> >> for person in composers:
> >> print(person)
> >
> > So, here you are printing every compose as you rightly state above.
> > What you now need to do is:
> >
> > For each of the composers (`person'), you need to check if the first
> > letter and the last letter are the same. Here;s a hint:
> >
>  s='abba'
> >
> > The first letter:
> >
>  s[0]
> > 'a'
> >
> > The last letter:
> >
>  s[-1]
> > 'a'
> >
> >
> > If you now compare these, you will know if they are the same and hence
> > you print him/her.
> >
> > Hope that helps.
> > -Amit.
> >
> >
> > --
> > http://echorand.me
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
>
>
>
> --
> Dharmit Shah
> www.about.me/dharmit
> ___
> 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] Help on class

2013-09-27 Thread bharath ks
Hello,

May i know why object 'c' does not prompt for employee name and employee id in 
the following code
i get out put as 

Enter employee name:john
Enter employee id:56
Employee name is: john
Employee id is: 56

Employee name is: test
Employee id is: 1003

Employee name is: john
Employee id is: 56

class employee:

    emp_name=""
    emp_id=0
    def collect_name():
        return(input("Enter employee name:"))
   
    
    def collect_id():
        return(input("Enter employee id:"))
            
    def show(self):
        print("Employee name is:",self.emp_name)
        print("Employee id is:",self.emp_id)
        
    def __init__(self,name=collect_name(),id=collect_id()):
        self.emp_name=name
        self.emp_id=id
     
     

a=employee()
a.show()
print("")
b=employee("test",1003)
b.show()
print("")
c=employee()
c.show()


 

Thanks & BR,
Bharath Shetty___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List Python Question..Please help

2013-09-27 Thread Steven D'Aprano
Hi Jacqueline, and welcome!

Further information below...

On Fri, Sep 27, 2013 at 12:48:26AM -0500, Jacqueline Canales wrote:

> Write a program that lists all the composers on the list ['Antheil',
> 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen'] whose name starts and
> ends with the same letter (so Nielsen gets lsited, but Antheil doesn't).

> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
> for person in composers:
> print(person)


One of the secrets to programming is to write out the instructions to 
solve the problem in English (or whatever your native language is), as 
if you were giving instructions to some other person, telling them what 
to do. Then you just need to make those instructions more and more 
precise, more and more detailed, as computers are much dumber than any 
person.

For instance, you might start off with:

Look at each composer's name, and print it only if it begins and ends 
with the same letter.

The computer doesn't understand "look at each composer's name", but you 
know how to tell the computer the same thing:


composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
for person in composers:


so you're already half-way there. "print it only if" needs to be written 
the other way around:

if it begins and ends with the same letter, print it


The computer doesn't know what "it" is, so you have to spell it out 
explicitly:

if person begins and ends with the same letter, print person


Change the syntax to match what the Python programming language expects:

if person begins and ends with the same letter:
print person


Now the only bit still to be solved is to work out the "begins and ends" 
part. I'm not going to give you the answer to this, but I will give you 
a few hints. If you copy and paste the following lines into the Python 
interpreter and see what they print, they should point you in the right 
direction:


name = "Beethovan"
print "The FIRST letter of his name is", name[0]
print "The LAST letter of his name is", name[-1]

print "Are the SECOND and THIRD letters equal?", name[1] == name[2]

print "Does X equal x?", 'X' == 'x'

print "Convert a string to lowercase:", name.lower()



Good luck, and don't hesitate to come back if you need any further help.



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


Re: [Tutor] List Python Question..Please help

2013-09-27 Thread Alan Gauld

On 27/09/13 06:48, Jacqueline Canales wrote:

So I have been trying to do this program using ifs and or loops.
I am having a hard time solving this question, If you could please
assist me in the right direction.


Happy to. We won't do the homework for you but we will ask leading 
questions and give hints.



I know below it prints the entire list of composers but i dont know  how
to do the program above.


OK, breaking it into three parts:

1) print names from the list - you can do that already

2) test if a name starts and ends with the same letter
   - Amit has shown you how to get the letters, do you
 know how to test for equality (using the == operator)?

3) Filter the printout based on the test in (2)
   - Do you know how to use an if statement to take
 action depending on the outcome of a test?

Your code should then look like

for person in persons:
if 
   print person


If there are still things you are unclear about
ask here and we will break it down further.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] List Python Question..Please help

2013-09-27 Thread Dharmit Shah
Also, comparison is case sensitive. Meaning, 'A' and 'a' are not the same.

Hope that helps. :)

On Fri, Sep 27, 2013 at 1:14 PM, Amit Saha  wrote:
> On Fri, Sep 27, 2013 at 3:48 PM, Jacqueline Canales
>  wrote:
>> So I have been trying to do this program using ifs and or loops.
>> I am having a hard time solving this question, If you could please assist me
>> in the right direction.
>>
>> Write a program that lists all the composers on the list ['Antheil',
>> 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen'] whose name starts and ends
>> with the same letter (so Nielsen gets lsited, but Antheil doesn't).
>>
>> I know below it prints the entire list of composers but i dont know  how to
>> do the program above. I think I am thinking to much into but ive looked at
>> all my notes and online resources and having a hard time coming up with
>> anything.
>> Please help!
>>
>> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
>> for person in composers:
>> print(person)
>
> So, here you are printing every compose as you rightly state above.
> What you now need to do is:
>
> For each of the composers (`person'), you need to check if the first
> letter and the last letter are the same. Here;s a hint:
>
 s='abba'
>
> The first letter:
>
 s[0]
> 'a'
>
> The last letter:
>
 s[-1]
> 'a'
>
>
> If you now compare these, you will know if they are the same and hence
> you print him/her.
>
> Hope that helps.
> -Amit.
>
>
> --
> http://echorand.me
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



-- 
Dharmit Shah
www.about.me/dharmit
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List Python Question..Please help

2013-09-27 Thread Amit Saha
On Fri, Sep 27, 2013 at 3:48 PM, Jacqueline Canales
 wrote:
> So I have been trying to do this program using ifs and or loops.
> I am having a hard time solving this question, If you could please assist me
> in the right direction.
>
> Write a program that lists all the composers on the list ['Antheil',
> 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen'] whose name starts and ends
> with the same letter (so Nielsen gets lsited, but Antheil doesn't).
>
> I know below it prints the entire list of composers but i dont know  how to
> do the program above. I think I am thinking to much into but ive looked at
> all my notes and online resources and having a hard time coming up with
> anything.
> Please help!
>
> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
> for person in composers:
> print(person)

So, here you are printing every compose as you rightly state above.
What you now need to do is:

For each of the composers (`person'), you need to check if the first
letter and the last letter are the same. Here;s a hint:

>>> s='abba'

The first letter:

>>> s[0]
'a'

The last letter:

>>> s[-1]
'a'


If you now compare these, you will know if they are the same and hence
you print him/her.

Hope that helps.
-Amit.


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


[Tutor] List Python Question..Please help

2013-09-27 Thread Jacqueline Canales
So I have been trying to do this program using ifs and or loops.
I am having a hard time solving this question, If you could please assist
me in the right direction.

Write a program that lists all the composers on the list ['Antheil',
'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen'] whose name starts and
ends with the same letter (so Nielsen gets lsited, but Antheil doesn't).

I know below it prints the entire list of composers but i dont know  how to
do the program above. I think I am thinking to much into but ive looked at
all my notes and online resources and having a hard time coming up with
anything.
Please help!

composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
for person in composers:
print(person)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor