Virtual Env:
An environment which is same as the system interpreter but is isolated from the other python environments on the system.
Installation:
to use virtual environment:
pip install virtualenv
to create:
virtualenv myprojectenv
to activate:
activate
pip freeze:
It returns all the packages installed in a given env along with the versions.
pip freeze > list.txt
we can share the this file to other user and they can recreate the same env using:
pip install -r list.txt
Lambda function:
syntax:
lambda arguments: expressions
instance:
square = lambda a : a * a
square(2)
Join method:
It creates a string from iterable objects.
l=["apple","mango","banana"]
" and ".join(l)
output: "apple and mango and banana"
format method:
formats the values inside a string into a desired output.
template.format(arguments)
example:
"{} is a good {}".format("john","boy")
or
"{1} is a good {0}".format("boy","john")
output: john is a good boy
Map, filter and reduce:
Map:
map applies a function to all the items in a input list.
map(function,list)
Filter:
It creates a list of items for which the function returns true.
list(filter(function,list))
Reduce:
It applies a rolling computation to sequential pair of elements.
from functools import reduce
val=reduce(function, list)
if the function computes the sum of two numbers and the list is [1,2,3,4], the output will be 10.
Practice:
Create two virtual environments , install few packages in the first one. create a similar env in the 2nd one.
pip install virtualenv
virtualenv env1
virtualenv env2
.\env1\Scripts\activate.ps1
pip install pandas
pip freeze > 2.txt
deactivate
.\env2\Scripts\activate.ps1
pip install -r .\2.txt
deactivate
WAP to input name, marks and phone numbers of a student and format it using the format function like "The name of the student is Sri and his marks are 65 and phone number is 0000000000.
a list contains the multiplication table of 7. WAP to convert to a vertical string of the same numbers.
WAP to filter a list of numbers which are divisible by 5.
WAP to find the max of the numbers in a list using reduce function.
No comments:
Post a Comment