Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts
Thursday, August 10, 2017
Python code interpretation and execution stages
Wednesday, May 17, 2017
PIP installation process & usefulness to Python
PIP is software installer. Follow the below mentioned procedure:
1. setup proxy
copy the proxy from internet explorer
set http_proxy=http://your_proxy:your_port
or
set http_proxy=http://username:password@your_proxy:your_port
set https_proxy=https://your_proxy:your_port
or
set https_proxy=https://username:password@your_proxy:your_port
once thi is done, you are done with setup to install softwares from internet via PIP
2. Install PIP..
$python get-pip.py
3. After this start installing using PIP
eg.
$pip install numpy
$pip install scipy
you can also install from the wheel file downloaded
eg.
$pip install setuptools-23.0.0-py2.py3-none-any.whl
----------------------------------------------------------------------------------------------
Why PIP is useful?
--> Because it downloads all the required dependancy for installing any particular piece of library
Thursday, December 17, 2015
"yield" function sample code
Following is a sample code where use of yield function is shown. create_generator() function is defined which basically iterates over a range of numbers 1 to 4. Here the value a and b is summed up and incremented in every iteration.
In the following steps, when this same function create_generator() is used to provide range of values for loop (for i in create_generator() ) every time the program flow stops at yield statement and provides value to i. This value is printed. Loop continues until the loop inside create_generator() completes.
It can also be seen in the following code where each step is printed using next() functionality. The last print statement is commented. Because, at that point there is no further value produced by generator function. It throws error.
Both the outputs (with and without comment part are provided). you can try it out too ....
In the following steps, when this same function create_generator() is used to provide range of values for loop (for i in create_generator() ) every time the program flow stops at yield statement and provides value to i. This value is printed. Loop continues until the loop inside create_generator() completes.
It can also be seen in the following code where each step is printed using next() functionality. The last print statement is commented. Because, at that point there is no further value produced by generator function. It throws error.
Both the outputs (with and without comment part are provided). you can try it out too ....
#__author__ = 'Sumant'
# use of yield functionality of python def create_generator(): a = 1
b = 10
for count in range(1, 5):
yield a + b a += 1
b += 1 for value in create_generator(): print value print "going step by step ..."
xx = create_generator() print xx.next() print xx.next() print xx.next() print xx.next()
#print xx.next()
-------------------------------------------------
OUTPUT (with last line commented)
-------------------------------------------------11 13 15 17going step by step ... 11 13 15 17 Process finished with exit code 0-------------------------------------------------OUTPUT (uncomment last line)-------------------------------------------------11 13 15 17 going step by step ... 11 13 15 17 Traceback (most recent call last): File ".../demo_yield.py", line 22, in <module> print xx.next() StopIteration Process finished with exit code 1
How to use "lambda" in python ??
Few days ago, I came across a very interesting function used in one of the python test scripts. Its called "lambda".
lambda is like a callback function. Its a alternate to def in situations where
But by using lambda, you can make use of other functions like "filter, map and reduce". I have provided this sample python script where lambda function is defined and shown how it can be used along with filter, map and reduce ...
lambda is like a callback function. Its a alternate to def in situations where
- function is just one liner & only used once
- when a calling function needs a function as an input parameter
But by using lambda, you can make use of other functions like "filter, map and reduce". I have provided this sample python script where lambda function is defined and shown how it can be used along with filter, map and reduce ...
#__author__ = 'Sumant'
# use of lambda function along with "filter, map & reduce" xx = lambda a, b: a+b print xx(2, 3) lst = range(1, 6)
print "lst = " + str(lst)
print "filter functionality:"
print filter(lambda a: a+1 < 5, lst)
print "lst = " + str(lst)
print "maps functionality:"
print map(lambda a, b: a*b, lst, lst)
print "lst = " + str(lst)
print "reduce functionality:"
print reduce(lambda a, b: a * b, lst)
--------------------------------
OUTPUT
--------------------------------
5
lst = [1, 2, 3, 4, 5]
filter functionality:
[1, 2, 3]
lst = [1, 2, 3, 4, 5]
maps functionality:
[1, 4, 9, 16, 25]
lst = [1, 2, 3, 4, 5]
reduce functionality:
120
Process finished with exit code 0
Hope this link will be helpful ... :)
There are few very wonderful links where it is explained when & where to use "lambda" function:
https://pythonconquerstheuniverse.wordpress.com/2011/08/29/lambda_tutorial/
Subscribe to:
Posts (Atom)
