1.input的输入
variable=input()
表示运行后,可以在屏幕中输入一个数字,该数字会赋值给自变量。看代码:
a_input=input('please input a number:')
print('this number is:',a_input)
''''
please input a number:12 #12 是我在硬盘中输入的数字
this number is: 12
''''
input()
应用在if
语句中.
在下面代码中,需要将input()
定义成整型,因为在if
语句中自变量 a_input
对应的是1
and 2
整数型。输入的内容和判断句中对应的内容格式应该一致 (总之要用数字的时候需要进行类型转换)
也可以将if
语句中的1and2
定义成字符串,其中区别请读者自定写入代码查看。
a_input=int(input('please input a number:'))#注意这里要定义一个整数型
if a_input==1:
print('This is a good one')
elif a_input==2:
print('See you next time')
else:
print('Good luck')
""""
please input a number:1 #input 1
This is a good one
please input a number:2 #input 2
See you next time
please input a number:3 #input 3 or other number
Good luck
""""
2.元组,列表,字典
元组
叫做 tuple
,用小括号、或者无括号来表述,是一连串有顺序的数字。
a_tuple = (12, 3, 5, 15 , 6)
another_tuple = 12, 3, 5, 15 , 6
列表
而list
是以中括号来命名的:
a_list = [12, 3, 67, 7, 82]
小例子:
range(5)则代表0-4之间5个数字
a_list = [2,3,4,1,23]
for index in range(len(a_list)):
print('index=',index+1,'number in list = ',a_list[index])
运行结果:
index= 1 number in list = 2
index= 2 number in list = 3
index= 3 number in list = 4
index= 4 number in list = 1
index= 5 number in list = 23
List 添加
列表是一系列有序的数列,有一系列自带的功能, 例如:
a = [1,2,3,4,1,1,-1]
a.append(0) # 在a的最后面追加一个0
print(a)
# [1, 2, 3, 4, 1, 1, -1, 0]
在指定的地方添加项:
a = [1,2,3,4,1,1,-1]
a.insert(1,0) # 在位置1处添加0
print(a)
# [1, 0, 2, 3, 4, 1, 1, -1]
List 移除
删除项:
a = [1,2,3,4,1,1,-1]
a.remove(2) # 删除列表中第一个出现的值为2的项
print(a)
# [1, 3, 4, 1, 1, -1]
List 索引
显示特定位:
a = [1,2,3,4,1,1,-1]
print(a[0]) # 显示列表a的第0位的值
# 1
print(a[-1]) # 显示列表a的最末位的值
# -1
print(a[0:3]) # 显示列表a的从第0位 到 第2位(第3位之前) 的所有项的值
# [1, 2, 3]
print(a[5:]) # 显示列表a的第5位及以后的所有项的值
# [1, -1]
print(a[-3:]) # 显示列表a的倒数第3位及以后的所有项的值
# [1, 1, -1]
打印列表中的某个值的索引(index):
a = [1,2,3,4,1,1,-1]
print(a.index(2)) # 显示列表a中第一次出现的值为2的项的索引
# 1
统计列表中某值出现的次数:
a = [4,1,2,3,4,1,1,-1]
print(a.count(-1))
# 1
List 排序
对列表的项排序:
a = [4,1,2,3,4,1,1,-1]
a.sort() # 默认从小到大排序
print(a)
# [-1, 1, 1, 1, 2, 3, 4, 4]
a.sort(reverse=True) # 从大到小排序
print(a)
# [4, 4, 3, 2, 1, 1, 1, -1]
创建二维列表
一个一维的List是线性的List,多维List是一个平面的List:
a = [1,2,3,4,5] # 一行五列
multi_dim_a = [[1,2,3],
[2,3,4],
[3,4,5]] # 三行三列
索引
在上面定义的List中进行搜索:
print(a[1])
# 2
print(multi_dim_a[0][1])
# 2
用行数和列数来定位list中的值。这里用的是二维的列表,但可以有更多的维度。
创建字典
如果说List
是有顺序地输出输入的话,那么字典的存档形式则是无需顺序的, 我们来看一个例子:
在字典中,有key
和 value
两种元素,每一个key
对应一个value
, key
是名字, value
是内容。数字和字符串都可以当做key
或者value
, 在同一个字典中, 并不需要所有的key
或value
有相同的形式。 这样说, List
可以说是一种key
为有序数列的字典。
a_list = [1,2,3,4,5,6,7,8]
d1 = {'apple':1, 'pear':2, 'orange':3}
d2 = {1:'a', 2:'b', 3:'c'}
d3 = {1:'a', 'b':2, 'c':3}
print(d1['apple']) # 1
print(a_list[0]) # 1
del d1['pear']
print(d1) # {'orange': 3, 'apple': 1}
d1['b'] = 20
print(d1) # {'orange': 3, 'b': 20, 'pear': 2, 'apple': 1}
字典存储类型
以上的例子可以对列表中的元素进行增减。在打印出整个列表时,可以发现各个元素并没有按规律打印出来,进一步验证了字典是一个无序的容器。
def func():
return 0
d4 = {'apple':[1,2,3], 'pear':{1:3, 3:'a'}, 'orange':func}
print(d4['pear'][3]) # a
字典还可以以更多样的形式出现,例如字典的元素可以是一个List,或者再是一个列表,再或者是一个function。索引需要的项目时,只需要正确指定对应的key
就可以了。
3.import模块
import 的各种方法
import time
指 import time
模块,这个模块可以python自带,也可以是自己安装的,比如以后会用到numpy
这些模块,需要自己安装。
import time
print(time.localtime()) #这样就可以print 当地时间了
""""
time.struct_time(tm_year=2016, tm_mon=12, tm_mday=23, tm_hour=14, tm_min=12, tm_sec=48, tm_wday=4, tm_yday=358, tm_isdst=0)
""""
方法二:import time as __
,__
下划线缩写部分可以自己定义,在代码中把time 定义成 t
.
import time as t
print(t.localtime()) # 需要加t.前缀来引出功能
""""
time.struct_time(tm_year=2016, tm_mon=12, tm_mday=23, tm_hour=14, tm_min=12, tm_sec=48, tm_wday=4, tm_yday=358, tm_isdst=0)
""""
方法三:from time import time,localtime
,只import
自己想要的功能.
from time import time, localtime
print(localtime())
print(time())
""""
time.struct_time(tm_year=2016, tm_mon=12, tm_mday=23, tm_hour=14, tm_min=41, tm_sec=38, tm_wday=4, tm_yday=358, tm_isdst=0)
1482475298.709855
""""
方法四:from time import *
输入模块的所有功能
from time import *
print(localtime())
""""
time.struct_time(tm_year=2016, tm_mon=12, tm_mday=23, tm_hour=14, tm_min=41, tm_sec=38, tm_wday=4, tm_yday=358, tm_isdst=0)
""""
自建一个模块
这里和视频有点差别,我自己写了另外一个模块,是计算五年复利本息的模块,代码如下:模块写好后保存在默认文件夹:balance.py
d=float(input('Please enter what is your initial balance: \n'))
p=float(input('Please input what is the interest rate (as a number): \n'))
d=float(d+d*(p/100))
year=1
while year<=5:
d=float(d+d*p/100)
print('Your new balance after year:',year,'is',d)
year=year+1
print('your final year is',d)
调用自己的模块
新开一个脚本,import balance
import balance
""""
Please enter what is your initial balance:
50000 # 手动输入我的本金
Please input what is the interest rate (as a number):
2.3 #手动输入我的银行利息
Your new balance after year: 1 is 52326.45
Your new balance after year: 2 is 53529.95834999999
Your new balance after year: 3 is 54761.14739204999
Your new balance after year: 4 is 56020.653782067144
Your new balance after year: 5 is 57309.12881905469
your final year is 57309.12881905469
""""
模块存储路径说明
在Mac系统中,下载的python模块会被存储到外部路径site-packages
,同样,我们自己建的模块也可以放到这个路径,最后不会影响到自建模块的调用。