Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Homework help python

    hello i have a question about this python program, i dont understand how does that append fucntion works, i know that first if i>0 will be used but dont understand how does that part with i-1 works, i have tried changing it to i-2 and other numbers but i couldnt find any explenation for how does it works, for result i get 0 1 0, so i hope someone cand find few moments of their time to explane me that, thanks.

    imgur: the simple image sharer

  • #2
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    105
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Have you tried opening the Python interactive shell to experiment how it works?

    All append really does is dynamically add new elements to your arrays and lists. That loop your pointing at seems pretty straight forward to me.

    It will loop through the size of that array, x and if the current index is greater than 0 then append whatever value the current index is minus one. Otherwise, append whatever value PLUS 1. Seems to me they're doing that to prevent from referencing something outside the bounds of that array.

    Take a look at this too:
    Python List append() Method


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •