Python教程:进击机器学习(二)--认识Python

in #python7 years ago

Self-intro: I am a graduate student at an unnamed institution in China :) The main focus is Computer vision using deep learning. I will update some of notes about deep learning at steemit. Hope that like-minded friends follow me, and we discuss and support with each other. Thanks for reading! @whytin

如果有Python基础或者有学习过的,可以直接跳过本教程。
本教程旨在快速认识和了解Python,作为机器学习的前调\坏笑。

了解Python

Python作为一个高级编程语言,它有哪一些特性:

  • 不同于C语言(编译型语言),Python是一种解释型语言。就是在执行一段Python代码之前并不需要对其编译。
  • Python是开源的、免费的
  • 跨平台,Linux/Unix、Windows、MacOS X。
  • 没有冗长的语法,可读性很强
  • 兼容并支持其他语言

Hello Python

打开Ipython shell,类似Matlab,简单的Hello world就是这样。

>>> print("Hello, world!")
Hello, world! 

Python简单的语法体现在,不需要声明变量和函数类型。

>>> a = 3
>>> type(a)   
<type 'int'>

如果是C语言:

int a = 3;

再来感受一下复数计算:

>>> a = 1.5 + 0.5j
>>> a.real
1.5
>>> a.imag
0.5

Python的基础我就不讲太多,如果之前没接触过编程的可以推荐参考:http://www.runoob.com/python/python-tutorial.html
书籍的话推荐《Python基础教程 (第2版) 》下载地址:Beginning.Python.From.Novice.to.Professional,2nd.Edition_chs

数组

定义数组不用声明类型,也不限制数组里面的类型。

>>> colors = [3, -200, 'hello']

数组索引是从0开始,跟C语言一样,跟Matlab有区别(从1开始)

>>> colors[0], colors[2]
(3, 'hello')

Python关于数组的也有很多内置函数,方便我们操作函数。
例如添加值list.append(x),插入list.insert(i, x),排序list.sort(key=None, reverse=False)。更多数组的内置函数参考:
https://docs.python.org/3/tutorial/datastructures.html#more-on-lists

字符串

Python对String的操作类似List:

>>> a = "hello"
>>> a[0]
'h'
>>> a[-1]
'o'

字符串分割:

>>> a = "hello, world!"
>>> a[2:10:2]               # 参数: a[开始:结束:步长]
'lo o'

字符串格式化:

>>> 'An integer: %i; a float: %f; another string: %s' % (1, 0.1, 'string')
'An integer: 1; a float: 0.100000; another string: string'

更多关于Python字符串参考:https://docs.python.org/tutorial/introduction.html#unicode-strings

字典

Python字典有一个特点,无序。
定义一个字典:

>>> tel = {'emmanuelle': 5752, 'sebastian': 5578}

添加键值对:

tel['francis'] = 5915

然后我们再来输出一下这个字典

>>> tel     
{'sebastian': 5578, 'francis': 5915, 'emmanuelle': 5752}

可以看到我们插入的键值对被放在中间,Python的字典是不按任何规则来排序的。
我们可以调用这个字典键和值通过以下两个函数:

>>> tel.keys()  
['sebastian', 'francis', 'emmanuelle']
>>> tel.values() 
[5578, 5915, 5752]

关于字典更高级的操作参考:https://docs.python.org/tutorial/datastructures.html#dictionaries
除了这几个常用的容器类型,还有元组(Tuples)和集合(Sets),因为在实际开发中比较少用,所以就不展开讲了。

需要注意的一点是Python对变量分配地址的机制,不像其他语言,Python是对对象分配地址,同一个对象只分配一个地址,但是可以有多个变量名:

In   [1]:  a = [1, 2, 3]
In   [2]:  b = a
In   [3]:  a
Out[3]:  [1, 2, 3]
In   [4]:  b
Out[4]:  [1, 2, 3]
In   [5]:  a is b
Out[5]:  True
In   [6]:  b[1] = 'hi!'
In   [7]:  a
Out[7]:  [1, 'hi!', 3]

控制语句

在C语言中,在同一个控制语句下的需要加{}来表示。而在Python中只需要通过缩进来保持控制块,在同一个控制块的缩进要一致。

>>> a = 10
>>> if a == 1:
...  print(1)
... elif a == 2:
...  print(2)
... else:
...  print('A lot')
A lot

for语句的用法:

>>> for i in range(4):
...  print(i)
0
1
2
3

while语句的用法:

>>> a = [1, 0, 2, 4]
>>> for element in a:
...  if element == 0:
...    continue
...  print(1. / element)
1.0
0.5
0.25

函数定义

Python的函数定义也只需要def 函数名(参数):
(同样需要缩进)

In [81]: def double_it(x):
....:     return x * 2
In [82]: double_it(3)
Out[82]: 6

脚本和模块

我们已经把最基础的数据类型、容器、控制语句和函数简单了解一下,接下来我们就是把它们写到程序里面。我们把代码写进文件的目的就是重复使用这些代码,就跟为什么我们要用函数类似。我们一般用Python编辑器(Windows下IDE可以用Pycharm来写Python程序)来创建.py文件。

比如我们构建一个测试文件test.py

message = "Hello how are you?"
for word in message.split():
  print word

在IPython里面我们可以用%run test.py来运行程序

In [1]: %run test.py
Hello
how
are
you?

在Windows或者Linux的命令行就使用 python test.py 来运行程序

$ python test.py
Hello
how
are
you?

但是我们有时候又不需要执行一个程序,只是希望把一些函数写到文件里面,需要的时候再拿出来用,有点类似C++的头文件。这个时候就需要用到导入模块(我们可以用来导入我们自己写的文件,也可以导入所需要的函数库):
假设我们需要导入Numpy库来对0-10进行采样,我们可以这样使用:

>>>import numpy as np
>>>np.linspace(0, 10, 6)
array([0, 2, 4, 6, 8, 10])

as可以对导入的文件设置别名,当我们在调用模块里面的函数时只需在用一个.来链接(module.function())

关于Python的介绍就先讲到这里,接下来是Python数学计算库Numpy。

Ref:http://www.scipy-lectures.org/intro/language/python_language.html

Sort:  

Nice post! I will follow you from now on.

Congratulations @whytin! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.20
TRX 0.14
JST 0.030
BTC 64294.06
ETH 3427.66
USDT 1.00
SBD 2.59