Recently I received an email from one of my readers asking me to write about Python’s complex topics such as Iterators, Generators, and Decorators. In this post, I’m going to cover the basics, implementation, and how to use them in your code.
An iterator is an object that can be iterated upon which means that you can traverse through all the values. List, tuples, dictionaries, and sets are all iterable objects.
To create an object as an iterator you have to implement the methods
__next__() to your object where —
__iter__() returns the iterator object itself. …
Programmers life is tough. Sleepless nights, loads of coffee and thousands of lines of code. Time to take a chill pill and enjoy some programming humor peeps (Sarcasm intended :D)
Want to read some hilarious code comments?
Credits : reddit.com
While I have covered important (advanced) constructs of Python in the posts listed below, in this article I’m going to cover the basics with examples, that you should know.
Let’s dive in —
In Python, map() function applies the given function to each item of a given iterable construct (i.e lists, tuples etc) and returns a map object.
numbers = (10, 20, 30)
result = map(lambda x: x+x, numbers)total = list(result)
[20, 40, 60]
list_one = ['monday', 'tuesday']
t = list(map(list, list_one))
[['m', 'o', 'n', 'd', 'a', 'y'], ['t', 'u', 'e', 's', 'd', 'a', 'y']]
Class is a blueprint or template to create objects. …
This is the part 3 of Advanced Python Made Easy. You can refer to the first two parts here —
Let’s dive in !
Well, defaultdict belongs to collections module and is a high performance container data type which is used to overcome the issues of KeyError in python. It is a subclass of dict and returns a dict type object. It adds one writable instance variable and one method.
Syntax : defaultdict(default_factory)
where default_factory is the function which returns the default value of the dictionary.
from collections import defaultdict
default_dict_var = defaultdict(list)
for i in range(10):
This is the part 2 of the series. You can read the part 1 below :
In this part we will see some advanced python constructs that will help you write efficient code, improve code readability and performance.
Let’s dive in!
Lambda is used to create small anonymous functions using “lambda” keyword and can be used wherever function objects are needed.It can any number of arguments but only one expression.
lambda argument(s): expression
#lambda arguments : expression
#A lambda function that adds 10 to the number passed in as an argument, and print the result
x = lambda a, b, c : a * b + c
Python, created by Guido van Rossum in 1991, is an interpreted, high level-general purpose programming language. Most of us, working in software engineering field must have used Python in our work life. In this post, I’m going to cover the advanced python constructs and how you can efficiently use them to improve your code readability and performance. This is the part 1 and to be followed by Part 2 (coming soon).
You must have heard about list comprehensions (Python 2.0). Comprehensions in Python are the constructs that allows sequence built from other sequences and while the list comprehensions are quite popular/frequently used, Python 3.0 …
Before you start implementing these techniques, load the data of your choice in your working environment. For this post, I’m using Iris data.
Start with importing the necessary libraries such as Pandas and Numpy and loading your data set. Once done, dive in the techniques below —
In the code below, we are splitting the data into a random sample of rows and removing them from the original data after dropping index values.
Binning is a technique to group/bin your data into multiple buckets which is very helpful if you dealing with continuous numeric data. In pandas you can bin the data using functions cut and cut. …
For almost a decade, Stack Overflow’s annual Developer Survey has been the largest survey of people who code around the world. This year they did a survey on 60,000+ software developers and shared some of the amazing statistics.
In this article, I’m going to write a detailed analysis of their survey result. So get ready to read some amazing analysis —
This network graph shows which technologies are most highly correlated with each other. The size of each circle corresponds to the proportion of survey respondents who use the technology tool.
Ruby and Rails tend to get used together. Similarly .NET developers look forward to developing on Windows machines and Azure Cloud. …
I just finished watching the 4th season of Money Heist, a popular Spanish Netflix series and I think it’s one of the best TV series I’ve seen in recent years.
The story focuses on a group of robbers who planned a heist at the Royal Mint of Spain.
Headed by a man called “The Professor”.
His band of thieves — named after a city.
The story is very compelling and its dialogues are impressive. …