Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37912 > unrolled thread
| Started by | su29090 <129km09@gmail.com> |
|---|---|
| First post | 2013-01-29 19:26 -0800 |
| Last post | 2013-01-30 04:43 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
struggling with these problems su29090 <129km09@gmail.com> - 2013-01-29 19:26 -0800
Re: struggling with these problems MRAB <python@mrabarnett.plus.com> - 2013-01-30 03:59 +0000
Re: struggling with these problems Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-30 04:43 +0000
| From | su29090 <129km09@gmail.com> |
|---|---|
| Date | 2013-01-29 19:26 -0800 |
| Subject | struggling with these problems |
| Message-ID | <72be91aa-b9fc-4225-bd54-0ae3459acea3@googlegroups.com> |
1.Given that worst_offenders has been defined as a list with at least 6 elements, write a statement that defines lesser_offenders to be a new list that contains all the elements from index 5 of worst_offenders and beyond. Do not modify worst_offenders . I tried this but it didn't work: lesser_offenders = worst_offenders[5:6] 2.Given a variable temps that refers to a list, all of whose elements refer to values of type float , representing temperature data, compute the average temperature and assign it to a variable named avg_temp . Besides temps and avg_temp , you may use two other variables -- k and total . I'm not sure about this one but this is what I have: for k in range(len(temps)): total += temps[k] avg_temp = total / len(temps) 3.Associate the sum of the non-negative values in the list numbers with the variable sum . is it this: for numbers in sum: if sum +=? I'm confused at #3 the most i'm not doing it in python 3.2.3 it's called Myprogramminglab.
[toc] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2013-01-30 03:59 +0000 |
| Message-ID | <mailman.1200.1359518372.2939.python-list@python.org> |
| In reply to | #37912 |
On 2013-01-30 03:26, su29090 wrote: > 1.Given that worst_offenders has been defined as a list with at least 6 elements, write a statement that defines lesser_offenders to be a new list that contains all the elements from index 5 of worst_offenders and beyond. Do not modify worst_offenders . > > I tried this but it didn't work: > > lesser_offenders = worst_offenders[5:6] > Python uses half-open ranges (and counts from 0), which means that the start index is included and the end index is excluded. Therefore, worst_offenders[5:6] means the slice from index 5 up to, but excluding, index 6; in other words, an empty list. The question says "and beyond"; in Python you can just omit the end index to indicate everything up to the end. > 2.Given a variable temps that refers to a list, all of whose elements refer to values of type float , representing temperature data, compute the average temperature and assign it to a variable named avg_temp . Besides temps and avg_temp , you may use two other variables -- k and total . > > > I'm not sure about this one but this is what I have: > > for k in range(len(temps)): > total += temps[k] > > avg_temp = total / len(temps) > You didn't set the initial value of total, which is 0. > 3.Associate the sum of the non-negative values in the list numbers with the variable sum . > > is it this: > > for numbers in sum: > if sum +=? > > I'm confused at #3 the most > Well, that's not valid Python. What you want to do is to add each number from the list to the sum only if it's non-negative, i.e. greater than or equal to 0. > i'm not doing it in python 3.2.3 it's called Myprogramminglab. > Have a look at Dive Into Python: http://www.diveintopython.net/
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-01-30 04:43 +0000 |
| Message-ID | <5108a50c$0$11104$c3e8da3@news.astraweb.com> |
| In reply to | #37917 |
On Wed, 30 Jan 2013 03:59:32 +0000, MRAB wrote:
> Python uses half-open ranges (and counts from 0), which means that the
> start index is included and the end index is excluded.
>
> Therefore, worst_offenders[5:6] means the slice from index 5 up to, but
> excluding, index 6; in other words, an empty list.
Er, no. It's a one-element list: index 5 is included, index 6 is excluded.
py> L = list("abcdefgh")
py> L[5:6]
['f']
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web