Some Advanced Features of Python and Its uses
Categories: Python
Here Some Advanced Features of Python and Its uses
The advanced features of any programming language are usually discovered through extensive experience. You’re coding up a complicated project and find yourself searching for something on stack over flow. You then come across a beautifully elegant solution to your problem that uses a Python feature you never even knew existed!
That’s totally the funnest way to learn: discovery by exploration and accident!
Here are 5 of the most useful advanced features of the Python programming language — and more importantly how to use them!
1. Lambda functions
A Lambda Function is a small, anonymous function — anonymous in the sense that it doesn’t actually have a name.
Python functions are typically defined using the style of def a_function_name() , but with lambda functions we don’t give it a name at all. We do this because the purpose of a lambda function is to perform some kind of simple expression or operation without the need for fully defining a function.
A lambda function can take any number of arguments, but must always have only one expression:
x = lambda a, b : a * b
print(x(5, 6)) # prints '30'
x = lambda a : a*3 + 3
print(x(3)) # prints '12'
See how easy that was! We performed a bit of basic math without the need for defining a full on function. This is one of the many features of Python that makes it a clean and simplistic programming language to use.
2. Maps
Map() is a built-in Python function used to apply a function to a sequence of elements like a list or dictionary. It’s a very clean and most importantly readable way to perform such an operation.
def square_it_func(a):
return a * a
x = map(square_it_func, [1, 4, 7])
print(x) # prints '[1, 16, 47]'
def multiplier_func(a, b):
return a * b
x = map(multiplier_func, [1, 4, 7], [2, 5, 8])
print(x) # prints '[2, 20, 56]'
Check out the example above! We can apply our function to a single list or multiple lists. In face, you can use a map with any python function you can think of, as long as it’s compatible with the sequence elements you are operating on.
3. Filtering
The Filter built-in function is quite similar to the Map function in that it applies a function to a sequence (list, tuple, dictionary). The key difference is that filter() will only return the elements which the applied function returned as True.
Check out the example below for an illustration:
# Our numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
# Function that filters out all numbers which are odd
def filter_odd_numbers(num):
if num % 2 == 0:
return True
else:
return False
filtered_numbers = filter(filter_odd_numbers, numbers)
print(filtered_numbers)
# filtered_numbers = [2, 4, 6, 8, 10, 12, 14]
Not only did we evaluate True or False for each list element, the *filter()*function also made sure to only return the elements which matched as True. Very convenient for handling to two steps of checking an expression and building a return list.