inserting characters at the start and end of a string

Strings are immutable so you can’t insert characters into an existing string. You have to create a new string. You can use string concatenation to do what you want: Note that you can also create a string with n Ls by using multiplication:

Implementing an efficient queue in Python

As Uri Goren astutely noted above, the Python stdlib already implemented an efficient queue on your fortunate behalf: collections.deque. What Not to Do Avoid reinventing the wheel by hand-rolling your own: Linked list implementation. While doing so reduces the worst-case time complexity of your dequeue() and enqueue() methods to O(1), the collections.deque type already does so. It’s also thread-safe and presumably more space and time … Read more

HTTPS connection Python

Python 2.x: docs.python.org/2/library/httplib.html: Note: HTTPS support is only available if the socket module was compiled with SSL support. Python 3.x: docs.python.org/3/library/http.client.html: Note HTTPS support is only available if Python was compiled with SSL support (through the ssl module). To verify if SSL is enabled, try:

How do I get a python program to do nothing?

You could use a pass statement: Python 2.x documentation Python 3.x documentation However I doubt you want to do this, unless you just need to put something in as a placeholder until you come back and write the actual code for the if statement. If you have something like this: You can in general change it to this: But … Read more

How do I find the duplicates in a list and create another list with them?

To remove duplicates use set(a). To print duplicates, something like: Note that Counter is not particularly efficient (timings) and probably overkill here. set will perform better. This code computes a list of unique elements in the source order: or, more concisely: I don’t recommend the latter style, because it is not obvious what not seen.add(x) is doing (the set add() method always returns None, hence … Read more

Decorators with parameters?

The syntax for decorators with arguments is a bit different – the decorator with arguments should return a function that will take a function and return another function. So it should really return a normal decorator. A bit confusing, right? What I mean is: Here you can read more on the subject – it’s also possible to implement … Read more