Python Foundation - part one

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.

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 21:41:09

Python Foundation - part one的相关文章

有意思的Python:开发和部署一览

我觉得在有时间的条件下,学习不同的开发语言,对于保持对技术的理解是有帮助的. Python是一门这样简单而且有趣的语言.网上资料已经比较多了.我这里主要对开发和部署环境所涉及的几个工具做些介绍. 1. 安装Python 这个相当于是一个运行环境,有些文档声称Python也有虚拟机机制(经过测试比较,它的运行速度其实和JAVA, C#编译的程序是同一个等级的,有时候甚至更慢一点),所以安装Python是第一步. 如果是Windows环境,请下载安装包(http://python.org/) 如果是

【译】使用 Python 编写虚拟机解释器

原文地址:[Making a simple VM interpreter in Python](https://csl.name/post/vm/) **更新:根据大家的评论我对代码做了轻微的改动.感谢 robin-gvx. bs4h 和 Dagur,具体代码见[这里](https://github.com/cslarsen/python-simple-vm)** Stack Machine 本身并没有任何的寄存器,它将所需要处理的值全部放入堆栈中而后进行处理.Stack Machine 虽然简

Python框架、库以及软件资源汇总

转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世界各地的程序员们都能够贡献他们的代码与创新. Python就是这样一门受到全世界各地开源社区支持的语言.Python可以用来开发各种小工具软件.web应用.科学计算.数据分析等等,Python拥有大量的流行框架,比如Django.使用Python框架时,可以根据自己的需求插入不同的模块,比如可以用S

python开发初期及二次开发C api

1,python2 or python 区别, https://wiki.python.org/moin/Python2orPython3 python software foundation 2,python 应用(最近Ruiy在搞openstack知道在openstack上有一个web框架(django)和openstack服务上的每一个组件的客户端都是由python写的) python C api https://docs.python.org/3/c-api/index.html pyt

为 Python Server Pages 和 Oracle 构建快速 Web 开发环境。

为 Python Server Pages 和 Oracle 构建快速 Web 开发环境. - 在水一方 - 博客频道 - CSDN.NET 为 Python Server Pages 和 Oracle 构建快速 Web 开发环境. 分类: 技术空间 2008-06-12 10:43 301人阅读 评论(0) 收藏 举报 pythonoracleserverwebapache数据库 目录(?)[+] Python 和 Python server Pages 的背景 解决方案组件 oracle 数

树莓派2代B model 上手初体验,不用显示器,Python GPIO 点亮一颗LED

开题:[好东西,值得研究!] 标题:树莓派2代B model 上手初体验,不用显示器,Python GPIO 点亮一颗LED [知识普及] 1,树莓派各版本对比: 2,树莓派2代BModel 主板,图样 树莓派2 代B GPIO 图 [所需硬件] 一张TF卡,8G或者8G以上,我的是 [三星TF卡16g class10 EVO] 一根网线,让树莓派与路由器连接 一个5V 500MA 的普通USB电源,为树莓派供电 ,我试过了,5V 500ma没问题 一个树莓派2代B 一个普通路由器[如果你连路由

Machine and Deep Learning with Python

Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstitions cheat sheet Introduction to Deep Learning with Python How to implement a neural network How to build and run your first deep learning network Neur

Python Tutorial 学习(六)--Modules

6. Modules 当你退出Python的shell模式然后又重新进入的时候,之前定义的变量,函数等都会没有了. 因此, 推荐的做法是将这些东西写入文件,并在适当的时候调用获取他们. 这就是为人所知的脚本文件. 随着编程的深入,代码的增多,你可能又会将代码存到不同的文件中方便管理. 你会想到去使用之前的编程中已经写好了的一个函数的定义. Python有自己的方式去实现这些.它会将这些保存了定义的函数,类等的文件(文件夹)称作module; 一个module中的定义的函数 类等可以被导入到另一个

Python windows下获取MAC地址的一种方法

我最近有一个项目,使用Python在win32下开发一个COM组建,该COM组建其中一个方法是获取本地电脑的MAC地址. 需求很简单,虽然我是Python新手中的新手,但我还是会使用搜索引擎进行搜索. 百度一下,发现大部分都介绍使用import UUID获取MAC地址,或使用os.popen("ipconfig /all")的方式获取.而后者容易受到操作系统中英文环境影响. 如这篇文章:http://www.cnblogs.com/Jerryshome/archive/2011/11/