python nums函数_Python中基础使用及Numpy、Scipy、Matplotlib 使用教程

本文介绍了Python的基础数据类型和容器,如整型、浮点型、布尔型、字符串型,以及列表、元组、字典和集合的使用。接着,讲解了Python的函数和类。文章重点介绍了科学计算库Numpy、SciPy和Matplotlib,包括数组操作、矩阵运算、图像处理和绘图方法。通过实例展示了这些库在科学计算中的应用。
摘要由CSDN通过智能技术生成

Python是本身是一个通用的编程语言,但其具有一些库(numpy,scipy,matplotlib)用于科学运算,原文的Python的版本是3.5。

本文先进行Python的基本介绍(数据、容器、函数、类)然后再介绍Numpy库、SciPy库以及MatPlotlib库的常用方法。

Python基本数据类型

整型 int ,浮点型 float (注:Python没有i++这种语句,乘方用x**i)

布尔型 booleans (注:Python 使用 and 、or、not替代C语言的&&、||、!;t!=f表示t和f的异或)

字符串型 strings (注:Python中字符串可以用单引号或双引号表示,一些常用的函数如大小写、去除空格、字符串替换、空格位置调整等,去掉空格s.strip()字符串替换s.replace('l','ell')具体使用什么函数可以现查)

Python容器

列表 list,与数组相同但是可变大小(注:下标从0开始算,与maltab从1开始算不同)

xs = [3, 1, 2] # Create a list

print(xs, xs[2]) # Prints "[3, 1, 2] 2" 注意小标从0开始

print(xs[-1]) # 负数下标从后往前数; prints "2"

xs[2] = 'foo' # 列表的元素可以类型不同

print(xs) # Prints "[3, 1, 'foo']"

xs.append('bar') # 增加元素

print(xs) # Prints "[3, 1, 'foo', 'bar']"

x = xs.pop() # 删除最后一个元素

print(x, xs) # Prints "bar [3, 1, 'foo']"

切片:list使用中的一个重要方法(注:切片的下标左边界包含,右边界不包含,与matlab两边界都包含不同)

nums = list(range(5)) # 利用list()函数生成列表

print(nums) # Prints "[0, 1, 2, 3, 4]"

print(nums[2:4]) # 切片下标2到(4-1); prints "[2, 3]"

print(nums[2:]) # 切片下标2到最后; prints "[2, 3, 4]"

print(nums[:2]) # 切片开始到下标(2-1); prints "[0, 1]"

print(nums[:]) # 切片全部; prints "[0, 1, 2, 3, 4]"

print(nums[:-1]) # 切片开始到(-1-1); prints "[0, 1, 2, 3]"

nums[2:4] = [8, 9] # 将下标2,3的值替换成8,9

print(nums) # Prints "[0, 1, 8, 9, 4]"

列表元素循环(与matlab相同)

animals = ['cat', 'dog', 'monkey']

for animal in animals:

print(animal)

# 三行分别打印 cat dog monkey

也可以利用内建函数enumerate(Lists)取得下标(从0开始)和元素

animals = ['cat', 'dog', 'monkey']

for idx, animal in enumerate(animals):

print('#%d: %s' % (idx + 1, animal))

# Prints "#1: cat", "#2: dog", "#3: monkey", each on its own line

列表解析:一种生成新的列表的方式,举两个例子

nums = [0, 1, 2, 3, 4]

squares = [x ** 2 for x in nums]

print(squares) # Prints [0, 1, 4, 9, 16]

nums = [0, 1, 2, 3, 4]

even_squares = [x ** 2 for x in nums if x % 2 == 0]

print(even_squares) # Prints "[0, 4, 16]"

字典 dictionaries,存储键值对(key,value)

d = {'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with some data

print(d['cat']) # Get an entry from a dictionary; prints "cute"

print('cat' in d) # Check if a dictionary has a given key; prints "True"

d['fish'] = 'wet' # Set an entry in a dictionary

print(d['fish']) # Prints "wet"

# print(d['monkey']) # KeyError: 'monkey' not a key of d

print(d.get('monkey', 'N/A')) # Get an element with a default; prints "N/A"

print(d.get('fish', 'N/A')) # Get an element with a default; prints "wet"

del d['fish'] # Remove an element from a dictionary

print(d.get('fish', 'N/A')) # "fish" is no longer a key; prints "N/A"

字典循环

d = {'person': 2, 'cat': 4, 'spider': 8}

for animal in d:

legs = d[animal]

print('A %s has %d legs' % (animal, legs))

# Prints "A person has 2 legs", "A cat has 4 legs", "A spider has 8 legs"

也可以利用内建函数d.items()取得键和值

d = {'person': 2, 'cat': 4, 'spider': 8}

for animal, legs in d.items():

print('A %s has %d legs' % (animal, legs))

# Prints "A person has 2 legs", "A cat has 4 legs", "A spider has 8 legs"

字典解析,利用表达式生成字典

nums = [0, 1, 2, 3, 4]

even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}

print(even_num_to_square) # Prints "{0: 0, 2: 4, 4: 16}"

集合sets(注:集合中元素无序且不重复)

animals = {'cat', 'dog'}

print('cat' in animals) # Check if an element is in a set; prints "True"

print('fish' in animals) # prints "False"

animals.add('fish') # Add an element to a set

print('fish' in animals) # Prints "True"

print(len(animals)) # Number of elements i

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值