List Count Function in Python
The Python count() function is used to return the occurrence of the given object in the specified list. For example…
The Python count() function is used to return the occurrence of the given object in the specified list. For example…
The Python open() function is used to open the specified file where you may perform reading, writing and other operations.
The with statement is particularly used for clean-up tasks as you are working with unmanaged resources e.g. opening a file, performing some operations and closing it (file streams).
A counter is a subclass of the dict (From collections library), is used to count the hashtable objects. elements of the iterable like lists can be counted by using Counter.
While there is no formal method like “substring” or “substr” etc. to get substrings in Python, the techniques are called slicing and extended slicing.
The Python break statement is used to terminate the for or while loops. Normally, the loop ends as the testing condition fails. However, in certain scenarios, you may require ending the loop earlier
In Python, you may concatenate the strings in different ways. Using + sign (plus), += concatenate operator, join() method – For iterators, Using StringIO, The space between string literals
The KeyError is raised when you try to access a key in a dictionary that does not exist. This tutorial addresses how to deal with KeyError.
The errors occur as a program is executed or running is called exception. These errors must be handled or a program will crash and may leave a confusion or bad impression on the user’s part.
The purpose of foreach loop is to iterate through arrays or collections easily. Then why Python does not support it? Well, there is another handy way of doing this job quite easily. You can say doing foreach in Pythonic way.
The Python ord() function is used to return an integer of the given single Unicode character. Inversely, the Python chr() function takes a Unicode code point (integer) and returns a string.
For single line comments, you may use the # (hash character) in the Python programs. For multi-line comments, use the triple quoted strings. Both of these ways are explained.
The official site suggests using the if…elif…elif..else statements for the limited number of possibilities (say up to 12 or so). However, for a very large number of possibilities, you may create a dictionary.
The startswith is a string method that returns True if the specified string starts with the given prefix. In order to check the suffix from last of the string, use the endswith method.
In Python, you may use the equal to (==) and not equal to (!=) operators for testing the equality of two objects.