前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >01月22日【Python3 基础知识】

01月22日【Python3 基础知识】

作者头像
py3study
发布2020-01-03 15:31:00
2400
发布2020-01-03 15:31:00
举报
文章被收录于专栏:python3python3python3

01月22日【Python3 基础知识】

2.4 计算器 2.5 tuple操作 2.6 dict 2.7 其他常用操作

2.4 计算器

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("##############################################")

2.5 tuple操作

# 元组的元素不能修改
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'))

2.6 dict

# 字典的定义:
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)

2.7 其他常用操作

# help: 方法解释
a = '123'
help(a.count)
# dir: 查看有什么方法可用
print(dir(a))
## type:  查看变量类型
print(type(a))
# len: 获取长度
print(len(a))
# isinstance: 判断变量是否某类型
print(isinstance(a, str))
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 01月22日【Python3 基础知识】
    • 2.4 计算器
      • 2.5 tuple操作
        • 2.6 dict
          • 2.7 其他常用操作
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档