参考:莫烦Python基础教程
一、一些基本函数
1. print函数
print('hello world')
或print("hello world")
- 可()内字符串相加,数字相加
2. 算数运算
- 幂
**
如:3**3=27 - 取余数
%
如:8%3=2 - 比较:等于(
==
)、不等于(!=
),会返回True
和False
值
3. 自变量
1)赋值
- 可以将数值或字符串赋值给自变量,如:
a = 1
或a = 'apple'
;如果用多个单词表示自变量,加下划线,如:a_b = 1
- 可以一次定义多个自变量,如:
a,b,c = 1,2,3
4. while循环语句
while condition:
expressions
condition为true,执行expressions;否则,跳过while语句块,向下执行。
注意:使用while
时,注意循环内部要修改判断条件的值,否则while部分永远执行下去。
比如:
while True:
print("hello world")
程序将一直打印hello world
。若要停止程序,使用Ctrl + c终止程序。
1)数字
- 整数和浮点数也能进行Boolean数据操作(如果值等于
0
或0.0
,将返回False
,其余返回True
)Boolean数据类型
如:
condition = 10
while condition:
print(condition)
condition -= 1
输出结果:10,9,8,7,6,5,4,3,2,1
condition在while语句中,如果大于0,返回True,执行循环内部语句,至condition等于0,返回False。
2)None类型
若while后语句数据类型None
,返回False
。
3)集合类型
Python中集合类型有list
, turple
, dict
, set
等,若该集合对象作为while判断语句,若集合元素数量为0,返回False
,否则返回True
。
如:
a = range(10) # 0 1 2 3 4 5 6 7 8 9
while a: # 判断列表是否空,若不空,执行
print(a[-1]) #打印列表最后一个内容
a = a[:len(a)-1] #切片操作去掉最后一个元素,更新列表
程序返回:9,8,7,6,5,4,3,2,1,0
5. for循环
1)
for item in sequence:
expressions
如:
example_list = [1,2,3,4,5,6,7,111,222,3]
for i in example_list:
print(i)
输出结果依次为1,2,3,4,5,6,7,111,222,3,即为example_list中每个元素。
2)range使用
- range(start,stop)
start
是起始值,stop
是结束值,但不包括该值,即**[start,stop)**。 - range(stop)
相当于range(0,stop) - range(start,stop,step)
for i in range (0,13,5):
print(i)
输出:0,5,10
3)内置函数
python内置了list
、tuple
、dict
、set
在、四种基本集合,每个集合对象都能迭代。
- tuple类型
tup = ('hi',1.1,64)
for i in tup:
print(i)
输出:hi,1.1,64
- dictionary类型
dic = {}
dic['lan'] = 'python'
dic['version'] = 3.6
dic['platform'] = 64
for key in dic:
print(key,dic[key])
输出:lan python,version 3.6,platform 64
note:python3.6开始,dict插入变为有序,即字典整体变的有序;而之前版本如python2.7,字典插入乱序。
- set类型
s = set(['hi','hello','a','b','hi'])
for item in s:
print(item)
输出:hi,b,a,hello
note:set集合将去除重复项;输出结果也不按序。
4)迭代器 【没看懂】【python中class还不咋会】
5)生成器
二、Pytorch
三、复现过程遇到的
- Python 广播机制
广播机制矩阵条件
广播机制矩阵条件2
torch的广播机制