2016-11-09 4 views
-1

私はプログラミングに慣れていて、異なる文字の印刷ステートメントで%記号が何をするのか理解しようとしています。これらのほとんどが%uが何を受け入れているのかを理解しています。 538を整数として出力するようです。私はユニコードで 'u'が印刷されたprint文の前に、%uで適用されるかどうかわかりません。pythonのシンボル%uと%o

print "In honor of the election I present %d" % 538.0 # integer 
print "In honor of the election I present %o" % 538.0 # octal 
print "In honor of the election I present %u" % 538.0 # ? 
print "In honor of the election I present %x" % 538.0 # lowercase hexadecimal 
print "In honor of the election I present %X" % 538.0 # uppercase hexadecimal 
print "In honor of the election I present %e" % 538.0 # exponential 
print "In honor of the election I present %i" % 538.0 # integer 

出力は以下の通りです:

In honor of the election I present 538 
In honor of the election I present 1032 *emphasized text* 
In honor of the election I present 538 *emphasized text* 
In honor of the election I present 21a 
In honor of the election I present 21A 
In honor of the election I present 5.380000e+02 
In honor of the election I present 538 

私もこの番号について%oで少し問題を抱えています。私はちょうど8角形の印刷が何であるかを学び、132538 --> 8^3 = 512 *(1) + 26, 8^1 = 8*(3) + 2, 8^0 = 1*(2))を出力すると考えましたが、出力は1032です。 0はどこから来たのですか? the docsから

+1

0は、その数に8^2 = 64の0が含まれているためです。 8進数132は2 * 8^0 + 3 * 8^1 + 1 * 8^2となります。 – melpomene

答えて

4

%u

廃止タイプである - それは'd'と同一です。

%oベース10として538を解釈し、進に変換するprintを伝えます。 538ベース10(538 )が進(1032 )で1032である。

1 * 8^3 + 0 * 8^2 + 3 * 8^1 + 2 * 8^0 
= 512 + 0 + 24 + 2 
= 538 

ものは8 Nの適切な係数であるので、それは、1032を示しています。 0は、8 に対応する。あなたはそれを残している場合は、132 = 1 * 64 + 3 * 8 + 2 = 90 、ない

ので538、そこに奇妙な何もしなければならないと思います。

関連する問題