发布时间:2019-09-25 08:16:40编辑:auto阅读(1563)
2.4 计算器
2.5 tuple操作
2.6 dict
2.7 其他常用操作
def add(string):
total = 0
numbers = []
numbers += string.split("+")
for num in numbers:
total += int(num.strip())
print("{0} = {1}".format(string, total))
#
def reduce(string):
result = 0
numbers = []
numbers += string.split("-")
result = int(numbers[0].strip())
numbers.pop(0)
for num in numbers:
result -= int(num.strip())
print("{0} = {1}".format(string, result))
#
def ride(string):
total = 1
numbers = []
numbers += string.split("*")
for num in numbers:
total *= int(num.strip())
print("{0} = {1}".format(string, total))
#
def division(string):
result = 0
numbers = []
numbers += string.split("/")
result = int(numbers[0].strip())
numbers.pop(0)
for num in numbers:
result /= int(num.strip())
print("{0} = {1}".format(string, result))
if __name__ == '__main__':
print("##############################################")
print("1: +法")
print("2: -法")
print("3: *法")
print("4: /法")
method = input("请选择: ")
string = input("请输入:")
if method == "1":
add(string)
elif method == "2":
reduce(string)
elif method == "3":
ride(string)
elif method == "4":
division(string)
else:
print("输入错误")
print("##############################################")
# 元组的元素不能修改
t = ('a', 'b', 3, 'b')
print(t)
print(t[0]) # 按下标输出元素
# index: 按元素输出下标
print(t.index('b')) # 默认左往右
print(t.index('b', -1)) # 倒序查找
# count:统计字符个数
print(t.count('b'))
# 字典的定义:
d1 = dict(name = 'long', age = 30)
d2 = {'id':101, 'name':'longlong'}
d3 = dict([('name', 'long'), ('age', 20)])
print(d1)
print(d2)
print(d3)
# 方法
# get(key): 根据key获取value
# setdefault: 根据key获取value,如果key不存在,可以设定默认的value
print((d1.get('name')))
print(d1.setdefault('dizhi', 'guangxi'))
#
print(d1.keys())
print(d1.values())
#
for x, y in d1.items():
print("{0}:{1}".format(x, y))
# update: 根据key更新valye;如果没有key则会添加该键值对
print(d1)
d1.update(d2)
print(d1)
# pop:根据key弹出value
print(d3.pop('name'))
print(d3)
# help: 方法解释
a = '123'
help(a.count)
# dir: 查看有什么方法可用
print(dir(a))
## type: 查看变量类型
print(type(a))
# len: 获取长度
print(len(a))
# isinstance: 判断变量是否某类型
print(isinstance(a, str))
上一篇: 01月24日【Python3 基础知识】
下一篇: python3 面向对象的创建及实例的属
47768
46278
37155
34658
29248
25908
24781
19882
19444
17938
5734°
6339°
5856°
5905°
7009°
5844°
5865°
6375°
6335°
7701°