深度学习之旅(一) python入门

178 阅读4分钟

虽然python有python2和python3两个版本,而且还有一些差别,但是笔者建议使用python2.7,不建议一味追新。 安装过程可以参考 https://brew.sh/index_zh-cn.html http://blog.csdn.net/fancylovejava/article/details/39140373

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install python
which python
sudo easy_install pip
>>> 7/5
1
>>> 7 / 5
1
>>> 7 // 5
1
>>> 7 / 5.0
1.4
>>> 7 // 5.0 ##取整
1.0
>>> 5 * 2
10
>>> 5 ** 2 ##5的2次方
25
>>> 5^2
7
>>> 1&1
1
>>> 1&0
0
>>> 0|1
1
>>> _+100 ##_代表上一个结果
101
>>> 2 ** 7
128
>>> width =20
>>> height = 5 * 9
>>> width * height
900
>>> 3 * 'ae' + 'ad'
'aeaeaead'
>>> 'Py' 'thon' ##自动相连,变量就不行
'Python'
>>> text = ('Put several strings within parentheses '
...          'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'
>>> word='avdkjjfe'
>>> word[0]
'a'
>>> word[-1]
'e'
>>> word[-4]
'j'
>>> word[2:4]
'dk'
>>> word[:4]
'avdk'
>>> word[2:]
'dkjjfe'
>>> word[42] ##不允许越界
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> word[42:] ##后面的会被用''默认输出
''
>>> word[0]='d' ##python的字符串是不可变类型无法赋值
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> 'd'+word[1:]
'dvdkjjfe'
>>> len(word)
8
>>> u'Hello\u0020World !'
u'Hello World !'
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
>>> squares[:] ##副本
[1, 4, 9, 16, 25]
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> a=[0,1,2]
>>> b=[1,2,3]
>>> c=[a,b]
>>> c
[[0, 1, 2], [1, 2, 3]]
>>> while b<10:
...     print b
...     b=b+1
...
1
2
3
4
5
6
7
8
9
>>> x=1
>>> if x<0:
...     print 'error'
... elif x<10:
...     print 'xxx'
... else:
...     print 'more'
...
xxx
>>> x=[1,2,3,4]
>>> for i in x:
...     print i
...
1
2
3
4
>>> for i in x[:]:
...     print i
...
1
2
3
4
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(0, 10, 3)
[0, 3, 6, 9]
>>> def fid(n):
...     a,b=0,1
...     while a<n:
...             print a,
...             a,b=b,a+b
...
>>> fid(200)
0 1 1 2 3 5 8 13 21 34 55 89 144
>>> ddd = fid
>>> ddd(1000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> def fid2(n,a=0):
...     result=[ ]
...     b=1
...     while a<n:
...             result.append(a)
...             a,b =b,a+b
...     return result
...
>>> f100 = fid2(100)
>>> f100
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> from collections import deque
>>> queue = deque(["aa","vv","cc"])
>>> queue.popleft()
'aa'
>>> queue.popleft()
'vv'
>>> queue
deque(['cc'])
>>> def f(x): return x % 3 == 0 or x % 5 == 0
...
>>> filter(f, range(2, 25))
[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24]

>>> def cube(x): return x*x*x
...
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

>>> def mul(x,y):return x*y
...
>>> reduce(mul,range(2,11),1)
3628800
>>> squares = [x**2 for x in range(10)]
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
>>> freshfruit = ['  banana', '  loganberry ', 'passion fruit  ']
>>> [weapon.strip() for weapon in freshfruit]
['banana', 'loganberry', 'passion fruit']
>>> t =1 ,2,333
>>> t
(1, 2, 333)
>>> x,y,z = t
>>> x
1
>>> y
2
>>> z
333
>>> t[0]=2 ##元组不可变
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> singstr='aaaa'
>>> singtru='aaaa',  ##元组后面带逗号
>>> singstr
'aaaa'
>>> singtru
('aaaa',)
>>> len(singstr)
4
>>> len(singtru)
1


>>> ts
set([1, 2, 333])
>>> 1 in ts
True
>>> 3 in ts
False
>>> ta
set([2, 3, 444])
>>> ts - ta
set([1, 333])
>>> ts | ta
set([1, 2, 3, 444, 333])
>>> ts & ta
set([2])
>>> ts ^ ta
set([1, 3, 444, 333])


>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['jd']=323
>>> tel
{'jd': 323, 'sape': 4139, 'jack': 4098}
>>> tel.keys()
['jd', 'sape', 'jack']
>>> 'guido' in tel
False
>>> 'jd' in tel
True
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}
>>> for i, v in enumerate(['tic', 'tac', 'toe']):
...     print i, v
...
0 tic
1 tac
2 toe
>>> for i in xrange(1,10,2):
...     print i
...
1
3
5
7
9
>>> for i in reversed(xrange(1,10,2)):
...     print i
...
9
7
5
3
1

>>> tt
set([1, 2, 3, 44, 567])
>>> sorted(tt)
[1, 2, 3, 44, 567]
>>> '-3.14'.zfill(7)
'-003.14'