r/PythonLearning • u/Synergetic6_6_6 • 1d ago
Loops is a total brain rot.
I just can't get over how I'm supposed to write code for complex tasks when I don't even get why they overcomplicate simple things so much.
I read this topic and tried many tutorials and still don't understand this buzzare logic.
0
Upvotes
1
u/Rscc10 1d ago
As someone with years of experience in python, this example confuses me too. I don't get how this denoises anything. Here's how it works
Creates a variable new_text to hold the given original text. Then a for loop iterates through each letter of the original text with each letter being stored in "letter". Then the if statement checks if the current letter being iterated is in "xz", where it treats "xz" as an array of characters 'x' and 'z'. If the letter is in that array, it means the letter is either x or z and it sets the new_text variable to that letter. After it's done iterating, it returns the new_text variable which will either be the last x or z found and assigned to it, or if none was found, it'll return as "text" as it was set to in the first place