Hello and Welcome to Python Tutorial for Beginners, where we teach you python from scratch. We have come a long way in our programming journey and this is our last class in this course. In the last class, we saw some Advance Python Concepts like Exception Handling, Enumeration, List comprehensions, etc. If you haven’t seen that, CLICK HERE.
Moving on, today we will be seeing Virtual Environment in Python, along with other small and useful concepts Lambda Function, Join Method, and many more. So, let’s get started.
Virtual Environment in Python
An Environment that is the same as the system interpreter but is isolated from the other python environments on the system.
Let’s take an example to understand better. Suppose, you are working on a Web App and you are using Flask Framework. For those, who don’t know, Flask is a framework to develop web apps in python. So you do import Flask and develop a web app with it. But when you develop the app, The Flask version is something like 0.0.1, you deploy the app and it works fine. But after some years, Now Flask version is 2.1.1. And you want to do more development in it, there are chances that you might have a lot of trouble doing that. As your code can break, which seems to be running smoothly till now. But if have developed a Virtual Environment of that version or project specifically you don’t have to worry about that. Having a Virtual Environment is like having two python installation in one computer, which is awesome for development.
Installation – To use Virtual Environment, we write:
pip install virtualenv – It will install the package for Virtual Environment.
Then, we can create a new environment using:
virtualenv myprojectenv – It will create a new Virtual Environment of name “myprojectenv”. You can choose whatever name you like.
Pip freeze command – pip freeze returns all the packages installed in a given python environment along with the versions.
“pip freeze > requirements.txt”
Now, why this is important? It’s because when you try to share your projects with other people, they might don’t have the same installation of modules on their machines. So, what will you do then, you will tell them specifically to pip install all the modules with their versions. It’s not convenient at all, Rather we use pip freeze.
The above command creates a file named requirements.txt in the same directory containing the output of the pip freeze.
We can distribute the file to other users and they can recreate the same environment using:
“pip install –r requirements.txt”
Lambda Functions in Python
Lambda Functions are the functions created using an expression using the lambda keyword. They are also known as Anonymous Function.
Syntax: lambda arguments: expressions
Let’s see an example to see understand it better.
Here we make a function name square and we pass the argument x, then we perform x * x as an expression. And then print it, and what we got the square of the number passed, which was 5 here. So, Lambda functions are kind of one-liners for functions like list comprehension for lists. But these are good at simple situations like add, square, multiply, etc. They are not very useful for Complex Functions.
Join method in Python
Join Method is used to join strings in Python. Let’s see an example:
Here we create a list named “fruits” and we add some fruit to it. then we use “add”.join(fruits) to join the elements in fruits. And when we print it, we get the fruits name with and.
Map, Filter and Reduce in Python
Map – Map applies a function to all the items in an input list. Function could be a lambda function.
Syntax: map(function, input_list)
Here we create a list of numbers and then we use the map with a lambda function that returns square like seen before with an input list. One thing, to remember here to typecast the map to list and we get our result.
Filter – Filter creates a list of items for which the function returns true. Function can be a lambda function.
Syntax: list(filter(function, input_list))
Here we have a list of elements and we specify the filter with a lambda function in which x > 10, which means the number is greater than 10. Then we specify the input list. The result is the elements greater than 10 from the list.
Reduce – Reduce applies a rolling computation to a sequential pair of elements. Function can be a lambda function in input.
Syntax: from functools import reduce
Val = reduce(function, input_list)
Here we first import reduce from functools, Then we initialize it with reduce with a lambda function that gives the sum of two numbers along with a simple list of 1,2,3,4, whose sum is 10. The technique reduce use to do this is rolling computation in which, the flow goes like this. First, the first two elements are added i.e 1 + 2 = 3, then the sum is added to next element, 3+3 = 6, then next, 6+4 = 10.
So, that’s all for today, and for our Python for beginners course, We have seen a lot and lots of concepts, and today finally we are at the end of the journey. Python is a versatile language and fun to do coding, Now it’s up to you to develop your skills more, you can do lots of projects to do so. For now, let’s revise what we learn today. We saw Virtual Environment in Python, We saw Lambda function, join method, Map, Filter, and Reduce. On that note, if you want to know more, CLICK HERE. This is Dolores Haze, signing off, I will meet you in the next one, till then, “Happy Coding” and Bye.