Python sys.argv lists and indexes
- by Fred Gerbig
In the below code I understand that sys.argv uses lists, however I am not clear on how the index's are used here.
def main():
if len(sys.argv) >= 2:
name = sys.argv[1]
else:
name = 'World'
print 'Hello', name
if __name__ == '__main__':
main()
If I change
name = sys.argv[1]
to
name = sys.argv[0]
and type something for an argument it returns:
Hello C:\Documents and Settings\fred\My Documents\Downloads\google-python-exercises
\google-python-exercises\hello.py
Which kind of make sense.
Can someone explain how the 2 is used here:
if len(sys.argv) >= 2:
And how the 1 is used here:
name = sys.argv[1]