Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 739335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:23:19+00:00 2026-05-14T08:23:19+00:00

In the below code I understand that sys.argv uses lists, however I am not

  • 0

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] 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:23:19+00:00Added an answer on May 14, 2026 at 8:23 am

    Let’s say on the command-line you have:

    C:\> C:\Documents and Settings\fred\My Documents\Downloads\google-python-exercises
    \google-python-exercises\hello.py John
    

    To make it easier to read, shorten it to:

    C:\> hello.py John
    

    argv represents all the items that come along via the command-line input, but counting starts at zero (0) not one (1): in this case, "hello.py" is element 0, "John" is element 1

    In other words, sys.argv[0] == 'hello.py' and sys.argv[1] == 'John' … but look, how many elements is this? 2, right! so even though the numbers are 0 and 1, there are 2 elements here.

    len(sys.argv) >= 2 just checks whether you entered at least two elements. in this case, we entered exactly 2.

    Now let’s translate your code into English:

    define main() function:
        if there are at least 2 elements on the cmd-line:
            set 'name' to the second element located at index 1, e.g., John
        otherwise there is only 1 element... the program name, e.g., hello.py:
            set 'name' to "World" (since we did not get any useful user input)
        display 'Hello' followed by whatever i assigned to 'name'
    

    So what does this mean? It means that if you enter:

    • "hello.py", the code outputs "Hello World" because you didn’t give a name
    • "hello.py John", the code outputs "Hello John" because you did
    • "hello.py John Paul", the code still outputs "Hello John" because it does not save nor use sys.argv[2], which was "Paul" — can you see in this case that len(sys.argv) == 3 because there are 3 elements in the sys.argv list?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 501k
  • Answers 501k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here's my elegant version in C#: Console.Write(File.ReadLines(@"s:\source\transactions.dat").ElementAt(219506323)); or more general:… May 16, 2026 at 2:13 pm
  • Editorial Team
    Editorial Team added an answer I'm not sure what kinds of queries you'll need to… May 16, 2026 at 2:13 pm
  • Editorial Team
    Editorial Team added an answer I've created such gesture trigger. And I'd like to share… May 16, 2026 at 2:13 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Below is the code snippet with comments which describes the problem statement. We have
Why does the below code prints 2147483647, the actual value being 2147483648? i =
My ultimate aim is to convert the below code in python to C#, but
I'm not sure I really understand dependency management. Does it mean you're only not
I know this does not sound Pythonic, but bear with me for a second.
I have just started programming in Objective-C, I understand it only partially supports method
I know this has been asked many times, but I don't understand what is
How can I unmarshal a message that was marshalled using a QName local part
I have a NSURLConnection that receives data output from a url pointing to a
I was looking at the assembler output of my code and need help with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.