0

# binary = "1010" flip = "" for i in binary: if i == "0": flip+ = "1" else: flip+="0" print(flip) #output:0101 # please exp

30th Oct 2021, 6:44 AM
Khalif BabyπΆ
+ 6
Hi! β1010β is a string. The for loop traverse the string, element by element, from left to right. If the element is a β0β, itβs changed to a β1β and vice versa. Then the result is printed. flip += β0β means flip = flip + β0β The two strings concatenates to one string and the result is put in the variable flip, whose value is being upgraded, like: β1β + β1β becomes β11β. In every loop the lenght of the variable flip is growing, to the size is equal to the original string β1010β. The process becomes like this: flip = ββ loop 1 -> flip = β0β loop 2 -> flip = β01β loop 3 -> flip = β010β loop 4 -> flip = β0101β So β1010β becomes β0101β.
30th Oct 2021, 7:06 AM
Per Bratthammar
+ 6
That's the place where you save your other code bits Go to code section -> click "New Code" -> select language -> write code, save code And then link the saved code here.
30th Oct 2021, 11:08 AM
Lisa
+ 5
From the top: β’ Initializes the output to the empty string β’ Looks at every character in the input string (binary), and β’ if the character is 0 it appends a 1 to the output string (flip) β’ else it appends a 0 β’ Then it prints the output string Does this explain, or is there something else you're looking for?
30th Oct 2021, 7:09 AM
Steve
+ 3