Posts

Showing posts with the label programming

Explain about assert statement?

Assertion : An assertion is a sanity-check that you can turn on or turn off when you are done with your testing of the program. We often place assertions at the start of a function to check for valid input, and after a function call to check for valid output. The assert Statement  : When encounters an assert statement, Python evaluates the accompanying expression, which is hopefully true. If the expression is false, Python raises an AssertionError exception. Syntax : assert Expression[, Arguments] If the assertion fails, Python uses ArgumentExpression as the argument for the AssertionError. AssertionError exceptions can be caught and handled like any other exception using the try-except statement, but if not handled, they will terminate the program and produce a traceback. Ex : >>> def addOne(n): ...     assert (n >= 0),"n should be greator than or equal to 0." ...     return n + 1 ...  >>> addOne(1) 2 >>> addOne

What is a negative index?

Negative index in python : Python list items can be accessed with positive or negative index.  If list of size n then  0 positive index // first index 1 positive index // second index (n-1) // last index -n // first index -(n-1) // second -1 // last index List indexes of -x mean the xth item from the end of the list, so n[-1] means the last item in the list n. Negative numbers mean that you count from the right instead of the left. So, list[-1] refers to the last element, list[-2] is the second-last, and so on. Ex:  If we have a list a = [1,2,3,4] then a[-1] will refer to 4 and a[-2] refer to 3.

What is __repr__ function?

__repr__ function : The __repr__ function returns a string representation of a Python object that may be evaluated by the Python interpreter to instantiate another instance of the object. Called by the repr() built-in function and by string conversions (reverse quotes) to compute the "official" string representation of an object. repr() tries to convert everything into a string that can be used with eval() to re-generate the original object. repr() generates a computer friendly output. str() is used by print() function to generate a user-friendly output. str()'s output may or may not be used with eval(). >>> eval("'Hello'") 'Hello' >>> eval('Hello') Traceback (most recent call last):   File "<pyshell#8>", line 1, in <module>     eval(' Hello ')   File "<string>", line 1, in <module> NameError: name ' Hello ' is not defined

What is Python?

Python : Python is an interpreted language. Python does not need to be compiled before it is run. Other interpreted languages include PHP and Ruby. Python is dynamically typed, this means that you don't need to state the types of variables when you declare them or anything like that. You can do things like x=123 and then x="Hello" without an error. Python is well suited to object orientated programming in that it allows the definition of classes along with composition and inheritance. In Python, functions are first-class objects. This means that they can be assigned to variables, returned from other functions and passed into functions. Classes are also first class objects. Writing Python code is quick but running it is often slower than compiled languages. Python allows the inclusion of C based extensions so bottlenecks can be optimized away and often are. The numpy package is a good example of this, it's really quite quick because a lot of the number crunching