您的位置:首页 > 编程语言 > Python开发

这是一个新的开始 复习python的基础知识 fighting!

2018-03-05 12:26 501 查看
由于受伤的原因 我在上大学前就喜欢的技术内容被耽搁了好久 现在我就要 重新捡拾起来 先从 python的基础知识开始
内容如下:

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
fruits = ['pin apple','lemon','strawberry','orange','kiwi']
fruits[1]
fruits[-1]
fruits[::]
fruits[-2:-1]
fruits[::-1]

#复习列表的知识,列表的基本操作

#列表的中取子列表
fruits = ['pin apple','lemon','strawberry','orange','kiwi']
fruits[1]
fruits[-1]
fruits[::]
fruits[1]
fruits[-2:-1]

#列表中添加元素和合并列表
fruits.append('peach','fig')#append函数只能添加一个 TypeError: append() takes exactly one argument (2 given)
fruits.append('peach')
fruits
fruits = fruits + ['fig','melon','grape']#+号在不同的数据结构中的作用不同 两个矩阵之间用+,表示对应位置的元素相加
#两个列表之间用+号,表示:把两个列表连接起来
fruits

#改变列表中的类容
fruits[0:2]=['grape','mango']#列表的前两个的元素就换成了'grape','mango'
fruits

fruits.remove('mango')#这里函数后面要用圆括号
fruits

#查看帮助文档,圆括号里面用方括号
help([round])

#列表的复制
numbers = [10, 42, 28, 420]
numbers_copy = numbers#这里复制的是numbers的引用,类比于酒店的房间卡,都能对酒店房间里面的东西进行改变
numbers_copy
numbers_copy[0]=100
numbers_copy
numbers##重点:复制列表,直接用等号,原列表会随着复制列表的改变而改变
#列表复制的两种方法 
numbers_copy_1 = list(numbers)
numbers_copy_1

numbers_copy_2 = numbers[:]
numbers_copy_2

#字符串的操作
text = "Data Science"
text.upper()#大写
text.lower()#小写
text.capitalize()# 单词的意思是一大写字母写 字符串的第一个词的第一个字母大写,别的都小写
text.center()

text.capitalize()
numbers.index(8)#查询列表中8的索引号码

numbers = [10, 42, 28, 420]
numbers.reverse()#将列表倒过来
numbers

text.index("a")#查询第一个a在字符串的索引号
numbers.index(28)
numbers.count(100)
numbers.count(10)

text.count("a")#数出字符串中有几个a
text.count("e")#数出字符串中有几个e

import numpy as np
np.array([37,48,50]) + 1 #数组里面每个元素都+1

np.array([20,30,40]) * 2 #数组里面每个元素都乘以2

np.array([20,30,40]) / 2 #数组里面每个元素都除以2
np.array([20,30,40]) * np.array([20,30,40]) #两个数组里面对应位子的数字相乘
np.array([20,30,40]) - np.array([20,30,40]) #两个数组里面对应位子的数字相减

#矩阵的运算 
numbers = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [10, 11, 12]
])#输入一个矩阵

#去除矩阵的元素
numbers[-3:-1,0:2]#-1指的是最后一行 一般最后一个数字不取
numbers[2:4,0:2]
#把第三行取出来
numbers[2:3,0:3]#注意这种:的用法就是  取前不取后


learning_hours = [1,2,6,4,10]
grapes = [3,4,6,5,6]

np.mean(learning_hours)#mean就是均值的意思
np.mean(learning_hours)

np.median(learning_hours)#median就是中位数的意思
np.std(learning_hours)#std就是方差的意思
np.corrcoef(learning_hours,grapes)#np.corrcoef就是相关系数的意思

np.corrcoef(learning_hours,grapes)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: