Posts: 30
Threads: 4
Joined: Jan 2019
(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 ??
Posts: 1,950
Threads: 8
Joined: Jun 2018
(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"
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.
Posts: 30
Threads: 4
Joined: Jan 2019
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?
Posts: 1,950
Threads: 8
Joined: Jun 2018
(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.
Posts: 30
Threads: 4
Joined: Jan 2019
Then why you used it (zip) since the exercise refers to list?
Posts: 1,950
Threads: 8
Joined: Jun 2018
Feb-03-2019, 12:38 PM
(This post was last modified: Feb-03-2019, 12:38 PM by perfringo.)
(Feb-03-2019, 12:29 PM)sonedap Wrote: Then why you used it (zip) since the exercise refers to list? 
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.
Posts: 30
Threads: 4
Joined: Jan 2019
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
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
|