Python Foundation-part one

Source: Internet
Author: User

Python Foundation-part one
Useful Tips

An error occurred while installing the python package in windows. setuptool is missing,
Download and install ez_setup.py, and then install the required package.

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 learned 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 ages.

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 shocould 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 
  
   , line 1, in 
   
    StopIteration
   
  
Generators

We all know the maid, 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, using 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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.