Advanced Python - 2


virtual Environment :  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, we write

pip install virtualenv     [install the package]

we create a new environment using :

virtualenv myenv              [create new virtual env]

    the next step after creating the virtual environment is to activate it. we can now use this virutal environment as a seperate python installation

PIP freeze command: PIP freeze returns all the packages installed in a given python environment along with the versions 

"PIP freeze > requirement.txt"

    the above command creates a file named requirements.txt in the same directory containing the output of PIP freeze
    We can distribute this file to other users and they can recreate the same environment using :

PIP install -r requirements.txt

Lambda Functions : functions created using an expression using lambda keyword.

syntax : lambda arguments : expressions  

expressions can be used as a normal  functions.

square = lambda x : x*x
square(6)   [returns 36]

sum = lambda a,b,c : a+b+c
sum(1,2,3)   [returns 6]

bin method (strings) : creates a string from iterable objects

l = ["apple", "mango" ,"banana"]
",and,".join(l)

    the above line will return "apple, and, mango, and, banana"

format method (strings) :  formats the values inside the string into a desired output : template.format(p1,p2, ......)   [ arguements]
syntax:
"{ } is a good { }".format("iam", "boy")

output : iam is agood boy

"{1} is a good {0}.format("iam", "boy")

output: boy is a good iam

Map, Filter and Reduce: Map applies a function to all the items in an input_list

syntax: map(function, input_list)

filter creates a list of items for which the function returns true

syntax: list(filter(function))

Reduce applies a rolling computation to sequential pair of elements
from functools import reduce
val = reduce (functin, ist1)

If the function computes sum of two numbers and the list is 
[1, 2, 3, 4]
1 2  3 4          [Special Computation]
  3   3 4
     6   4
       10

**function can be a lambda function

Programs :

1. #virtualenv

# For MacOS/linux users: source myprojectenv/bin/activate
# For windows powershell users: .\myprojectenv\Scripts\activate.ps1
# https://stackoverflow.com/questions/18713086/virtualenv-wont-activate-on-windows

import flask # flask - 0.5.2
import pandas as pd
import pygame

2. #lambda

# def func(a):
#     return a+5

func = lambda a: a+5
square = lambda x: x*x
sum = lambda a, b, c: a+b+c

x = 3
print(func(x)) # Prints 8
print(square(x)) # Prints 9
print(sum(x, 1, 2)) # Prints 6

3. #join

l = ["Camera", "Laptop", "Phone", "ipad", "Hard Disk", "Nvidia Graphic 3080 card"]

# sentence = "~~".join(l)
# sentence = "==".join(l)
sentence = "\n".join(l)
print(sentence)
print(type(sentence))

4. #format

name = "cstechiie"
channel = "blog"
type = "Coding"
# a = f"This is {name}"
# a = "This is {}".format(name)
# a = "This is {} and his channel is {}".format(name, channel)
# a = "This is {0} and his {2} channel is {1}".format(name, channel, type)
a = "This is {} and his {} channel is {}".format(name, channel, type)

print(a)

5. #map

def square(num):
    return num*num

l = [1, 2, 4]

# Method 1
l2 = []
for item in l:
    l2.append(square(item))
print(l2)

# Method 2
print(list(map(square, l)))

6. #filter

# Filter Syntax: list(filter(function, list))

def greater_than_5(num):
    if num > 5:
        return True
    else:
        return False

g10 = lambda num: num>10

l = [1, 2, 3, 4, 5, 6, 7, 8, 89, 98]
print(list(filter(greater_than_5, l)))
print(list(filter(g10, l)))

7. #reduce

from functools import reduce

sum = lambda a, b: a+b

l = [1, 2, 3, 4]
val = reduce(sum, l)
print(val)