Python Foundation-part One

Source: Internet
Author: User

Useful Tips

There was an error in installing the Python package under Windows, missing Setuptool,
Download the installation ez_setup.py First, then install the required packages

Development

Now, I changed to Linux, forget windows, it really sucks to does development not. NET on Windows, so I nuked up my windows A nd installed solely Ubuntu14.04, and personally, I changed the theme to Mac, Mac is excellent through delivering a user-fr iendly 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 was that, we are EAS Y to start-to-play, but difficult to dive in.
We are easy-to-code like this:

   listing = [1234]   str"12345"

Just like in all other programming languages.

Awesome Python

But, what's awesome in Python are list comprehensions, generators, map, reduce, filter, decorator, high order funcions.

Also, something important in all language are, you should get used to write recursive functions, and know about Desigi n Patterns

List Comprehensionsgeneratorsyield

To understand generators, you should know the use yield.
Then you can image the 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 would happen if we call next () once again?

>>> print friends.next()Traceback (most recent call last):  File"<stdin>"1in <module>StopIteration
Generators

We all know the Fibonacci, and know how to compute it, but now what we want are, to get the next number in the sequence, Ev Erytime we fetch only one.

Then Python's generator is good at it.

def fibonacci(n):    """Fibonacci numbers generator, generate n numbers"""    010    whileTrue:        ifreturn        yield a        a, b = b, a + b        1

Then, the just a minor change can leads it to the generate all numbers without limit:

def fibonacci():    """Fibonacci numbers generator"""    01    whileTrue:        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 is related to this. In Python, we can easily code it.

def permutations(items):    n = len(items)    if n==0yield []    else:        forin range(len(items)):            forin permutations(items[:i]+items[i+1:]):                yield [items[i]]+cc

Till now, we at least has a taste of Python ' s generator, to make it more complex, you can have generator to create genera Tor, but I'll not introduce it here.

As a python freshmen, it ' s good to know comprehensions after a quick guide of basic Python knowledges.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python Foundation-part One

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.