Useful Tips
在windows下安装python包出现错误,缺少setuptool,
此时先下载安装ez_setup.py, 然后就可安装所需包
Development
Now, I changed to Linux, forget windows, it really sucks to do development not .NET on Windows, So I nuked up my windows and installed solely Ubuntu14.04, and personally, I changed the theme to Mac, Mac is excellent through delivering a user-friendly interface like windows, but also a happy development environment like Linux, though not as strong as linux.
Start
I learnt python all by myself, the confusion for learning a programming language also a software skill is that, we are easy to start to play, but difficult to dive in.
We are easy to code like this:
listing = [1, 2, 3, 4]
str = "12345"
just like in all other programming languages.
Awesome Python
But, what is awesome in Python is list comprehensions, generators, map, reduce, filter, decorator, high order funcions.
Also, something important in all language is that, you should get used to write recursive functions, and know about desigin patterns
List comprehensions
Generators
yield
To understand generators, you should know how to use yield.
then you can image how we use iterators in java or other similar functions.
def my_friends():
yield("zhang san")
yield("li si")
yield("wang wu")
Ok, then in the python IDLE;
>>> friends = my_friends()
>>> print friends.next()
zhang san
>>> print friends.next()
li si
>>> print friends.next()
wang wu
Finished, as it reached the last yield, so what will happen if we call next() once again?
>>> print friends.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
Generators
We all know the fibonacci, and know how to compute it, but now what we want is, to get the next number in the sequence, everytime we fetch only one.
Then Python’s generator is good at it.
def fibonacci(n):
"""Fibonacci numbers generator, generate n numbers"""
a, b, counter = 0, 1, 0
while True:
if (counter > n): return
yield a
a, b = b, a + b
counter += 1
Then, just a minor change can lead it to generate all numbers without limit:
def fibonacci():
"""Fibonacci numbers generator"""
a, b = 0, 1
while True:
yield a
a, b = b, a + b
In C++ STL, there is a method called permutation, this is also important algorithm (backtrack) in Leetcode, many problems are related to this. In python, we can easily code it.
def permutations(items):
n = len(items)
if n==0: yield []
else:
for i in range(len(items)):
for cc in permutations(items[:i]+items[i+1:]):
yield [items[i]]+cc
Till now, we at least have a taste of Python’s generator, to make it more complex, you can have generator to create generator, but I will not introduce it here.
As a Python freshmen, it’s good to know comprehensions after a quick guide of basic python knowledges.
版权声明:本文为博主原创文章,未经博主允许不得转载。