Python Foundation - part one

來源:互聯網
上載者:User

標籤:python

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 comprehensionsGeneratorsyield

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.

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Python Foundation - part one

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.