Sep-30-2018, 03:48 PM
Hi folks,
I am an older fella, who has worked in and around IT for many years and who decided to take on a new challenge and learn to code - I haven't done any coding since about 1987 when I did a small amount of C and PASCAL in college when I left the military. My Cyber Security colleagues convinced me that Python was the way to go so i've got an online tutorial underway along with an eBook from NoStarchPress.
Because my brain doesn't retain things as it did in 1987 I need to do and redo some stuff to get it to sink in so I try to adapt some of the exercises and lessons after we complete a section to confirm my understanding. When we were playing with lists and loops I tried to combine the two subjects and I have some unexpected behaviour. I am hoping that I can develop my skills with the help of the forum and later give back to any other newcomers, but for now I am afraid this is brand new to me - Ive been at it for two weekends.
I am using a Mac on High Sierra, Sublime Text as an editor, pre-instaalled Python (2.7.10)
this is my simple code to practice with lists and loops, I create a list, add to the list, remove items from the list and move them to a second list:
This is the output:
I am an older fella, who has worked in and around IT for many years and who decided to take on a new challenge and learn to code - I haven't done any coding since about 1987 when I did a small amount of C and PASCAL in college when I left the military. My Cyber Security colleagues convinced me that Python was the way to go so i've got an online tutorial underway along with an eBook from NoStarchPress.
Because my brain doesn't retain things as it did in 1987 I need to do and redo some stuff to get it to sink in so I try to adapt some of the exercises and lessons after we complete a section to confirm my understanding. When we were playing with lists and loops I tried to combine the two subjects and I have some unexpected behaviour. I am hoping that I can develop my skills with the help of the forum and later give back to any other newcomers, but for now I am afraid this is brand new to me - Ive been at it for two weekends.
I am using a Mac on High Sierra, Sublime Text as an editor, pre-instaalled Python (2.7.10)
this is my simple code to practice with lists and loops, I create a list, add to the list, remove items from the list and move them to a second list:
# -*- coding: utf-8 -*- # start with a newlist guests = ['David', 'Adolf','Charles'] print(guests) # add some more entries to the list, add to first, second and last guests.insert(0, 'Geoff') guests.insert(1, 'Colin') guests.append('Fred') print(guests) # print the list to check its valid print('\n') # start to remove guests and add them to an alternate list removedlist = [] print('there are ' + str(len(guests)) + ' items in the list \n') for guest in guests: popped=guests.pop() removedlist.append(popped) print('last popped value - ' + popped) print('current guests list') print(guests) print('the following have been removed') print(removedlist) print('\n')I expected the for loop to iterate six times and for all the entries in "guests" to end up in "removedlist" but it only appears to iterate three times:
This is the output:
Output:['David', 'Adolf', 'Charles']
['Geoff', 'Colin', 'David', 'Adolf', 'Charles', 'Fred']
there are 6 items in the list
last popped value - Fred
current guests list
['Geoff', 'Colin', 'David', 'Adolf', 'Charles']
the following have been removed
['Fred']
last popped value - Charles
current guests list
['Geoff', 'Colin', 'David', 'Adolf']
the following have been removed
['Fred', 'Charles']
last popped value - Adolf
current guests list
['Geoff', 'Colin', 'David']
the following have been removed
['Fred', 'Charles', 'Adolf']
[Finished in 0.1s]
All help and guidance is appreciated on both this query and the wider "how to learn python" question