python tutorial - Python interview questions - learn python - python programming
python interview questions :101
Print in terminal with colors using Python?
- This somewhat depends on what platform you are on. The most common way to do this is by printing ANSI escape sequences. For a simple example, here's some python code from the blender build scripts:
Learn python - python tutorial - colors - python examples - python programs
python interview questions :102
How to convert string to lowercase in Python?
- Is there any way to convert an entire user inputted string from uppercase, or even part uppercase to lowercase.
- E.g. Kilometers --> kilometers
- This doesn't work for non-English words in UTF-8. In this case decode('utf-8') can help:
Learn python - python tutorial - lowercase - python examples - python programs
- It will then automatically convert the thing they typed into lowercase.
Note: raw_input
was renamed to input
in Python 3.x and above
python interview questions :103
How can I remove (chomp) a newline in Python?
- Python's
rstrip()
method strips all kinds of trailing whitespace by default, not just one newline as Perl does withchomp.
Learn python - python tutorial - chomp - python examples - python programs
- To strip only newlines:
- There are also the methods
lstrip()
andstrip():
python interview questions :104
- The
index()
function only returns the first occurrence, whileenumerate()
returns all occurrences. - As a list comprehension:
- This is more efficient for larger lists than using
enumerate():
Learn python - python tutorial - index - python examples - python programs
python interview questions :105
Python method to check if a string is a float ?
- A longer and more accurate name for this function could be:
isConvertibleToFloat(value)
Learn python - python tutorial - float - python examples - python programs
python interview questions :106
Hidden features of Python [closed] ?
- Try to limit answers to Python core.
- One feature per answer.
- Give an example and short description of the feature, not just a link to documentation.
- Label the feature using a title as the first line.
python interview questions :107
Get the python regex parse tree to debug your regex ?
- Regular expressions are a great feature of python, but debugging them can be a pain, and it's all too easy to get a regex wrong.
Learn python - python tutorial - hidden - python examples - python programs
- Fortunately, python can print the regex parse tree, by passing the undocumented, experimental, hidden flag
re.DEBUG
(actually, 128) tore.compile.
python interview questions :108
What is iter() can take a callable argument?
- For instance:
Learn python - python tutorial - iter - python examples - python programs
- The iter
(callable, until_value)
function repeatedly calls callable and yields its resultuntil until_value
is returned.
Learn python - python tutorial - iter-execution - python examples - python programs
python interview questions :109
What is the difference between @staticmethod and @classmethod in Python?
Classmethods :
- The static method does not take the instance as an argument.
- It is very similar to a module level function.
- However, a module level function must live in the module and be specially imported to other places where it is used.
- You can also call
class_foo
using the class. In fact, if you define something to be a classmethod, it is probably because you intend to call it from the class rather than from a class instance.A.foo(1)
would have raised a TypeError, butA.class_foo(1)
works just fine
Learn python - python tutorial - static-method - python examples - python programs
- Staticmethods :
- A class method is a similar to an instance method in that it takes an implicit first argument, but instead of taking the instance, it takes the class.
- Frequently these are used as alternative constructors for better semantic usage and it will support inheritance.
- Neither
self
(the object instance) norcls
(the class) is implicitly passed as the first argument.
Learn python - python tutorial - static-method-implementation - python examples - python programs
- They behave like plain functions except that you can call them from an instance or the class:
- Staticmethods are used to group functions which have some logical connection with a class to the class
python interview questions :110
How can I make a time delay in Python?
- In a single thread I suggest the sleep function:
- This actually suspends the processing of the thread in which it is called by the operating system, allowing other threads and processes to execute while it sleeps.
Learn python - python tutorial - time-delay - python examples - python programs
- Use it for that purpose, or simply to delay a function from executing. For example: