class 定义一个类
注意冒号 def class 后面都有冒号
class 定义一个类, 后面的类别首字母推荐以大写的形式定义,比如Calculator. class可以先定义自己的属性,比如该属性的名称可以写为 name=‘Good Calculator’. class后面还可以跟def, 定义一个函数. 比如def add(self,x,y): 加法, 输出print(x+y). 其他的函数定义方法一样,注意这里的self 是默认值.
class Calculator: #首字母要大写,冒号不能缺
name='Good Calculator' #该行为class的属性
price=18
def add(self,x,y):
print(self.name)
result = x + y
print(result)
def minus(self,x,y):
result=x-y
print(result)
def times(self,x,y):
print(x*y)
def divide(self,x,y):
print(x/y)
""""
>>> cal=Calculator() #注意这里运行class的时候要加"()",否则调用下面函数的时候会出现错误,导致无法调用.
>>> cal.name
'Good Calculator'
>>> cal.price
18
>>> cal.add(10,20)
Good Calculator
30
>>> cal.minus(10,20)
-10
>>> cal.times(10,20)
200
>>> cal.divide(10,20)
0.5
>>>
""""
总结
注意定义自变量cal等于Calculator要加括号“()” ,cal=Calculator()否则运行下面函数的时候会出现错误,导致无法调用.
self 介绍
https://blog.csdn.net/daocaoren1543169565/article/details/80626035
init
__init__可以理解成初始化class的变量,取自英文中initial 最初的意思.可以在运行时,给初始值附值,
运行c=Calculator(‘bad calculator’,18,17,16,15),然后调出每个初始值的值。看如下代码。
class Calculator:
name='good calculator'
price=18
def __init__(self,name,price,height,width,weight): # 注意,这里的下划线是双下划线
self.name=name
self.price=price
self.h=height
self.wi=width
self.we=weight
""""
>>> c=Calculator('bad calculator',18,17,16,15)
>>> c.name
'bad calculator'
>>> c.price
18
>>> c.h
17
>>> c.wi
16
>>> c.we
15
>>>
""""
如何设置属性的默认值, 直接在def里输入即可,如下:
def init(self,name,price,height=10,width=14,weight=16):查看运行结果, 三个有默认值的属性,可以直接输出默认值,这些默认值可以在code中更改, 比如c.wi=17再输出c.wi就会把wi属性值更改为17.同理可推其他属性的更改方法。
class Calculator:
name='good calculator'
price=18
def __init__(self,name,price,hight=10,width=14,weight=16): #后面三个属性设置默认值,查看运行
self.name=name
self.price=price
self.h=hight
self.wi=width
self.we=weight
""""
>>> c=Calculator('bad calculator',18)
>>> c.h
10
>>> c.wi
14
>>> c.we
16
>>> c.we=17
>>> c.we
17
""""
总结
def init(self,name,price,height,width,weight): 注意,这里的下划线是双下划线
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
""""
input扩展
用input()来判断成绩
score=int(input('Please input your score: \n'))
if score>=90:
print('Congradulation, you get an A')
elif score >=80:
print('You get a B')
elif score >=70:
print('You get a C')
elif score >=60:
print('You get a D')
else:
print('Sorry, You are failed ')
""""
Please input your score:
100 #手动输入
Congradulation, you get an A
""""
Tuple
叫做 tuple,用小括号、或者无括号来表述,是一连串有顺序的数字。
a_tuple = (12, 3, 5, 15 , 6)
another_tuple = 12, 3, 5, 15 , 6
List
而list是以中括号来命名的:
a_list = [12, 3, 67, 7, 82]
两者对比
他们的元素可以一个一个地被迭代、输出、运用、定位取值:
for content in a_list:
print(content)
"""
12
3
67
7
82
"""
for content_tuple in a_tuple:
print(content_tuple)
"""
12
3
5
15
6
"""
下一个例子,依次输出a_tuple和a_list中的各个元素:
for index in range(len(a_list)):
print("index = ", index, ", number in list = ", a_list[index])
"""
index = 0 , number in list = 12
index = 1 , number in list = 3
index = 2 , number in list = 67
index = 3 , number in list = 7
index = 4 , number in list = 82
"""
for index in range(len(a_tuple)):
print("index = ", index, ", number in tuple = ", a_tuple[index])
"""
index = 0 , number in tuple = 12
index = 1 , number in tuple = 3
index = 2 , number in tuple = 5
index = 3 , number in tuple = 15
index = 4 , number in tuple = 6
"""
list 列表
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中进行搜索:
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就可以了。
字典中注意所有的字符串都要带着 ‘’** 包括【】中括号引用的时候**