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
#12
(Feb-03-2019, 08:53 AM)sonedap Wrote: Iunderstand what you are saying but isn't safer for someone who started learning Python now to follow the "book" to understand the basics ??

People, their motivation, abilities and time available differ greatly. Therefore there is no one 'right' way to learn. One is for sure - mastering something is not an easy task.

If you are beginner you have no way of knowing whether the material you are using is good or not. For example, based on posts on this forum it seems to me that Python 2 is still in widespread use in teaching Python despite the fact that it support will end at 31.12.2019. If someone using Python 2 for teaching beginners in my eyes it qualifies as 'very-very-very bad material'.

If you want to 'follow the book' then you could choose Python documentation. If you notice there is material which is marked with "keep this under your pillow" Smile

You also should make subtle distinction between learning programming and learning Python.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#13
Well, I am looking for zip and.format to read their uses and
Zip is saying that is used for Tuples.
Isn't Tuple different from List?
Reply
#14
(Feb-03-2019, 10:03 AM)sonedap Wrote: Isn't Tuple different from List?

Yes, tuple and list are different types of sequences in Python. Tuples are immutable and lists are mutable. You can learn all types from Python documentation: Built-in Types
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#15
Then why you used it (zip) since the exercise refers to list? Huh
Reply
#16
(Feb-03-2019, 12:29 PM)sonedap Wrote: Then why you used it (zip) since the exercise refers to list? Huh

There can be any iterable as zip argument (including list and tuple). There is no tuple in code outputs (only string, list and integer).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#17
i tried to run the part of the codes you wrote like this one:
>>> for i in range(1, 5):
...    print(f'The first element of the list is {list(range(1, i+1))} with sum {sum(range(1, i+i))}')
...
The first element of the list is [1]with sum 1
The first element of the list is [1, 2]with sum 3
The first element of the list is [1, 2, 3]with sum 6
The first element of the list is [1, 2, 3, 4]with sum 10
and i am getting
Error:
print(f'The element of the list is {list(range(1, i+1))} with sum {sum(range(1, i+i))}') ^ SyntaxError: invalid syntax
Reply
#18
f-strings are only available in Python 3.6 or later. If you are stuck using an earlier version of Python, I would suggest using the format method, which I believe was shown earlier in this thread.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  py4e book exercise not working when compiled adriand 11 8,794 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,521 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