Python Forum
Absolutely new to python - basic advise needed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Absolutely new to python - basic advise needed
#1
Dear community,

I am absolutely new to Python but like to learn it.
Could you pls help to understand following code ( I dont understand the bold portion of the code):

# enumerate function in loops 
l1 = ["eat","sleep","repeat"] 
  
# printing the tuples in object directly 
for ele in enumerate(l1): 
    print (ele) 

# changing index and printing separately
# I don't understand next part
for count,ele in enumerate(l1,100): 
    print (count,ele) 
So i dont understand what does count,ele is ? Shouldn't it be one variable after for? what is the comma in between count and ele? And why it removes parentheses in output?
Reply
#2
enumearte(ietrable, start) takes 2 arguments - iterable and optional start argument.
In your case these is l1 list and start is 100.
enumerate will return 2-element tuple (index, element).
this tuple is unpacked into two variables - count and ele. so in each iteration count takes the value of index and ele is the element

the docs: https://docs.python.org/3/library/functi...#enumerate
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
To add to buran's reply

Wording taken direct from the python docs Wrote:
d = 1, 2, 3
The statement t = 12345, 54321, 'hello!' is an example of tuple packing: the values 12345, 54321 and 'hello!' are packed together in a tuple.
The reverse operation is also possible:

x, y, z = t
This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires that there are as many variables on the left side of the equals sign as there are elements in the sequence. Note that multiple assignments are really just a combination of tuple packing and sequence unpacking.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python issue - Data science - Help is needed yovel 2 2,016 Jul-29-2021, 04:27 PM
Last Post: yovel
  Basic python Natters10 3 3,095 Nov-29-2020, 07:04 AM
Last Post: Love2code
  Help needed for a python package keysson 1 2,219 Sep-02-2020, 03:37 AM
Last Post: Larz60+
  I need advise with developing a brute forcing script fatjuicypython 11 5,072 Aug-21-2020, 09:20 PM
Last Post: Marbelous
  Help with basic python AaronG123 4 2,259 Nov-14-2019, 02:57 PM
Last Post: AaronG123
Smile Help needed. Python Newbie!, it will be fun. knightdea 3 2,633 Oct-13-2019, 08:50 AM
Last Post: perfringo
  Please, advise collections for my task AlekseyPython 1 2,107 Sep-11-2019, 12:44 PM
Last Post: perfringo
  help needed with python launcher fallenlight 3 3,379 Jan-19-2019, 01:06 PM
Last Post: snippsat
  Basic Help Needed JohnV 2 2,401 Jan-09-2019, 09:43 PM
Last Post: buran
  Very basic programming help needed: pig latin program bstocks 2 7,998 Dec-02-2017, 08:12 AM
Last Post: RickyWilson

Forum Jump:

User Panel Messages

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