Learn Python The Hard Way learning (13)-parameters, unpacking, variables

Source: Internet
Author: User

In the following example, we will pass a variable to the script. Do you know why you enter python ex13.py to execute the ex13.py file? The command "ex13.py" is actually a "parameter". Let's write a script that can accept parameters.
[Python]
1. from sys import argv
2.
3.
4. script, first, second, third = argv
5.
6.
7. print "The script is called:", script
8. print "Your first variable is:", first
9. print "Your second variable is:", second
10. print "Your third variable is:", third

In the first line, we see "import", which is a method for importing functions from the python function library. Instead of importing all functions, we import what you want to import, in this way, the program is concise and easy for other programmers to read.

Argv is a "parameter variable", a very standard variable name, which can be seen in other programming languages. This variable saves all the parameters when you run the script.

The function of Row 3 is to unpack the variable argv and assign it to four variables: script, first, second, and third.

Wait, the feature has another name.
The real name of "features" is: modules. Of course, some people call it "libraries", but let's call it a module.

Running result
Different running parameters produce different results.
Root @ he-desktop :~ /Mystuff # python ex13.py first 2nd 3nd
The script is called: ex13.py
Your first variable is: first
Your second variable is: 2nd
Your third variable is: 3nd
Root @ he-desktop :~ /Mystuff # python ex13.py cheese apples bread
The script is called: ex13.py
Your first variable is: cheese
Your second variable is: apples
Your third variable is: bread
Root @ he-desktop :~ /Mystuff # python ex13.py Zed A. Shaw
The script is called: ex13.py
Your first variable is: Zed
Your second variable is:.
Your third variable is: Shaw

You can change the values of the following three parameters at will. However, if you only enter two parameters, the following error is returned:
Traceback (most recent call last ):
File "ex13.py", line 3, in <module>
Script, first, second, third = argv
ValueError: need more than 3 values to unpack
It indicates that the number of parameters is not three.

Extra score exercise
1. Try to enter less than 3 parameters to see what the error is like?
The error is as follows:

2. Write a script with fewer parameters and a script with more parameters to give the unwrapped variables a well-understood name.

3. Write a script with raw_input and argv.
[Python]
1. from sys import argv
2.
3.
4. script, first = argv
5.
6.
7. print "How % s are your? "% First
8. write = raw_input ()
9. print "Your % s is % s." % (first, write)

Output:
Root @ he-desktop :~ /Mystuff # python ex13.py old
How old are your? Www.2cto.com
25
Your old is 25.

4. Remember that modules provides functions for us and will be used later.

Author: lixiang0522

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.