0

Easy Python Code for Beginner for reverse string
a = "Hello World"[ : :-1]
print(a)

Comments
  • 2
    Do it without any built in reverse functionality.
  • 6
    And people say Python is super readable, almost like pseudocode.

    Meanwhile in Swift:

    "Hello World".reversed()
  • 5
    Like @Lensflare, I don’t understand the praise Python gets for readability — especially when its significant whitespace makes following blocks so bloody difficult. That, tuples, and its bizarre slicing syntax (among other things) just look so strange.

    No, Python is kind of difficult to read, especially for a layman/nontechie.

    Meanwhile in Ruby:
    print “Hello World”.reverse
  • 2
    @Lensflare @Root Python literally has reverse(myIterable)

    You don't need to use slicing syntax
  • 2
    @lungdart Then OP’s tip is a terrible one, especially for beginners.

    Still, the slice syntax with the minus -1 has very low expressiveness and readability.
  • 0
    @Root you get used to it and at some point productivity goes way up. Then you do something else for a couple of years and you dump it all.
  • 0
    @Lensflare how do you reverse slice in your preferred language?
  • 0
    yay em dnatsrednu nac enoyreve ,hsiresrever si klat i lla
  • 0
    @lungdart what do you mean? Reversing and slicing?

    "Hello World".reversed()[2…4]
  • 0
    @Lensflare so your preferred language doesn't have reverse slice notation, but pythons syntax for it is bad?

    reverse("Hello World")[2:4] is perfectly cromulent. Python has the extra feature of using a stride in a slice. And also has same defaults to reduce verbosity

    [0:len(x)] == [0:-1] == [:] == [0:-1:1] == [::1] == [::]

    Specifying a single index gives you a single item. Specifying a first and last item gives you a slice, and specifying a first last and stride gives you a slice that can skip elements (good for batching threads, or abusing it to reverse a list)

    Worker1_jobs = jobs[::2] (even jobs)
    Worker2_jobs = jobs[1::2] (odd jobs)

    Worker1_jobs = jobs[::2]
    Worker2_jobs = jobs[1::2]
  • 0
    @lungdart accessing an element, slicing, striding and reversing all with the same compact syntax and magic numbers (-1) doesn’t make it good. It’s still an unreadable cryptic mess.

    I prefer to have separate, properly named and documented functions for that.
  • 0
    @Lensflare you don't need to use this syntax. It's short hand notation.
  • 1
  • 1
    @Lensflare be aware that using things like reverse() map() and filter() result in function calls and object creation and have a performance penalty vs doing advanced iteration patterns on the iterable. Not that this matters these days, especially Python.
  • 1
    @lungdart Swift returns iterator-like collections for those kind of operations for that exact reason.
  • 1
    @Lensflare Python has that concept too, they call them generators, but creating them is not free, more like processed just in time.
  • 0
    @Root Ruby masterace, you beat me to it
  • 1
    @lungdart nothing is free but a few function calls and memory allocations is constant time (and memory) and thus negligible in almost all of the use cases.
Add Comment