Python
Python是一种解释型的、面向对象的、带有动态语义的脚本语言
什么是变量?
变量是内存中存储数据的小空间。
变量的命名规则:
1、由数字,字母,下划线组成
2、数字不能开头
3、大小写敏感,不能使用关键字
变量命名协议:
最好使用英文命名让变量”见名知义”
变量赋值
无需声明类型,直接使用
变量赋值
简单赋值,Variable(变量)=Value(值)
多变量赋值Variable1,variable2,...=Value1,Value2,...
– 多变量赋值也可用于变量交换
多目标赋值,a=b=variable
什么是函数
函数是一段可以重复利用的代码,通过输入参数值,
返回需要的结果。
函数的定义
def 函数名(参数1[=默认值1],参数2[=默认值2]):
…..
return 表达式
函数名可以是字母、数字、下划线组成的字符串,但不能以数字开头
def arithmatic(x, y, operator):
result = {
'+':x + y,
'-':x - y,
'*':x * y,
'/':x / y,
}
函数的参数
def arithmatic(x=1, y=1, operator='+'):
result = {
'+':x + y,
'-':x - y,
'*':x * y,
'/':x / y,
}
return result.get(operator)
print(arithmatic(1, 2))
print(arithmatic(1, 2, '-'))
print(arithmatic(y=3, operator='*'))
print(arithmatic(x=4, operator='*'))
print(arithmatic(y=4,x=3, operator='-'))
函数的返回值
函数的返回值使用return语句,后面可以是变量
或表达式
def arithmatic(x=1, y=1, operator='+'): result = { '+':x + y, '-':x - y, '*':x * y, '/':x / y, } return result.get(operator)
函数的使用
def sum(a,b):
return a+b
def sub(a,b):
return a-b
def func():
x=1
y=2
m=3
n=4
return sum(x,y)*sub(m,n)
print func()
数据类型-元组(Tuple)
创建后不能再更改,通过圆括号()中用逗号分割,类似于java和c的
数组
创建元组
– 空元组
• tuple_name=()
– 含单个元素的元组
• tuple_name=(‘mtesting’,) 必须要写逗号
– 一般元组
• tuple_name=(‘mtesting’,’caichang’,’apple’)
– 二维元组
• tuple_name=((‘mtesting’,’caichang’,’apple’),(‘testing’,’develpoer’,’ui’))
访问
– 下标
• 整数
• 负数
• 切片
遍历
– range() 和len()
数据类型-元组(Tuple)
tuple_name = ('mtesting','testing','caichang','baobao')
print(tuple_name[0])
print(tuple_name[-1])
print(tuple_name[-3])
#从第2个开始到第3个
print(tuple_name[1:3])
print(tuple_name[0:-2])
print(tuple_name[2:-2])
#2维元组
tuple_name_2=(('mtesting','caichang','apple'),('testing','de
velpoer','ui'))
print(tuple_name_2[0][1])
print(tuple_name_2[1][1])
print(tuple_name_2[1][2])
rang()返回数字列表
len()返回元素个数
tuple_name = ('apple', 'banana','grape',
'orange', 'watermelon','mtesting',)
for i in range(len(tuple_name)):
print(tuple_name[i])
数据类型-列表(List)
最重要的数据类型,通常作为函数的返回类型,
可以添加、删除、查找,元素值也可以修改
创建元组
– list_name=[‘mtesting’,’caichang’]
操作
– 添加
• append(object)
• insert()
– 删除
• remove(value),如果value不在list中,将抛出ValueError的异常
• 如果有相同元素,将删除靠前元素
• pop(),删除最后一个元素
list = ['mtesting','caichang','test','devel']
print(list)
print(list[2])
#append 添加,追加
list.append('baobao')
print(list)
#在第1位上插入nihao
list.insert(1, 'nihao')
print(list)
#移除caichang元素
list.remove('caichang')
print(list)
#移除最后一个元素
list.pop()
print(list)
列表和元组一样,支持索引、分片、多维操作
列表自己也拥有属于自己特有的方法
list_name = [['apple', 'banana'], ['grape', 'orange'],
['watermelon',], ['mtesting',]]
for i in range(len(list_name)):
for j in range(len(list_name[i])):
print(list_name[i][j])
特有的方法:查找、排序、反转
查找:index、in
排序:sort、reverse
list_name = ['mtesting','caichang','test','apple']
print(list_name.index('test'))
print('caichang' in list_name)
list_name.sort()
print(list_name)
list_name.reverse()
print(list_name)
数据类型-字典
字典由“键-值”对组成,类似于java的map,
键区分大小写,一般用数字做键
创建
– dictionary_name = {key1:value1,key2:value2}
– 创建空字典
• dictionary_name ={}
– 书写顺序不是存储顺序,根据hashcode进行排列
dictionary_name =
{'m':'mtesting','cai':'caichang','b':'banana'}
#通过key访问
print(dictionary_name['cai'])
#通过数字key访问
dictionary_name_2 = {1:'mtesting',2:'caichang',3:'banana'}
print(dictionary_name_2[3])
数据类型-字典
涉及方法:
– items() 返回(key,value)元组组成的列表
– iteritems() 返回指向字典的遍历器
– setdefault(k) 创建新元素并返回默认值
– pop(k) 删除k对应的value值
– get(k) 返回k对应的value值
– keys() 返回key的列表
– values() 返回value的列表
– update(E) 字典E中的数据扩展到原字典
– copy() 复制字典中的所有数据
排序和复制
dict = {'a':'apple' , 'b':'grape' , 'c':'orange' , 'd':'banana'}
#按照key排序
print(sorted(dict.items(), key=lambda d:d[0], reverse=False))
#按照value排序
print(sorted(dict.items(), key=lambda d:d[1], reverse=False))
dict2 = {'a':'apple' , 'b':'grape'}
dict3 = {'c':'orange' , 'd':'banana'}
dict3 = dict2.copy()
print(dict3)
序列
序列是具有索引和切片能力的集合
元组、列表、字符串都属于序列5 序列
索引功能
str = "apple"
tuple = ("apple","banana","grape","orange")
list = ["apple","banana","grape","orange"]
print(str[0])
print(str[-1])
print(tuple[0])
print(tuple[-1])
print(list[0])
print(list[-1])5 序列
切片功能
str = "apple"
tuple = ("apple","banana","grape","orange")
list = ["apple","banana","grape","orange"]
print(str[:3])
print(str[3:])
print(str[1:-1])
print(str[:])
print(tuple[:3])
print(tuple[3:])
print(tuple[1:-1])
print(tuple[:])
print(list[:3])
print(list[3:])
print(list[1:-1])
print(list[:])

浙公网安备 33010602011771号