Best way to read file:
with open(file_name) as fp:
fp.read()
write lines to file
-----
with open(file_name, 'w') as fp:
fp.writelines(text)
-----
Get prime numbers:
nums = range(2, 50)
for i in range(2, 8):
nums = filter(lambda x: x == i or x % i, nums)
print nums
-----
Remove duplicates from a list: [use one line command 'set': this removes duplicates: it may sort also but no guarantee of ordering: to sort use 'sorted']
a = [8,8,9,10]
b = set(a)
print b
=> [8,9,10]
---
No comments:
Post a Comment