Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between argv and raw_input () of python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the differences between python's argv and raw_input ()". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

From sys import argv

Script, first, second, third = argv

Print "The script is called:", script

Print "Your first variable is:", first

Print "Your second variable is:", second

Print "Your third variable is:", third

In the first line of code, we use an import statement, which is how to add the functional modules of Python to your own script. Python does not provide you with all its functions at once, but allows you to call whatever you need. This makes your program more concise, and when later programmers see your code, these "import" statements can be used as a hint to let them know what functions your code uses.

Argv is a so-called "parameter variable", which is a very standard programming term. You can also see it in other programming languages. This variable contains the parameters you passed to Python.

Line 3 of the code "unpack" argv, and instead of putting all parameters under the same variable, we give each parameter a variable name: script, first, second, and third. This may seem strange, but "unpacking" is probably the best way to describe it. Its meaning is simple: "unpack the things in argv and give all the parameters to the variable name on the left in turn."

Before using import to make the program achieve more functions, we call import functions, and their real names are actually modules. Run your script as in the following example:

$python ex13.py first 2nd 3rd

The script is called: ex13.py

Your first variable is: first

Your second variable is: 2nd

Your third variable is: 3rd

If you enter different parameters each time, the output you see will be slightly different:

$python ex13.py stuff things that

The script is called: ex13.py

Your first variable is: stuff

Your second variable is: things

Your third variable is: that

$

Python ex13.py apple orange grapefruit

The script is called: ex13.py

Your first variable is: apple

Your second variable is: orange

Your third variable is: grapefruit

You can replace first, 2nd, and 3rd with any three parameters you like. If you don't run the right, you may see an error message:

$python ex13.py first 2nd

Traceback (most recent call last):

File "ex13.py", line 3, in

Script, first, second, third = argv

ValueError: need more than 3 values to unpack

This is the end of the content of "what are the differences between python's argv and raw_input ()". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report