「机器学习系列1」Python基础-变量|表达式|函数
Python Base:Variable
1.variable变量
a,b,c,d= 3,3.0,'hello world',True
print(type(a),type(b),type(c),type(d))
<class 'int'> <class 'float'> <class 'str'> <class 'bool'>
整型int 浮点型float 字符串str 布尔型bool
2.列表list
list_temp = [1,2,3,4,5,6,7,8]
列表属性方法:dir(list_temp)
(1)添加元素:list_temp.append(9) list_temp.extend([10,11])
(2)排序 :list_temp = [2,5,1,6,3,4]
正排序:list_temp.sort()
逆排序list_temp.sort(reverse=True)
(3)删除元素:list_temp.pop() 删除最后一个元素
(4)反转列表:list_temp.reverse()
(5)列表指定位置插入一个元素:list_temp.insert(2,'insert') 第三个位置插入一个元素
(6)删除指定数值:list_temp.remove(2)
3.元组tuple
tu = (1,2,3)
元组的属性方法:dir(tu)
count()/index()
4.字典Dictionary
dic = {'a':1, 'b':0.2, 'c': 'Hello'}
dic.keys()/dic.values()
按照关键字提取数值:dic['b']/dic.get('b')
for key in dic:
print(key, dic.get(key))
Python Base:Statement
1.for 循环语句
a= [1,2,3,4,5,6,7,8]
for ele in a:
if ele%2 == 0:
print(ele)
print('done')
2.if判断语句
a = 10
if a > 10 :
print("a is over 10")
elif a == 10:
print("a is equal to 10")
else:
print("a is below 10")
Python Base: Function
1.定义函数及调用函数
def addNumber(a,b):
return a + b
addNumber(3,5)
2.匿名函数
square = lambda x: x*x
square(3)