Category: python basic

python tutorials and learn python

Created with Sketch.

Python Class

Python Class Python is a completely object-oriented language. This approach towards programming seeks to treat data and functions as part of a single unit called object. The class defines attributes and the behaviour of the object, while the object, on the other hand, represents the class. We have been (unknowingly) working with classes and objects…
Read more

Python – Assert Statement

Python – Assert Statement Python provides the assert statement to check if a given logical expression is true or false. Program execution proceeds only if the expression is true and raises the AssertionError when it is false. The following code shows the usage of the assert statement. Example: assert num=int(input(‘Enter a number: ‘)) assert num>=0…
Read more

Exception Handling in Python

Exception Handling in Python The cause of an exception is often external to the program itself. For example, an incorrect input, a malfunctioning IO device etc. Because the program abruptly terminates on encountering an exception, it may cause damage to system resources, such as files. Hence, the exceptions should be properly handled so that an…
Read more

Python – Error Types

Python – Error Types The most common reason of an error in a Python program is when a certain statement is not in accordance with the prescribed usage. Such an error is called a syntax error. The Python interpreter immediately reports it, usually along with the reason. >>> print “hello” SyntaxError: Missing parentheses in call…
Read more

Python-Recursion in Python

Recursion in Python A function that calls itself is a recursive function. This method is used when a certain problem is defined in terms of itself. Although this involves iteration, using an iterative approach to solve such a problem can be tedious. The recursive approach provides a very concise solution to a seemingly complex problem.…
Read more

Python – List Comprehension

Python – List Comprehension List comprehension in Python is an easy and compact syntax for creating a list from a string or another list. It is a very concise way to create a new list by performing an operation on each item in the existing list. List comprehension is considerably faster than processing a list…
Read more

Python – Reduce Function

Python – Reduce Function The reduce() function is defined in the functools module. Like the map and filter functions, the reduce() function receives two arguments, a function and an iterable. However, it doesn’t return another iterable, instead it returns a single value. The argument function is applied cumulatively to arguments in the list from left…
Read more

Python – Filter Function

Python – Filter Function The filter() function calls the specified function which returns boolen for each item of the specified iterable (list). filter() Signature: filter(function, iterable) –> filter object   The filter() function also receives two arguments, a function and a sequence (e.g. a list). Each item in the list is processed by the function…
Read more

Python – Map Function

Python – Map Function The map() function is a built-in function. map() Signature: map(function, iterable [, iterable2, iterable3,…iterableN]) –> map object   The map() function calls the specified function for each item of an iterable (such as string, list, tuple or dictionary) and returns a list of results. Consider the following simple square function. def…
Read more

Python – Generator

Python – Generator Python provides a generator to create your own iterator function. A generator is a special type of function which does not return a single value, instead it returns an iterator object with a sequence of values. In a generator function, a yield statement is used rather than a return statement. The following…
Read more