Documente Academic
Documente Profesional
Documente Cultură
def foo1():
with open('a.txt','r') as f:
for l in f:
pass
Output:
{'baz': 'Hello', 'foo': 123, 'bar': [1, 2, 3]}
5. What is isinstance?
Returns a Boolean stating whether the object is an instance or subclass of another
object.
Example:
>>> isinstance('foo', basestring)
True
>>> isinstance('foo', float)
False
6. Slicing
The general method format is: slice(start, stop, increment) , and it is analogous
to start:stop:increment when applied to a list or tuple.
>>> a = [1, 2, 3, 4, 5, 6, 7, 8]
>>> a[1:4:2]
[2, 4]
>>> a[::-1]
[8, 7, 6, 5, 4, 3, 2, 1]
7. List1=[1,2,3]
List2 = [4,5,6]
List1.append(List2) => output? [1,2,3,[4,5,6]]
List1.extends(List2) => output? [1,2,3,4,5,6]
.pyc contain the compiled bytecode of Python source files. The Python interpreter
loads .pyc files before .py files, so if they're present,
it can save some time by not having to re-compile the Python source code.
11. Map?
Map applies a function to all the items in an input_list
r = map(func, seq)
items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))
def multiply(x):
return (x*x)
def add(x):
return (x+x)
# Output:
# [0, 0]
# [1, 2]
# [4, 4]
# [9, 6]
# [16, 8]
12. reduce?
Reduce is a really useful function for performing some computation on a list and
returning the result.
Examples of reduce()
Determining the maximum of a list of numerical values by using reduce:
>>> f = lambda a,b: a if (a > b) else b
>>> reduce(f, [47,11,42,102,13])
102
>>>
Calculating the sum of the numbers from 1 to 100:
>>> reduce(lambda x, y: x+y, range(1,101))
5050
13. Filter
filter creates a list of elements for which a function returns true
number_list = range(-5, 5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
print(less_than_zero)
18. Args & kargs? (* and **). Where list & dict can pass?
import os
def get_immediate_subdirectories(a_dir):
print [name for name in os.listdir(a_dir)
if os.path.isdir(os.path.join(a_dir, name))]
get_immediate_subdirectories('C:\Users\inmkharade.ADHARMAN\Desktop')
https://hplgit.github.io/edu/ostasks/ostasks.pdf