Я новичок в программировании и пытаюсь выяснить, что символ% делает в печатных заявлениях с разными буквами. Я понимаю, что почти все они принимают за то, что делает% u. Кажется, он просто печатает 538 как целое число. Я видел, что перед оператором печати с печатью «u» в юникоде, но я не знаю, относится ли оно к% u.python symbol% 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
для этого номера. Я только что узнал, что печатает как восьмеричный, и я решил, что он выведет 132
(538 --> 8^3 = 512 *(1) + 26, 8^1 = 8*(3) + 2, 8^0 = 1*(2)
), но вывод 1032
. Откуда приходит 0?
0, потому что в этом номере 0 раз 8^2 = 64. Октал 132 был бы 2 * 8^0 + 3 * 8^1 + 1 * 8^2. – melpomene