从 Python到Tensorflow 学习之路(二)

来源:互联网 发布:知乎 不愿光顾迷你ktv 编辑:程序博客网 时间:2024/06/06 00:21

上一篇博客简单介绍了Python一些知识点,接下来介绍的一些经验更高级和进阶些,但是却能大大简化我们的程序。

Python学习教程


python中的高级特性

Python中的切片,熟悉MATLAB的朋友应该知道MATLAB中的数组索引操作非常方便,而Python中的切片正是有异曲同工之妙。考虑下面一个问题,如何取出一个list或者tuple中的前三个元素,后三个元素等等。。。

print ('hello python')print ("hello python")

可以在字符串前输入“`实现多行效果

print('''hello python      python2      pytnon3''')

变量赋值问题,请运行下面代码,理解赋值实际上是将一个变量指向另另一个变量所指向的数据

a = '123'b = aa = '456'print a,b

运行结果实际上是456,123

Python中的list和tuple

list

  • len()函数可以获取list的长度
friendlist = ['Alice','Bob','Clark']print len(friendlist)
  • list的索引依旧是从0开始,可以用负数n来取倒数第|n|个元素
friendlist = ['Alice','Bob','Clark']print friendlist[-1], friendlist[-2], friendlist[-3]
  • list可以通过append将新元素加入末尾,也可以通过insert方法插入指定位置,还可以通过pop进行删除(可加索引),也可以直接赋值给指定的索引位置
#appendfriendlist = ['Alice','Bob', 'Clark']friendlist.append('David')print friendlist#insertfriendlist.insert(1,'Evil')print friendlist#popfriendlist.pop(3)print friendlist
  • list中的元素可以是相同的数据类型也可以是不同的数据类型
my_list = ['Apple', 123, 3.2]print my_list

tuple

  • 另外一种有序列表叫做tuple(元组),但是tuple初始化后不能进行修改,tuple是用小括号。
my_tuple = ('apple', 4, 3.14)print my_tuple
  • tuple的不变是指每个元素的指向不变,但是tuple的每个元素可以发生变化,但是如果改变下面的整数或者浮点数将会报错
my_tuple = (['apple','banana'], 4, 3.14)my_tuple[0][0] = 'orange'my_tuple[0][1] = 'lemon'print my_tuple

Python中的条件判断和循环

与C和C++不同没有else if只有elif

age = 12if age >= 18:    print 'adult'elif age >= 6:    print 'teenager'else:    print 'kid'

for…in循环

sum = 0for x in range(101):    sum += xprint sum

Python中的dict和set

dict类似于C++中的map,使用键和值存储,使用大括号(list用中括号,元组用小括号,dict则用大括号)

dictionary = {'Son':20, 'Father':50,'Mother':48}print dictionary['Son']

当不确定键对应的值是否存在时,有下面两种方法:

#Use indictionary = {'Son':20, 'Father':50,'Mother':48}print 'Sister' in dictionary #Use get methodprint dictionary.get('Sister')

前者会输出False,而后者会输出None.可以在get函数参数指定想要得到的value(如何找不到对应的value,则输出预设的值)

可以利用pop方法删除一个key,其对应的value也将从dict中删去(dict的key是不可变对象)

dictionary = {'Son':20, 'Father':50,'Mother':48}print dictionarydictionary.pop('Father')print dictionary 

set也是key的集合,但是不存储value.set中没有重复的key,重复的元素在set中会被自动过滤,这一点很方便.创建set需要使用一个list作为输入集合

my_set = set([1,2,2])print my_set

set可以看作是数学上无须且无重复元素的集合,因此两个set可以做数学上的交和补操作

set_1 = set([1,2,3])set_2 = set([2,3,4])print set_1&set_2print set_1|set_2###list

Python中的函数

cmp函数,cmp(a,b)如果a>b返回1,如果a==b返回0 如果a<b返回-1

print cmp(2,1)print cmp(1,1)print cmp(1,2)

Python中的函数可以起别名,函数名就是指向一个函数的引用,可以把函数名赋给一个变量

a = absprint a(-1.5)

Python中的空函数,利用pass语句占位,让代码可以运行起来

def judge_age(age):    if age >=18:        pass    elif age >= 12:        print 'teenager'    else:        print 'kid'judge_age(18)

Python中函数返回多个元素,实际上是返回一个tuple

#calculate a circle's area and lengthdef calculate_circle_parameters(radius):    area = 3.14*radius*radius    length = 6.28*radius    return area,lengthprint calculate_circle_parameters(4)

使用默认参数降低调用函数的难度

#calculate a circle's area and lengthdef calculate_circle_parameters(radius,pi = 3.14):    area = pi*radius*radius    length = pi*radius    return area,lengthprint calculate_circle_parameters(4)print calculate_circle_parameters(4, pi=3.1415926)

使用默认参数调用函数时,默认参数必须指向不变的对象,因为Python函数在定义的时候默认参数已经被计算出来,当不断使用默认参数时,就会使用上一次的结果。

def add_list(L = []):    L.append('0')    return Lprint add_list()print add_list()

Python中的可变参数,在函数定义时,在参数前面加号,即可让参数接收任意个参数(接收一个tuple).如果本身就有一个tuple或者list,可以在list或者tuple前面加号来把其中的元素变成可变参数调用函数.

def calculate_sum(*num):    result = 0    for x in num:        result += x    return resultprint calculate_sum()print calculate_sum(1,2,3)L = [2,3,4]print calculate_sum(*L)T = (4,5,6)print calculate_sum(*T)

Python中关键字参数允许传入0个或者任意个含参数名的参数,这些关键字参数在函数内部自动组装成一个dict

def person(name,age,**kw):    print 'name:',name, 'age:',age, 'others',kwperson('Bauer', 30)person('Bauer', 30, city='Guangzhou')kw = {'city':'Guangzhou','job':'teacher'}person('Bauer',30,**kw)

Python中定义函数,有必选参数,默认参数,可变参数和关键字参数,这四种参数可以混用.但是*参数定义的顺序必须是:必选参数、默认参数、可变参数和关键字参数*

def func(a,b,c=0,*args,**kw):    print 'a = ', a, 'b = ',b, 'c = ',c ,'args = ',args, 'kw = ',kwfunc(1,2)func(1,2,c=3)func(1,2,3,'a','b')func(1,2,3,'a','b',x=99)

我们下期见!~


原创粉丝点击