eval ()
Before print does anything, look at this stuff first. It's not useless, because it might be used at some point.
Copy the Code code as follows:
>>> Help (eval) #这个是一招鲜, if you don't understand how to use it, use this to read the document
Help on built-in function eval in module __builtin__:
Eval (...)
Eval (source[, globals[, locals]), value
Evaluate the source in the context of globals and locals.
The source may be a string representing a Python expression
Or a code object as returned by compile ().
The globals must be a dictionary and locals can is any mapping,
Defaulting to the current globals and locals.
If only globals are given, locals defaults to it.
Can read better, do not understand also has no relationship. Look what I wrote. Ha ha. To summarize, eval () computes the object in the string that conforms to the Python expression. The meaning is:
Copy the Code code as follows:
>>> 3+4 #这是一个表达式, Python calculates the results based on the rules of calculation.
7
>>> "3+4" #这是一个字符串, Python doesn't count the contents, although it's a Python-compliant expression.
' 3+4 '
>>> eval ("3+4") #这里就跟上面不一样了, the expression inside the string is calculated.
7
Here is another example of a string "add":
Copy the Code code as follows:
>>> "Qiwsir" + ". Github.io"
' Qiwsir.github.io '
>>> "' Qiwsir ' + '. Github.io '" #字符串里面, Python is not "calculated"
"' Qiwsir ' + '. Github.io '"
>>> eval ("' Qiwsir ' + '. Github.io ') #eval () do something completely different, it will calculate the inside of the string
' Qiwsir.github.io '
By the way, another function that is similar to eval () is exec (), which is designed to execute a string or Python statement inside a file.
Copy the Code code as follows:
>>> exec "print ' Hello, Qiwsir '"
Hello, Qiwsir.
>>> "print ' Hello, Qiwsir '"
"Print ' Hello, Qiwsir '"
Print Detailed
The Print command is used more in programming practice, especially to see what happens when the program runs to some point, it must be printed with print, or, more broadly speaking, to understand the output of the results from the program.
Relatively simple output, which has already been mentioned:
Copy the Code code as follows:
>>> name = ' Qiwsir '
>>> = 703
>>> website = ' Qiwsir.github.io '
>>> print "MY name is:%s\nmy is:%d\nmy website is:%s"% (name,room,website)
MY name Is:qiwsir
My is:703
My website Is:qiwsir.github.io
Where%s,%d is the placeholder.
Copy the Code code as follows:
>>> A = 3.1415926
>>> print "%d"%a #%d can only output integers, int type
3
>>> print "%f"%a #%f output floating-point number
3.141593
>>> print "%.2f"%a #按照要求输出小数位数
3.14
>>> print "%.9f"%a #如果要求的小数位数过多, followed by 0 complement
3.141592600
>>> B = 3
>>> print "%4d"%b #如果是整数, which requires the integer to occupy four positions, thus adding three spaces to the front
3 #而不是写成0003的样式
Another paradigm, written like this, is a little different from the above.
Copy the Code code as follows:
>>> Import Math #引入数学模块
>>> print "pi=%f"%math.pi #默认, the pi is printed like this
pi=3.141593
>>> print "pi=%10.3f"%math.pi #约束一下, meaning that the integer part plus the decimal and fractional parts amounts to 10 bits, and right-aligned
Pi= 3.142
>>> print "pi=%-10.3f"%math.pi #要求显示的左对齐, the rest is the same as above
pi=3.142
>>> print "pi=%06d"%int (Math.PI) #整数部分的显示, requires a total of 6 bits, so the front with 0 top up.
pi=000003
In fact, similar to the above number operation, the string can also do some constraint output operation. Look at the following experiment, the best crossing also try.
Copy the Code code as follows:
>>> website
' Qiwsir.github.io '
>>> print "%.3s"%website
Qiw
>>> print "%.*s"% (3,website)
Qiw
>>> print "%7.3s"%website
Qiw
>>> print "%-7.3s"%website
Qiw
In general, it is similar to the output operation for numbers. However, in the actual operation, these are really not many, at least in my years of code career, the use of the above complex operation, is now shown to yours faithfully, at best with a float type of data output scale operation, the other output operations, in the default way the majority. Please crossing here to despise my ignorance.
Text to this, remind yours faithfully, if using Python3, please use print (), to add a parenthesis.
Print has a feature, that is, when the output, each line is automatically followed by a newline symbol \ n, which is mentioned earlier.
Copy the Code code as follows:
>>> website
' Qiwsir.github.io '
>>> for Word in Website.split ("."):
.. print Word
...
Qiwsir
GitHub
Io
>>> for Word in Website.split ("."):
... print word, #注意, add a comma and the output will change.
...
Qiwsir GitHub IO
Is%r a panacea?
I once said that lazy people change the world, especially in the field of knocking code. So someone asked, in front of a moment is%s, a moment is%d, trouble, there is no omnipotent? So someone on the internet gave the answer,%r is omnipotent. Look at the experiment:
Copy the Code code as follows:
>>> Import Math
>>> print "pi=%r"%math.pi
pi=3.141592653589793
>>> print "pi=%r"%int (Math.PI)
Pi=3
really is omnipotent Ah! Don't worry, look at this, are you confused?
Copy the Code code as follows:
>>> print "pi=%s"%int (Math.PI)
Pi=3
Of course, this is definitely a mistake:
Copy the Code code as follows:
>>> print "p=%d"% "PI"
Traceback (most recent):
File " ", line 1, in
TypeError:%d format:a number is required, not str
If you see here, crossing is a bit confused is very normal, especially the so-called Almighty%r and%s, how can you originally belong to%d normal output?
In fact, either%r or%s (%d) converts an integer object into a string output instead of an integer. But%r and%s are a bit different, this talk about this do not do in-depth study, just to illustrate the corresponding:%s-->str ();%r-->repr (), what do you mean? This means that%s calls the STR () function to convert the object to the STR type, whereas%r calls Repr () to convert the object to a string. For the difference between the two, please refer to: difference between STR and repr in Python, here is a simple example to illustrate the difference:
Copy the Code code as follows:
>>> Import datetime
>>> today = Datetime.date.today ()
>>> today
Datetime.date (2014, 8, 15)
>>> Str (today)
' 2014-08-15 '
>>> repr (today)
' Datetime.date (2014, 8, 15) '
Finally, to express one of my views, there is nothing omnipotent, everything is based on actual needs.
For more output format placeholder description, this page has a table, unfortunately did not find Chinese, if crossing find Chinese, please share it: string formatting
Re-expansion
Copy the Code code as follows:
>>> MyInfo
{' website ': ' Qiwsir.github.io ', ' name ': ' Qiwsir ', ' 703}
>>> print "Qiwsir is in% (guest) d"%myinfo
Qiwsir is in 703
Does the crossing see the output above? It's kind of interesting. This output is an extension of the previous output.
Out of this extension, in the output, you can also use a name: format of the things, here do not see%, but more {}. See the experiment First:
Copy the Code code as follows:
>>> print "My name is {0} and I am in {1}". Format ("Qiwsir", 703) #将format后面的内容以此填充
My name is Qiwsir and I am in 703
>>> "My website is {website}". Format (website= "Qiwsir.github.io") #{} that's the equivalent of a variable.
' My website is Qiwsir.github.io '
See here, is not feeling this format a little meaning? Not lose to the previous output mode. It is said that format will gradually replace the previous. With regard to format, I plan to continue in the next lecture. Here is just a primer, behind the use of format output to make more points.