5 Advanced Features of Python and How to Use Them

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 stackoverflow. 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
	        return False

	filtered_numbers = filter(filter_odd_numbers, 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.

(4) Itertools

The Python Itertools module is a collection of tools for handling iterators. An iterator is a data type that can be used in a for loop including lists, tuples, and dictionaries.

Using the functions in the Itertools module will allow you to perform many iterator operations that would normally require multi-line functions and complicated list comprehension. Check out the examples below for an awesome illustration of the magic of Itertools!

from itertools import *

	# Easy joining of two lists into a list of tuples
	for i in izip([1, 2, 3], ['a', 'b', 'c']):
	    print i
	# ('a', 1)
	# ('b', 2)
	# ('c', 3)

	# The count() function returns an interator that 
	# produces consecutive integers, forever. This 
	# one is great for adding indices next to your list 
	# elements for readability and convenience
	for i in izip(count(1), ['Bob', 'Emily', 'Joe']):
	    print i
	# (1, 'Bob')
	# (2, 'Emily')
	# (3, 'Joe')    

	# The dropwhile() function returns an iterator that returns 
	# all the elements of the input which come after a certain 
	# condition becomes false for the first time. 
	def check_for_drop(x):
	    print 'Checking: ', x
	    return (x > 5)

	for i in dropwhile(should_drop, [2, 4, 6, 8, 10, 12]):
	    print 'Result: ', i

	# Checking: 2
	# Checking: 4
	# Result: 6
	# Result: 8
	# Result: 10
	# Result: 12


	# The groupby() function is great for retrieving bunches
	# of iterator elements which are the same or have similar 
	# properties

	a = sorted([1, 2, 1, 3, 2, 1, 2, 3, 4, 5])
	for key, value in groupby(a):
	    print(key, value), end=' ')
	# (1, [1, 1, 1])
	# (2, [2, 2, 2]) 
	# (3, [3, 3]) 
	# (4, [4]) 
	# (5, [5]) 

(5) Generators

Generator functions allow you to declare a function that behaves like an iterator, i.e. it can be used in a for loop. This greatly simplifies your code and is much more memory efficient than a simple for loop.

Consider an example where we want to add up all of the numbers from 1 to 1000. The first part of the code below illustrates how you would do this using a for loop.

Now that’s all fine and dandy if the list is small, say a length of 1000. The problem arises when you want to do this with a huge list, say 1 billion float numbers. With a for loop, that massive memory chewing list is created *in memory — *not everyone has unlimited RAM to store such a thing! The range() function in Python does the same thing, it builds the list in memory

Section (2) of the code illustrates the summing of the list of numbers using a Python generator. A generator will create elements and store them in memory only as it needs them i.e one at a time. That means, if you have to create 1 billion floating point numbers, you’ll only be storing them in memory one at a time! The xrange() function in Python uses generators to build lists.

Moral of the story: If you have a large range that you’d like to generate a list for, use a generator or the xrange function. This is especially true if you have a really memory sensitive system such as mobile or at-the-edge computing.

That being said, if you’d like to iterate over the list multiple times and it’s small enough to fit into memory, it will be better to use for loops and the range function. This is because generators and xrange will be freshly generating the list values every time you access them, whereas range is a static list and the integers already exist in memory for quick access.

# (1) Using a for loop
	numbers = list()

	for i in range(1000):
	total = sum(numbers)

	# (2) Using a generator
	 def generate_numbers(n):
	     num, numbers = 1, []
	     while num < n:
	     num += 1
	     return numbers
	 total = sum(generate_numbers(1000))
	 # (3) range() vs xrange()
	 total = sum(range(1000 + 1))
	 total = sum(xrange(1000 + 1))

#python #machine-learning

What is GEEK

Buddha Community

5 Advanced Features of Python and How to Use Them
Ray  Patel

Ray Patel


top 30 Python Tips and Tricks for Beginners

Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.

1) swap two numbers.

2) Reversing a string in Python.

3) Create a single string from all the elements in list.

4) Chaining Of Comparison Operators.

5) Print The File Path Of Imported Modules.

6) Return Multiple Values From Functions.

7) Find The Most Frequent Value In A List.

