python 2 和python 3 统一小窍门,python 2 加入下面一行代码
from __future__ import print_function
字面意思即为,从未来加载模块。
warming up
print('size of image:',256)
('size of image:', 256)
print 'Price of eggs: $%d' % 42
Price of eggs: $42
(C语言格式,‘d%’被替换)
怎么换行?
print(2,3,4,'\n',5,6,7)
print("When it gets hard, that's \n when he has got to stick it out.")
2 3 4
5 6 7
When it gets hard, that's
when he has got to stick it out.
format用法
大括号!'{ }'.format( )。{}其实是set。
a= 1
b = 2
a = a + b
b = a - b
a = a - b
print ('a = {0}, b = {1}'.format(a,b))
print ("a={1} b={0} {1}".format("hello", "world"))
a = a ^ b
b = a ^ b
a = a ^ b
print 'a=%d' %a +',b=%d' %b
输出
a = 2, b = 1
a=world b=hello world
a=1,b=2
上述植入了两种交换a和b值的例子,^是按位异或。
if i % 30 == 0:
print("image:", '%04d' % (i+1),\
"cost=", "{:.9f}".format(loss))
('image:', '0001', 'cost=', '3.899574518')
('image:', '0031', 'cost=', '2.469503164')
('image:', '0061', 'cost=', '1.211874366')
('image:', '0091', 'cost=', '0.633342564')
('image:', '0121', 'cost=', '0.465918243')
.9f 的意思是:dot后面是精度,如
>>>from math import pi
>>>"Pi with three decimals: %.3f"% pi
Pi with three decimals: 3.142
dot前是指定位宽,指的是整个数的位宽(包括小数位),这样做的目的之一是前面补0。
>>>'%04d'% pi
'0003'
>>>'%20.2f' % pi
' 3.14'
不补0那么就是空格。这种用法在制表对齐时很有用。
k=2
validation_accuracy=0.3
print('epoch %d : complete,Validation accuracy = %.1f%%' % (k,validation_accuracy * 100.0))
epoch 2 : complete,Validation accuracy = 30.0%
acc = 0.759876
print("Epoch:", '%04d' % 1,
"cost=", "{:.9f}".format(20),
"Training accuracy","{:.5f}%".format(acc*100))
Epoch: 0001 cost= 20.000000000 Training accuracy 75.98760%
\033[1;033 #颜色打印(蓝色)
\033[0m #重置print
patient_n=5
epoch=1
validation_accuracy = 0.8
print("\033[1;031m",end="")
print("Step: ", 2, "Cost: ",3, "IoU:", 4)
print('\033[1;034m Patient %d complete, epoch %d complete,Validation accuracy = %.1f%%' % (patient_n, epoch,validation_accuracy * 100.0))
print('\033[0m Saving...')