m = input()
bin_m = bin(M)[2:][::-1]
print(bin_m)
我正在查看一个代码,该代码要求我将十进制数转换为二进制,并且它们使用了:
bin_m = bin(M)[2:][::-1]
将m转换为二进制。
有人,请帮助我
[2:][::-1]
是什么意思
答案 0 :(得分:0)
"""this is string slicing. It cuts a portion of string which you have provided it. For this you need to know string indexing.
suppose you are having a string "Python". Then each character of this string is having its index number. The index number starts from 0 from left to right, and -1 from reversed side
Character --> index number when you read the string from right to left --> index number when you read the string from right to left(reversed side)
P --> 0 --> -6
y --> 1 --> -5
t --> 2 --> -4
h --> 3 --> -3
o --> 4 --> -2
n --> 5 --> -1
Using these index numbers you can also access the characters from your string. For example:
{1} If you want print "P" from Python then"""
string = "Python"
print(string[0])
"""Result is P
Similarly you can access all the characters from your string
print(string[1], string[2])...
You can also use negative indexes like -1, -2..."""
print(string[-1])
# Result will be n
"""Now by using these indexes you can do string slicing
Syntax : variable_name[start_argument : stop_argument : step_argument]
start argument --> from where string slicing will start(index number)
stop argument --> till where the slicing will stop(index number) + 1
Now how to do string slicing
Suppose that you are having a string "Hello World" then you can perform slicing on this string"""
new_var = "Hello World"
print(new_var[0 : 5])
"""Now it will give me output Hello because it will slice the string from index 1 to index 4 because the value gets decreased by 1 in stop argument in stop argument
You can also give step argument"""
print(new_var[1 : 8 : 2])
"""At first our variable will get sliced that is "ello Wo"(between index 1 to 7) now our program will skip a word and then pick next word due to step argument
So our output will be "el W" rest all characters are skipped due to step argument
Note : By default the value of start argument is 0, stop argument is length of string and step argument is 1 so if you write var_name[::] then your full string will get printed
If we do not give to start argument then its value will be set to 0 and our string will be printed from beginning to character whose index number is stop argument -> 1"""
new_var[:9]
"""Result is Hello Wor
012345678
If we don't give the stop argument then by default the value will be set to length of string and our string will be sliced from start argument till the end"""
new_var[4:]
"""Result is o World
45678910
Here is also a trick to reverse your string"""
new_var[::-1]
"""Result is dlroW olleH
This happens because we have neither given the start argument nor the stop argument so our whole string is sliced. If we give negative value then our characters will be skipped from right to left i.e. reversed also the step argument value is 1(negative) so no character is skipped
I hope this helps you!!"""