8) Check The Memory Usage Of An Object.

#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners

Ray  Patel

Ray Patel


Lambda, Map, Filter functions in python

Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.

Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is

Syntax: x = lambda arguments : expression

Now i will show you some python lambda function examples:

#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map

How To Compare Tesla and Ford Company By Using Magic Methods in Python

Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc…

You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like init, call, str etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).

Now there are a number of such special methods, which you might have come across too, in Python. We will just be taking an example of a few of them to understand how they work and how we can use them.

1. init

class AnyClass:
    def __init__():
        print("Init called on its own")
obj = AnyClass()

The first example is _init, _and as the name suggests, it is used for initializing objects. Init method is called on its own, ie. whenever an object is created for the class, the init method is called on its own.

The output of the above code will be given below. Note how we did not call the init method and it got invoked as we created an object for class AnyClass.

Init called on its own

2. add

Let’s move to some other example, add gives us the ability to access the built in syntax feature of the character +. Let’s see how,

class AnyClass:
    def __init__(self, var):
        self.some_var = var
    def __add__(self, other_obj):
        print("Calling the add method")
        return self.some_var + other_obj.some_var
obj1 = AnyClass(5)
obj2 = AnyClass(6)
obj1 + obj2

#python3 #python #python-programming #python-web-development #python-tutorials #python-top-story #python-tips #learn-python

Biju Augustian

Biju Augustian


Learn Python Tutorial from Basic to Advance

Become a Python Programmer and learn one of employer’s most requested skills of 21st century!

This is the most comprehensive, yet straight-forward, course for the Python programming language on Simpliv! Whether you have never programmed before, already know basic syntax, or want to learn about the advanced features of Python, this course is for you! In this course we will teach you Python 3. (Note, we also provide older Python 2 notes in case you need them)

With over 40 lectures and more than 3 hours of video this comprehensive course leaves no stone unturned! This course includes tests, and homework assignments as well as 3 major projects to create a Python project portfolio!

This course will teach you Python in a practical manner, with every lecture comes a full coding screencast and a corresponding code notebook! Learn in whatever manner is best for you!

We will start by helping you get Python installed on your computer, regardless of your operating system, whether its Linux, MacOS, or Windows, we’ve got you covered!

We cover a wide variety of topics, including:

Command Line Basics
Installing Python
Running Python Code
Number Data Types
Print Formatting
Built-in Functions
Debugging and Error Handling
External Modules
Object Oriented Programming
File I/O
Web scrapping
Database Connection
Email sending
and much more!
Project that we will complete:

Guess the number
Guess the word using speech recognition
Love Calculator
google search in python
Image download from a link
Click and save image using openCV
Ludo game dice simulator
open wikipedia on command prompt
Password generator
QR code reader and generator
You will get lifetime access to over 40 lectures.

So what are you waiting for? Learn Python in a way that will advance your career and increase your knowledge, all in a fun and practical way!

Basic knowledge
Basic programming concept in any language will help but not require to attend this tutorial
What will you learn
Learn to use Python professionally, learning both Python 2 and Python 3!
Create games with Python, like Tic Tac Toe and Blackjack!
Learn advanced Python features, like the collections module and how to work with timestamps!
Learn to use Object Oriented Programming with classes!
Understand complex topics, like decorators.
Understand how to use both the pycharm and create .py files
Get an understanding of how to create GUIs in the pycharm!
Build a complete understanding of Python from the ground up!

#Learn Python #Learn Python from Basic #Python from Basic to Advance #Python from Basic to Advance with Projects #Learn Python from Basic to Advance with Projects in a day

Art  Lind

Art Lind


Python Tricks Every Developer Should Know

Python is awesome, it’s one of the easiest languages with simple and intuitive syntax but wait, have you ever thought that there might ways to write your python code simpler?

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

Let’s get started

Swapping value in Python

Instead of creating a temporary variable to hold the value of the one while swapping, you can do this instead

>>> FirstName = "kalebu"
>>> LastName = "Jordan"
>>> FirstName, LastName = LastName, FirstName 
>>> print(FirstName, LastName)
('Jordan', 'kalebu')

#python #python-programming #python3 #python-tutorials #learn-python #python-tips #python-skills #python-development