python学习笔记1

根据一个知乎高票回答的THU大神学习的
http://nbviewer.jupyter.org/github/lijin-THU/notes-python/tree/master/
只整理了一些我认为不太熟悉的东西
对于acmer或者一些熟悉C++的童鞋来说应该有些参考吧,我觉得
我觉得python是一门未来语言,总有一天取代C++,java
说白了就是更加简化
就像汇编和C的关系一样

Chat1 Tools

虽然以前也学过一点的python,但是希望这次能够更加系统的学习这门语言,为未来打好基础

dict.get(key, default=None)
# dict是字典类型,返回查找key的值
# 如果不存在,返回default
%whos
#查看当前变量空间
%reset -f
#清空变量空间
%%writefile hello_world.py
print "hello world"
# 将这行字写到hello_world.py当中去
%rmdir demo_test
# 删除文件集
%hist
# 查看历史命令
sum?
#使用??查看函数帮助和函数源代码
a = 12
a
_ + 13
#_使用上一个cell的输出结果

第一次安装好 Anaconda 以后,可以在命令行输入以下命令使 Anaconda 保持最新:

conda update conda
conda update anaconda


conda 是一种很强大的工具,具体用法可以参照它的文档

也可以参考它的 cheat sheet 来快速查看它的用法。

可以使用它来安装,更新,卸载第三方的 python 工具包:

conda install <some package>
conda update <some package>
conda remove <some package>

Chat2 part 1 a-tour-of-python.ipynb

import numpy
del numpy
# 撤销引入
s = """hello
world"""
print s
# “”“来表示换行

list的使用

a = [1, 2.0, 'hello', 5 + 1.0]
a.append("world")

set的使用

a = {1, 2, 3, 4}
b = {2, 3, 4, 5}
a & b
a | b
a ^ b
#对称差,也就是两个集合所独有的元素

dictionary的使用

d = {'dogs':5, 'cats':4}
d["pigs"]
d.keys()
d.values()

numpy.array

a = array([1, 2, 3, 4])
a + 2
# python自带也是有array数组的,这里使用

列表推导式的用法

https://blog.csdn.net/yzhou86/article/details/42964299

文件操作

f = open('data.txt', 'w')
f.write('1 2 3 4
')
f.write('2 3 4 5
')
f.close() 

函数定义

def poly(x, a, b, c):
    y = a * x ** 2 + b * x + c
    return y
x = 1
poly(array([1, 2, 3]), 1, 2, 3)
# 这里注意必须使用numpy的转化

Class 类的建立

class Person():
    def __init__(self, first, last, age):
        self.first = first
        self.last = last
        self.age = age
    def full_name(self):
        return self.first + ' ' + self.last
        
person = Person('Mertle', 'Sedgewick', 52)
person.first
person.full_name()
person.basasuya = d
person.basasuya
# 这里可以发现Person的元素是可以随机添加的
url = 'http://ichart.finance.yahoo.com/table.csv?s=GE&d=10&e=5&f=2013&g=d&a=0&b=2&c=1962&ignore=.csv'
import urllib2
ge_csv = urllib2.urlopen(url)
# web使用