1、函数
# 现在我想生成一个有10个字母a的列表
lst =[]
for i in range(10):
lst.append('a')
print(lst)
# 也可以这样写:lst = ['a' for i in range(10)]
# 那么如果我想做一个功能:生成n个m的列表
def f1(n,m):
lstf = []
for i in range(n):
lstf.append(m)
return lstf
# 定义好函数,其中有两个参数 → n代表个数,m代表元素值
# 同时注意,这里的lstf是局部变量
print(f1(10,'a'))
print(f1(5,'helloworld'))
print(f1(3,[1,2,3]))
# 执行函数
# 这里要注意return的意义
2.数组与图表
科学计算工具包:numpy
图表绘制工具包:matplotlib
数据分析工具包:pandas
import numpy as np
import matplotlib.pyplot as plt
# 导入模块
ar = np.array([1,2,3,4,5,6,7])
print(ar) # 输出数组,注意数组的格式:中括号,元素之间没有逗号(和列表区分)
print(ar.ndim) # 输出数组维度的个数(轴数),或者说“秩”,维度的数量也称rank
print(ar.shape) # 数组的维度,对于n行m列的数组,shape为(n,m)