Bottom Page

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 ??
Quote
#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
Quote
#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?
Quote
#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
Quote
#15
Then why you used it (zip) since the exercise refers to list? Huh
Quote
#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
Quote
#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
Quote
#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

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Help in exercise for loop kais92 13 600 Dec-01-2018, 05:16 PM
Last Post: kais92
  Python exercise janaraguz 2 342 Sep-22-2018, 08:43 PM
Last Post: ichabod801
  py4e book exercise not working when compiled adriand 10 1,198 Jun-13-2018, 01:01 PM
Last Post: adriand
  Help with Dictionaries and Lists of Lists Nate5800 5 657 Apr-23-2018, 06:42 PM
Last Post: Nate5800
  Help with test exercise lm777 3 772 Feb-21-2018, 10:42 PM
Last Post: nilamo
  John Guttag Book - Finger Exercise 4 - need help to make the code better pritesh 9 1,673 Feb-13-2018, 10:31 PM
Last Post: pritesh
  This is an open book home assignment but i really need your help Samiha 3 1,183 Oct-04-2017, 05:39 PM
Last Post: nilamo
  Simple exercise - how to write in python tomtom55 6 1,580 Sep-28-2017, 07:18 PM
Last Post: nilamo
  Help with an exercise Cerobug 8 1,131 Sep-20-2017, 09:10 PM
Last Post: nilamo
  The Python Book altered rock paper scissors Python_Noob 0 818 Sep-18-2017, 06:13 AM
Last Post: Python_Noob

Forum Jump:


Users browsing this thread: 1 Guest(s)