Python Forum
Book exercise in lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Book exercise in lists
#11
(Feb-02-2019, 11:32 PM)perfringo Wrote:
(Feb-02-2019, 10:58 PM)sonedap Wrote: The zip part and f' I haven't found them in the books yet so I am trying to solve it with using for or while.. The simplest way possible at the moment!
(if I succeed I will be happiest person in planet Earth)

zip() is Python built-in function and you can read about in Python documentation.

f-string is available in Python 3.6 or newer. You can read about them in PEP 498 -- Literal String Interpolation .

Don't limit yourself to 'the book'. Be curious and try to learn and understand whether it is in book or not.

If you want to limit yourself only to for-loop then you can to something like that:

>>> sequentials = ['first', 'second', 'third', 'fourth']
>>> for i in range(1, 5):
...     print('The {} element of the list is {} with sum {}'.format(sequentials[i-1], list(range(1, i+1)), sum(range(1, i+1))))
The first element of the list is [1] with sum 1
The second element of the list is [1, 2] with sum 3
The third element of the list is [1, 2, 3] with sum 6
The fourth element of the list is [1, 2, 3, 4] with sum 10
This code uses .format method which is somewhat 'older' nowadays, one resource to get acquainted with this string metohd is Using % and .format() for great good!.

Iunderstand what you are saying but isn't safer for someone who started learning Python now to follow the "book" to understand the basics ??
Reply


Messages In This Thread
Book exercise in lists - by sonedap - Feb-02-2019, 06:19 PM
RE: Book exercise in lists - by ichabod801 - Feb-02-2019, 06:58 PM
RE: Book exercise in lists - by sonedap - Feb-02-2019, 07:51 PM
RE: Book exercise in lists - by perfringo - Feb-02-2019, 10:15 PM
RE: Book exercise in lists - by ichabod801 - Feb-02-2019, 09:15 PM
RE: Book exercise in lists - by sonedap - Feb-02-2019, 10:08 PM
RE: Book exercise in lists - by sonedap - Feb-02-2019, 10:24 PM
RE: Book exercise in lists - by perfringo - Feb-02-2019, 10:50 PM
RE: Book exercise in lists - by sonedap - Feb-02-2019, 10:58 PM
RE: Book exercise in lists - by perfringo - Feb-02-2019, 11:32 PM
RE: Book exercise in lists - by sonedap - Feb-03-2019, 08:53 AM
RE: Book exercise in lists - by perfringo - Feb-03-2019, 09:11 AM
RE: Book exercise in lists - by sonedap - Feb-03-2019, 10:03 AM
RE: Book exercise in lists - by perfringo - Feb-03-2019, 10:09 AM
RE: Book exercise in lists - by sonedap - Feb-03-2019, 12:29 PM
RE: Book exercise in lists - by perfringo - Feb-03-2019, 12:38 PM
RE: Book exercise in lists - by sonedap - Feb-03-2019, 06:39 PM
RE: Book exercise in lists - by ichabod801 - Feb-03-2019, 08:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  py4e book exercise not working when compiled adriand 11 8,937 Jul-01-2020, 08:42 AM
Last Post: tawhidbd1248
  John Guttag Book - Finger Exercise 4 - need help to make the code better pritesh 12 10,693 May-06-2020, 05:10 PM
Last Post: riteshp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020