Python Foundation - part one
来源:互联网 发布:程序员找女朋友 知乎 编辑:程序博客网 时间:2024/05/01 20:46
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.
- Python Foundation - part one
- Python Foundation - part two
- Python Foundation - part three
- Windows Communication Foundation入门(Part One)
- Windows Communication Foundation入门(Part One)
- Windows Communication Foundation入门(Part One)
- Windows Communication Foundation入门(Part One)
- Python en:First Steps--part one
- Python socket 模块——part one
- The One Foundation
- 封装变化(Part One)
- Part One Basic Progaraming
- 虚拟语气(part one)
- Exception Handling - Part One
- java_12-24-part one
- MFC message route (Part ONE)
- Part One 软件的安装
- Java coding style - Part One
- python修饰器
- MySQL 分页查询性能分析
- JavaScript中的this代表什么?
- 去掉UUID的小横线,如果你觉得不爽的话,哈哈
- JNI Helloworld
- Python Foundation - part one
- python 如何压平嵌套
- Linux(deepin)安装Oracle JDK
- 第86讲:Scala中For表达式的生成器、定义和过滤器学习笔记
- 关于位域的知识
- python按照表修改excel中的内容
- NYOJ 24 素数距离问题
- 我的stm32第一个程序按键防抖
- 亿唐