Learn python3 the stupid way pdf Baidu Cloud, how about learning python3 the stupid way

Hello everyone, I would like to share with you a stupid way to learn the fifth version of Python on Baidu Cloud. Many people still don’t know this. Let’s explain it in detail below. Now let’s take a look!

Basic exercises:
from sys import argv
#read the wyss section for how to run this
, first, second, third = argv

print("The  is called:", )
print("The first variable is called:", first)
print("The second variable is called:", sncond)
print("The third variable is called:", third)

Note: There is an import statement in the first line, which is a way to introduce a Python module (some also call it a library) into the script. Python will not give you all the modules at once, but will let you call whatever quick code paper you need . Not only does this keep the program small, but when other programmers read your code in the future, these imports can also be used as documentation.

    argv is the so-called argument variable, which is a very standard programming term. This variable holds the parameters passed to the python script when you run the script.

    The third line unpacks (UNpark) argv. Instead of putting all parameters under the same variable, it is better to assign them to four variables: first, second, third. Its meaning is very simple: "Take out the contents of argv, unpack it, and assign all parameter variables to the variables on the left in turn."

Answer: 

Note: If you run the script using the previous method, an error will be reported. Take a closer look at how it runs below (passing 3 command line parameters). This method should be used whenever argv is used.

 

 If you run it with different parameters each time (you can replace them with any parameters you want), you will see the following results:


expand: 

    1. What is the difference between argv and input(): The difference lies in the timing of user input. argv is input when the user executes the command, while input() is input during the execution of the command.

    2. Give the script less than 3 parameters and see what happens: an error is reported, there are not enough values ​​to decompress!

    3. Try using argv and input() at the same time:

from sys import argv
#read the wyss section for how to run this
, first, second, third = argv

print("The  is called:", )
print("The first variable is called:", first)
print("The second variable is called:", second)
print("The third variable is called:", third)

 = input("会报错吗?")
first = input("会报错吗?")

Finish ! ! !

Guess you like

Origin blog.csdn.net/mynote/article/details/133548745