Saturday, July 3, 2021

Python - Virtual env, pip freeze, Lambda function, join method, format method and map, filter, reduce

 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.

nameinput("Please enter your name: ")
marksinput("Please enter your marks: ")
phoneinput("Please enter phone number: ")
template="the name of the Stdent is {} and his marks are {} and his phone number is {}."
print(template.format(name,marks,phone))


a list contains the multiplication table of 7. WAP to convert to a vertical string of the same numbers.

num = int(input("Enter a number: "))
table = [str(num * ifor i in range(1,11)]
print(table)
vertical="\n".join(table)
print(vertical)

WAP to filter a list of numbers which are divisible by 5.

myList=[1,2,4,6,7,8,9,123,54,67,90,45,5,56,50]

newList=filter(lambda a:a % 5 ==0,myList)

print(list(newList))

WAP to find the max of the numbers in a list using reduce function.

from functools import reduce
numberList=[1,2,4,6,7,8,9,123,54,67,90,45,5,56,50]

maxnumber=reduce(max,numberList)

print(maxnumber)







No comments:

Post a Comment

Featured Post

OIC - how can I use XSLT functions to remove leading zeros from numeric and alphanumeric fields?

To remove leading zeros from an numeric field in Oracle Integration Cloud (OIC) using XSLT, you can Use number() Function The number() funct...