Advanced python topics are pretty significant depending on which side you want to dig down. However, within the python advanced tutorial, we will cover several advanced python topics. In this advanced python tutorial, I will add detailed explanations and code examples to make it easy for you to grasp the advanced python concept.
In this advanced python tutorial course, these are the topics I will discuss
- Map
- Reduce
- Filter
- List Comprehension
- Set Comprehension
- Dictionary Comprehension
- Decorator
- Generator
- Slicing in python
- Regex in python
In another writing I will add details of python object-oriented concept.
Map Functionality in Python
Now, let us start with the first concept of map functionality in python. You can consider the map is an advanced python concept. So, what is a map does in python? You can say that it is an advanced version of the traditional for loop. In another writing, I have shown how to write a basic for loop in python.
Nevertheless, you will see the advanced version of them for a loop. Map consider in python it is more efficient than python regular for loop. Map-reduce memory consumption that means if you use a traditional for loop with a list, set, or tuple for any functionality, it will get all the contents in the memory to do its functionality, wherewith map only gets one item at a given time in your system memory when it is required. Now, let us see how to write map syntax?
How to write map syntax in python?
map(function, iterable,....)
Here, the function does some action with the iterable items.
How does the map work in python?
It takes two required parameters, but you can pass more than one iterable item as many as you like. First, however, make sure the function has a parameter for each iterable item. For example, a map returns an object of a map class, but you need to convert it to either list or set and see the result.
#Example 1
In example one, I will show you at first with the traditional for loop how you can do list items multiplied by two. To do this, we need to do a couple of things. Below, I have created a list with items and an empty list of square_list_items. Then with the for loop, I have gone through each number_one_list[] items multiplied by two and added the result to the empty list. Later ion printed the result.
number_one_list = [2,3,4,5,6]
number_two_list = [7,8,9,10,11]
square_list_items = []
for number in number_one_list:
square_list_items.append(number ** 2)
print('Square list items:',square_list_items)
#Example 2
In example two, we first created a function that will square the number whatever number will pass through the function. Next, we are going to utilize the python advanced concept map. With the help map, we can do so many things; one of them is squaring the number.
def square_list_items(number):
return number * number
result = map(square_list_items, number_one_list )
print('Memory location address: ', result)
print(list(result))
In the above line, we showed map functionality. For map inside, we called the first parameter a function: the square_list_items() function and the second parameter is an iterable item which is number_one_list. Map returns an object, and we store it to the result variable. On the following line, we print that result, but it will show the memory address. That is why we have to convert the map return result to either list set or tuple.
#Example 3
In example three, we showed how you could pass more than one iterable item with the map function in python.
def add_list_items(number1, number2):return number1 + number2
add_result_items = map(add_list_items, number_one_list, number_two_list)
print('Adding two list items:', list(add_result_items))
#Example 4
In the example four we showed that you can pass any of the built in python function into map as a first argument.
string_items = ['1','2','3']
convert_int = map(int,string_items)
print('Converted int items',list(convert_int))
convert_float = map(float,string_items)
print('Converted float items',list(convert_float))
int_items = [1,2,3,4]
convert_str = map(str,int_items)
print('Int to string:', list(convert_str))
#Example 5
string_list = ['Welcome', 'to', 'my', 'channel']
print('Length of the each string:',list(map(len,string_list)))
#Example 6
In the example six we showed how you can pass lambda as a first argument within the map function without the creating specifically function. Because lambda is an anonymous function. Anonymous function is a function which has a no name of the function.
number_one_list = [2,3,4,5,6]
lambda_result = map(lambda num: num * num, number_one_list)
print('Lambda result:', list(lambda_result))
#Example 7
my_string = ['I' , 'like', 'python']
result = map(str.capitalize, my_string)
str_result = list(result)
print('Word capital:',str_result)
print('All uppercase:', list(map(str.upper, my_string)))
print('All lowercase:', list(map(str.lower, my_string)))
#print('Remove white space:', list(map(str.strip, my_string)))
The above examples I have show you a glimpse of python advanced map functionality. You can do lots of amazing thing without even writing long lines of python code.
This python advance topic reduces if you want to work with the reduce you have to import reduce from the python functools. Below I have shown how to work with the reduce. What reduce does in the coding? Let us say we have an iterable item such as a list containing numbers, and we want to get all the numbers their final value, either adding or depending on your functionality. In the below example, we have created a list with numbers, and we want to find out all the numbers adding value. Reduce can do that very efficiently. To create a reduce, it is similar to map syntax. But one difference from the map is that you do not have to convert it again either list or set to get or see the final result.
#Example 1
from functools import reduce
my_numbers = [1,2,3,4,5,6]
def add(num1, num2):
return num1 + num2
print('Final value:', reduce(add, my_numbers))
The above example of what we have done is very straightforward. At first, I have created a function, and that function task is to add numbers together. Then, we passed that function to the reduce function.
#Example 2
Here again, we showed without even creating a function, and we can use the lambda function to do our job done.
print('Using lambda :', reduce((lambda num1, num2 : num1 + num2), [1,2,3,4,5,6]))
As you can see, by just writing one line of code, you can do quite a complex functionality in python by python's advanced topic reduce. As the name is reduced, it makes sense to reduce something from large items.
Filter Functionality in Python
Same here name itself makes sense that we have to filter something from something. So in the below example, we have created a list with the numbers, and our task is to filter or find only even numbers. To do that, we used python's advanced topic filter functionality. So at first, we have created a function whose main task is to calculate a number whether a number after divide is there any leftover or not. If leftover, then that number is not an even number; it is an odd number, and if they are no leftover, then that number is an even number.
#Example 1
my_numbers = [1,2,3,4,5,6,7,8]
def find_even_number(num):
if num % 2 == 0:
return True
return False
print('Finding even number:', list(filter(find_even_number,my_numbers)))
#Example 2
print('Finding the even number using the lambda function:',list(filter(lambda num : (num % 2 == 0),my_numbers)))
List Comprehension in Python
List comprehensions in an advanced python tutorial is a unique way of creating a list. So how to create it? It consists of a bracket containing an expression followed by a for loop clause. By a list comprehensions, we can perform other python operations such as getting string length, making string uppercase, etc. You can find full details on python's original documentation. Even we can use condition statements within the list comprehensions like if, else condition. Below I have added some python advanced code examples.#Example 1 old way
fruit_names = ['apple', 'banana', 'orange'] #list with items
empty_fruit_list = [] #created empty list
for fruit in fruit_names:
empty_fruit_list.append(len(fruit))
print(empty_fruit_list)
In example one, I have shown you how to do the old way to get the length of each string. Here, each string was used different fruit names.
#Example 2 - list comprehension way
fruit_names = ['apple', 'banana', 'orange']
list_comprehension = [('Length', len(fruitName)) for fruitName in fruit_names]
print(list_comprehension)
In example two, we have shown you a list comprehension way to get the length of each fruit name.#Example 3 old way to find the even numbers
find_even_num = []
for num in range(100):
if num % 2 == 0:
find_even_num.append(num)
print('0 to 100 even num:', find_even_num)
#Example 4 list comprehension way to find the even numbers.
find_all_even_num = [('even num:', num) for num in range(100) if num % 2 == 0]
print(find_all_even_num)
#Example 5
find_all_even_num = [str(num) + '= Even num' if num % 2 == 0 else str(num) + '= Odd num' for num in range(10)]
print(find_all_even_num)
#Example 6
fruit_names = ['apple', 'banana', 'orange']
list_comprehension = [('Length', len(fruitName),fruitName.upper() ) for fruitName in fruit_names]
print(list_comprehension)
Dictionary Comprehension in Python
In an advanced python programming dictionary, Comprehensions is similar to list comprehensions. The difference between list and dictionary comprehension is that dictionary comprehension uses curly braces instead of brackets. Dictionary consists of key-value pairs like- key: value.#Example 1
student_result = {f'student_name- {result}': 0 for result in range (0, 10)}
print(student_result)
#Example 2
student_result = [(f'student_name - {result}', result * 5) for result in range(1,10)]
print(student_result)
result = {key:value for (key, value) in student_result}
print(result)
Set Comprehension in Python
In advanced python, set comprehension is one of the essential topics. Set comprehensions are a combination of list and dictionary comprehensions. First, it creates a set object. Below is an example of the set comprehension.
student_result = { result for result in [50,80,80,60,50,30]}
print(student_result)
Decorator in PythonAnother important topic in python advanced is the Decorator concept. Below we will learn how to create and use decorator in advanced python programming coding.
- In python, a decorator is a function and receives a function as an argument.
- It helps to add extra functionalities without modifying the actual function.
- In python, a function can be passed as an argument - you have seen earlier in the map, reduce function.
- We can create a function within another function.
- A function can return another function.
- Syntax of the decorator starts with @ sign.
#Example 1
In the below example, I have shown you how to pass a function as an argument to another function.
def python_basic():
print('So far we have learned python basic.')
def python_advance(function):
#function()
print('Whats up..')
function()
#calling function passing function as an argumentpython_advance(python_basic)
Here, we will create a decorator. Now at first, we have created a function called python_advanced(), an outer or decorator function called. Furthermore, we have passed a function as an argument, and that argument we can give any name. Here, I gave the name of that argument as a name function. Within theouter function, we have created another function called the inner function, andthen we gave the name of the function is python_object_oriented(). Here, we cando our different programming functionality. After that, we called again thatargument in the shape of function() which we passed at first in the outer function.Same here again, we can do our different programming functionality, and finally,we call out again inner function in the shape of the return statement.
def python_advance(function): # outer function/decorator function
def python_object_oriented():# inner function
function(). # calling outer function/decorator function
print('Now, we are learning python advance topics..')
#function() # calling outer function/decorator function
return python_object_oriented# return inner function
Now here we are calling our decorator function which we created on the above as a python_advanced() function. Whenever you call the decorator function, makesure you put @sign before that function.
@python_advance #calling the decorator functiondef python_basic():
print('So far we have learned python basic.')
python_basic()# calling the function.
In the below example we created another decorator.
def calculator(function): # outer function/decorator function
def smart_calculator(a,b):# inner function
return function(a,b). # calling outer function/decorator function
return smart_calculator. # return inner function
@calculator #calling the decorator function
def addition(a,b):
print(a * b)
#print(a + b)
addition(5, 5)# calling the function.
Generator in PythonGenerator Expression is considered an advanced python topic. In python generator expression
- It is an iterable object - like list, set, dictionary.
- The generator works on-demand on a basis, unlike a list that iterates elements all at once.
- Generator expression looks like list comprehensions, just difference generator use parenthesis where list comprehensions use brackets.
Generators are helpful for a large set of a list without sorting the entire list in the memory.
#Example 1 - at first, we will see with a list comprehension
fruit_names = ['apple', 'banana', 'orange']
list_comprehension = [('Length = ', len(fruitName)) for fruitName in fruit_names]
print(list_comprehension)
#Example 2 - now same thing we will do by generator expression
fruit_names = ['apple', 'banana', 'orange']
generator_expression = (('Length = ', len(fruitName)) for fruitName in fruit_names)
print(generator_expression)
for fruit in generator_expression:
print(fruit)
#Example 3
The examples below will show the difference between which one is faster in computing either list comprehension or generator expression. Of course, generator expression is faster than list comprehension. To do that, we imported the timeit function. Below example is with the list comprehension.
Below example is with the list comprehension.
import timeit
list_com = '[number for number in range(0,5 ** 11)]'
print(timeit.timeit(list_com, number =1))
Below example is with the generator expression.
generator_expression = '[number for number in range(0,5 ** 11)]'
print(timeit.timeit(generator_expression, number =1))
Below example we have shown you can do with the lambda function.
generator_expression = [number for number in range(0,5 ** 11)]
print(timeit.timeit(lambda:generator_expression, number =1))
You will get more discussion on the below link. https://stackoverflow.com/questions/54135771/timeit-valueerror-stmt-is-neither-a-string-nor-callable
Slicing in Python
Slicing is another crucial advanced python concept. It is not only in the advanced python topic. You can also consider it is a basic python concept. Slicing is widely used in the string formating and two or three-dimensional array in python. Below you will see how you can do slicing in python programming.
word = 'programming'print(word[0])
The above code will print only the 'p' character because we wanted to see what [0] zeroth position is in the world.
print(word[0:4])
The above line will print what is in the zeroth position and end win the fourth position but do not print the fourth position. It will only print from zero to third position 'prog' start 0 and end 4th but exclude the fourth position.
print(word[3:8])
The above line will print 'grammas 'g' character started at the third position and 'm' character end in the seventh position. The position eighth will never print. So it is vital to remember.
print(word[:4])
The above print command will print 'prog', and the expression means starting from 0 to end the fourth position and exclude the fourth position.
print(word[4:])
The above expression will print 'gramming' as it says, starting from 4th to the end.
sentence = 'python programming'
print(sentence[:])
The above expression will print the whole sentence' python programming'.
# list example slicing
numbers = [1,2,3,4,5,6,7,8,9,10]
print(numbers[0])
The above expression will only print number '1' as it sits in the 0(zeroth) position.
print(numbers[:])
The above print command will print whole list contents.
print(numbers[::2])
The above expression inside the print function means moving forward by 2. Start from the zeroth(0) position to the end position and step by 2. So the print function will print [1, 3, 5, 7, 9]
print(numbers[::-1])
The above print function will print the reversing list. Like this - [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
print(numbers[1:9:3])
The above statement said that, start index 1 and finish to end index and each step by 3. So it will print like [2, 5, 8]
Regex in Python
RegEx is widely used in string formatting. It is an advanced python topic, and at the same time, it is an advanced python programming concept.
- Regular expressions are also called REs, regexes, or regex patterns.
- It is available through the python re module.
- A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern.
- Using the pattern, you can specify the rules for the set of possible strings that you want to match;
- This set of strings might contain English sentences, e-mail addresses, etc.
Here is a complete list of the metacharacters; their meanings will be discussed.
. ^ $ * + ? { } [ ] \ | ( )Metacharacters meaning--
. Period: A period is used to match any single character - except newline \n\r\f\v
^ Caret: The caret symbol ^ is used to check if a string starts with a specific character.
^a abc 1 match
bac no match
^ab abc 1 match
bca no match
$ Dollar: The dollar symbol is used to check if a string ends with a specific character.
$a baba 1 match
baby no match
* Star: The star symbol is used to check to match zero or more occurrences of the pattern left to it.
ca*b ab 1 match
can no match as a is not followed by b
+ Plus: The plus symbol + is used to match one or more occurrences of the pattern left to is.
ca+b cb no match as there is no a character
cab 1 match
can no match as a is not followed by b
? Question Mark: The question mark symbol is used to match zero or one occurrence of the pattern left to it.
ca?b cb 1 match
cab 1 match
caaa No match as there are more than one a character
can no match as a is not followed by b
{}Braces: The braces are used to check repetitions of the pattern left to it. Example {a,b} this means at least a, and at most b repetitions of the pattern left to it.
b{2,3} abc bca no match
abba baac 1 match as there is bb pattern
abba abbba 2 match as there are two bb and bbb pattern
[] Bracket: This square bracket is used to specify a set of characters you want to match.
[abc] a 1 match
ab 2 match
how are you? 1 match
You can also specify a range of characters using - inside square brackets.
[a-e] is the same [abcde]
[1-5] is the same [12345]
You can skip characters set by using the caret ^ symbol at the start of a square bracket.
[^abcd] that means any character except a b c d
[^0-8] - - - any non-digit character.
\Backslash: Backslash is used to escape characters, including all metacharacters.
| Alternation: The alternation or vertical bar is used for or operator
a|b efg no match
ade 1 match
acbda 3 match
() Group: The parenthesis is used to group sub-patterns.
Example (x|y|z)abc match any string either x or y or z, followed by ABC
(x|y|z)abc xy abc no match
xyabc 1 match (match from yabc)
xabc xzabc 2 match xabcand zabc
Special Sequences
\A Matches if the specified characters are at the start of a string.
\b Matches if the specified characters are at the beginning or end of a word.
\B Opposite of \b. It matches if the specified characters are not at the beginning or end of a word.
\d Matches any decimal digit; this is equivalent to the class [0-9].
\D Matches any non-digit character; this is equivalent to the class [^0-9].
\s Matches any whitespace character; this is equivalent to the class [ \t\n\r\f\v].
\S Matches any non-whitespace character; this is equivalent to the class [^ \t\n\r\f\v].
\w Matches any alphanumeric character; this is equivalent to the class [a-zA-Z0-9_].
\W Matches any non-alphanumeric character; this is equivalent to the class
[^a-zA-Z0-9_].
\Z Matches if the specified characters are at the end of a string. this is
equivalent to the class
Expression - Python\Z - I am learning Python match
- I am doing Python Project - No match
NB-https://regex101.com/ - excellent site to learn regex.
#Example-1
#. Period - A period is used to match any single character - except newline \n\r\f\v
import re
text = 'Python Programming'
result = re.findall('Py..on', text)
print(result)
#Example -2
#^ Caret - The caret symbol ^ is used to check if a string starts with a particular character.
caret_result = re.findall('^Python', text)
if caret_result:
print('Yes the string start with P')
else:
print('No..start with different character')
#Example 3
#$ Dollar - The dollar symbol is used to check if a string ends with a particular character.
dollar_result = re.findall('Programming$', text)
if dollar_result:
print('Yes the string end with Programming')
else:
print('No..end with different character')
#Example 4#* Star - The star symbol is used to check to match zero or more occurrences of the pattern left to it.
text = 'Python Programming'
start_result = re.findall('Py.*m', text)
print(start_result)
#Example 5
#+ Plus - The plus symbol + is used to matches one or more occurrences of the pattern left to is.
text = 'Python Programming'
plus_result = re.findall('Py.+h', text)
#print(plus_result)
#Example 6#? Question Mark The question mark symbol is used to match zero or one occurrence of the pattern left to it.
#text = 'Python Programming'
text = 'banana'
question_result = re.findall('ba.?a', text)
print(question_result)
#Example 7#{} Braces - The braces are used to check repetitions of the pattern left to it. # example {a,b} this means at least a, and at most b repetitions of the pattern left to it.
text = 'Python Programming'
brace_result = re.findall('Pro.{7}g', text)
print(brace_result)
#Example 8#[] Bracket: This square bracket is used to specify a set of characters you want
to match.
text = 'Python Programming'
bracket_result = re.findall('[a-h]', text)
print(bracket_result)
#Example 9#\ Backslash - Backslash is used to excape characters including all meta characters.
text = 'Python Programming 2021'
blackslash_result = re.findall('\d', text)
print(blackslash_result)
#Example 10# | Alternation - The alternation or vertical bar is used for or operator
text = 'Python Programming 2021 basic and advance learning'
alternation_result = re.findall('NLP | machine', text)
alternation_result = re.findall('basic | machine', text)
if alternation_result:
print('Yes there is a match')
else:
print('No there is no match')
#\A Matches if the specified characters are at the start of a string.
text = 'Python Programming 2021'
result = re.search('\AProgramming', text)
if result:
print('Yes there is a text in the beginning')
else:
print('No there is no match in the beginning')
The re module offers a set of functions that allows us to search a string for a match: findall() Returns a list containing all matches search() Returns a Match object if there is a match anywhere in the string split() Returns a list where the string has been split at each match sub() Replaces one or many matches with a string match() Determine if the RE matches at the beginning of the string. match() and search() return None if no match can be found. If they are successful, a match object instance is returned.You can watch more on that below video.Please like, share ,and comment. Thank you.