Posts

Showing posts with the label Functions

Python Tutorial 34 --- "Decorators in Python"

 Python Tutorial 34 --- "Decorators in Python" What is Python Decorator? Decorator  as can be noticed by the name is like a designer that helps to modify a function. The decorator can be said as a modification to the external layer of function, as it does not make any change in its structure. What a decorator does is, it takes a function and insert some new functionality in it without changing the function itself. A reference to a function is passed to a decorator and the decorator returns a modified function. The modified functions usually contain calls to the original function. This is also known as  metaprogramming  because a part of the program tries to modify and add functionality into another part of the program at compile time. Understanding the definition could be difficult but we can grasp the concept easily through the example in the video section. In terms of python, the other function is also called a wrapper. A  wrapper  is a function that prov...

Python tutorial 32 --- "Join Function in Python"

Python tutorial 32 --- "Join Function in Python" We are all familiar with the word join and it's meaning i.e., to combine. Join has nearly the same meaning in Python, as it is used to join the elements of a list, tuple, set, etc. In this article, we will learn about the join() method and how to use it. In Python, join() is a function that is used with iterables like list, dictionaries, and string. Examples are also mentioned below so you may learn how to use join() function. If you do not know what iterables are, then check our tutorials on  Python Lists And List Functions ,  Dictionary & It's Functions and   String Slicing And Other Functions  to get the basic idea about iterables. What is the join method in Python? "Join is a function in Python, that returns a string by joining the elements of an iterable, using a string or character of our choice." In the case of join function, the iterable can be a list, dictionary, set, tuple, or even a string itsel...

Python Tutorial 30 --- "How Import Works In Python?"

Python Tutorial 30 --- "How Import Works In Python?" As always I am again going to repeat: not to name our file similar to a module name. The reason for that, I finally will be sharing with you in this particular tutorial.We are going to name our files file1.py and file2.py. In this tutorial we aim to understand the working of the import statement, so we can have a better grasp of the concept and resolve common importing issues. In Python, we give access to a module by using a keyword  import . To use any package in our code, we must make it accessible by importing it first. There are many ways we can import a module in python, but what can be easier than using a single keyword, so it is also the most common way for importing. How does the import keyword work? When we write a certain module name along with the  import  keyword, it will start searching for a file with that name having an extension  .py . After finding the file, it will import it into our program, whic...

Python Tutorial 29 --- "Enumerate Function"

Python Tutorial 29 --- "Enumerate Function"   “ An enumerate is a built-in function that provides an index to data structure elements, making iterations through them easier and simpler.”  In previous tutorials we have seen many methods that print items of different data structures onto the screen. For these kinds of operations, we need to have a for loop along with an iterator and an integer variable in which we have to increment every time the loop runs. Well, python provides us with a simple and easy solution to deal with certain situations by providing us with a built-in function known as “ enumerate function .” Using the enumerate function, we can summarize the code and make it easier and simpler to use. It is really important to know the concept of Enumerate function so that you can apply this concept to your code.   Let us understand the working of enumerate function: What enumerate function does is, it assigns an index to every element or value in th...

Python Tutorial 26 --- "*args and **kwargs In Python"

Python Tutorial 26 --- "*args and **kwargs In Python" DOC: We have worked with functions lately and also have made our own functions. We have seen that a function can only pass a certain number of arguments. The number of arguments has to be decided while defining the function, and it can not be changed while calling it. In simple terms, the number of arguments passed should be the same as the ones that are defined. If we dislike this restriction and do not want ourselves to be bound by certain limits, then we are lucky to have  *args  and  **kwargs  with us. Before discussing *args and **kwargs, we should have a basic knowledge about types of arguments. In Python programming, there are two types of arguments that can be passed in a function. positional arguments keyword arguments Positional arguments  are the one in which an order has to be followed while passing arguments. We have been dealing with them until now. We know how normal functions work with positio...

Python Tutorial 25 --- "F-Strings & String Formatting In Python"

Python Tutorial 25 ---  "F-Strings & String Formatting In Python" Doc: String formatting is used to design the string using formatting techniques provided by the particular programming language. From the % formatting to the format() method, to format string literals, there is no limit as to the potential of string crafting. There are four significant ways to do string formatting in Python. In this tutorial, we will learn about the four main approaches to string formatting in Python. #1 String Formatting (% Operator) Python has a built-in operation that we can access with the % operator. This will help us do simple positional formatting. If anyone knows a little bit about C programming, then they have worked with printf statement, which is used to print the output. This statement uses the % operator. Similarly, in Python, we can perform string formatting using the % operator. For Example: name = "Jack” n = " % s My name is % s” % name print ( n ) Output : ...