Join our telegram channel and group :)
Telegram Group: https://t.me/randomskoolQnATelegram Channel: https://t.me/randomskool
In this tutorial, you'll learn three interesting topics .i.e. decorators, enumerate function, join, and bonus topic indexing/slicing.
Summary of topics:
1. Decorators: Modified the behavior of a class or function.
2. Enumerate Functions: It will help keep a count of iterations.
3. Join: It will join a string of the element using string separator.
4. Indexing/Slicing: From this concept, we will able to access an individual element from the list.
Let's discuss each topic in detail with examples.
Decorator in Python
The decorator adds additional behavior to a class or function without altering the data.
Features of decorator in python:
1. Decorator act as a wrapper means that when we pack an actual gift using a wrapper, the gift is present inside the wrapper all we are providing additional decoration of a gift. Similarly, when we use decorators in python programming we are providing additional functionality to our original function without any alteration.
Let's discuss the example below:
In the above example, we are declaring one function named like an option() in which we are printing only a welcome message ( Welcome to Random Skool ). Also, we are declaring decorators using @ symbol named as help above the option function. In decorator, we are passing the function name as a parameter and under the decorator declaring another function named as inner in which we are printing two statements before and after the option function.
Working: At the time of function calling first the decorator will call and proceed with the set of statements once complete it will return to calling function after that actual function will perform the set of statements.
2. Decorator help in exception handlings such as divide by zero scenarios, login password checking scenario, and many more.
3. Decorator Function works with parameter passing - we can pass the parameter as well in a decorator to perform logical reasoning and computational tasks.
Let's discuss the above points with the help of examples:
When login is successfully
In the above example, we are passing the parameter in the decorator to cross-check whether the login credentials are correct or not. This way of error-handling ensures that our original functions remain unaffected if someone enters incorrect credentials. Best use cases - to avoid traceback error ( any kind of traceback error such as divide by zero, overflow error, and many more).
When login is denied
4. Decorators are used in chaining implies that we can use multiple decorators in one function or can use one decorator multiple times. The thing we need to keep in mind order is important.
Example :
def FirstDecorator(func):
def inner():
print("I am first")
func()
print("I am first")
return inner
def SecondDecorator(func):
def inner():
print("I am Second")
func()
print("I am Second")
return inner
@FirstDecorator
@SecondDecorator
def ChainingExp():
print(" Order is important ")
ChainingExp()
Output:
I am first
Order is important
I am first
I am second
Order is important
I am second
Let's change the decorator order of calling.
@SecondDecorator
@FirstDecorator
def ChainingExp():
print(" Order is important ")
ChainingExp()
Output:
I am second
Order is important
I am second
I am first
Order is important
I am first
Now we understand with the help of this example that the order of calling in decorator plays a vital role in python programming. In the above example, we are calling two decorators first, and second, after that when we changed the order and checked the result we will get to know that the order is reversed.
That's all about decorators.
Now let's move on to Enumerate Function.
Enumerate Function in Python
Basically enumerate is used to count the number of iteration.
Using enumerate in an iterable object such as list or tuple it will create enumerate object having index and value.
Let us understand the enumerate in detail using an example.
In the below example, we are using 4 different cases of enumerating.
First, we are declaring a simple list named lst having three values.
Case 1. Converting the list into enumerate and checking the type using type keyword.
Case 2. Loop a regular list and demonstrating that regular list having no index only value is displayed.
Case 3. Using enumerate keyword we are converting a regular list into enumerate type and loop through a enumerate and printing the index as well as value.
Case 4. Similar to Case 3 now we are passing the starting index as well after the list name.
Join in Python
Syntax:
str.join(list)
str: string separator .i.e. "-", "%","&" and many more.
Let's discuss this with the help of a simple example.
In this example, we are declaring a string list and with the help of join function, we are joining the list of elements in three different forms.
The first form where the string separator is "" (no space).
The second form where the string separator is "-" (hyphen).
The third form where the string separator is "%" (percentage symbol).
Note: Join can be used only in the string type list. Otherwise, it will raise an error (mentioned below).
Traceback (most recent call last):
File "main.py", line 3, in <module>
print("".join(lst))
TypeError: sequence item 0: expected str instance, int found
Indexing\Slicing in Python
Indexing in python is used for accessing the individual element from a list.
We can access the elements from left to right (+tive index) and right to left (-tive index) with the indexing.
Slicing is used in python for accessing the range of elements from the list.
Let's discuss slicing with the help of an example.
In the example below, all the scenarios are explained with the help of print statements.
Note: For accessing all the elements use ":"
In Slicing, there is one thing we all need to know about is the step - slicing.
What is a step - slicing?
In Slicing, we only specify the range of elements but in case we need every alternate element or every 3rd element within the range where we can use step - slicing.Syntax:
lst = [1,2,3,4,5,6,7,8,9,10]
Regular Slicing
lst[2:5]
Step-Slicing
lst[2:5:2]
lst[2:8:3]
lst[::-3]
lst[3:5:-1]
In the next tutorial, we will study a few small topics such as built and external modules, f-string, dunder variable, requirement.txt, and virtual env.
Click here for next tutorial.
For Complete Python Programming 101 tutorial series ( Click here ).
For Queries and Contact there are multiple ways:
1. From telegram search @randomskoolQnA and @randomskool
2. From Contact Us Page
3. From Comment
For Daily Updates Please subscribe to the feed.
For Job-related Updates Please fill the subscriber feedback form.
For Queries and Contact there are multiple ways:
1. From telegram search @randomskoolQnA and @randomskool
2. From Contact Us Page
3. From Comment
For Daily Updates Please subscribe to the feed.
For Job-related Updates Please fill the subscriber feedback form.
Join our telegram channel and group :)
Telegram Group: https://t.me/randomskoolQnATelegram Channel: https://t.me/randomskool
Really enjoyed this article.Much thanks again. Want more
ReplyDeletedata science training
python training
angular js training
selenium trainings
Hi, I read your whole blog. This is very nice. Good to know about the career in. Python Training & CertificationPython Training for making their career in this field .
ReplyDeleteThanks for sharing this informative blog on, best online digital marketing course with certificate, Learn python programming online, Best Python online courses with certification, Online Python course, Python Certification Course, best python online course in india, python certification online course , Python online course & certifications , Learn python programming online , No.1 python online course in India , Top python online course in India , best online python course for beginners , best online python course with certificate
ReplyDelete