首页 > 编程语言 >python入门之元组(tuple)

python入门之元组(tuple)

时间:2023-02-02 15:35:06浏览次数:45  
标签:tuple python 31 30 元组 print month day

"""
元组 tuple
1.由一系列变量组成的不可变系列容器
2.不可变是指一但创建,不可以再添加/删除/修改元素
3.列表用[],元组用()
4.列表和元组之间能互相转换
5.元组切片还是元组,列表切片还是列表,字符串切片还是字符串

"""
# 1.创建元组
# 空
tuple01 = ()

# 列表  --> 元组
tuple01 = tuple(["a", "b"])
print(tuple01)

# 元组 --> 列表
list01 = list(tuple01)
print(list01)

# 如果元组只有一个元素要加逗号
# tuple02 = (100)
# print(type(tuple02)) # int
tuple02 = (100,)
print(type(tuple02))  # tuple

# 具有默认值
tuple01 = (1, 2, 3)
print(tuple01)
# 不能变化

# 2.获取元素(索引  切片)
tuple03 = ("a", "b", "c", "d")
e01 = tuple03[1]
print(type(e01))  # str

e02 = tuple03[-2:]
print(type(e02))  # tuple

tuple04 = (100, 200)
# 可以直接将元组赋值给多个变量
a, b = tuple04
print(a)  # 100
print(b)  # 200

# 3.遍历元素
# 正向
for item in tuple04:
    print(item)
# 反向
# 1 0
for i in range(len(tuple04) - 1, -1, -1):
    print(tuple04[i])

 

练习:

"""
    练习1:
        借助元组完成下列功能
"""
# month = int(input("请输入月份:"))
# if month < 1 or month > 12:
#     print("输入有误....")
# elif month == 2:
#     print("当前月份是28天")
# elif month == 4 or month == 6 or month == 9\
#         or month == 11:
#     print("当前月份是30天")
# else:
#     print("当前月份是31天")

# 方式1:
month = int(input("请输入月份:"))
if month < 1 or month > 12:
    print("输入有误....")
elif month == 2:
    print("当前月份是28天")
elif month in (4, 6, 9, 11):
    print("当前月份是30天")
else:
    print("当前月份是31天")

# 方法2:
month = int(input("请输入月份:"))
if month < 1 or month > 12:
    print("输入有误....")
else:
    # 将每月天数存入元组
    day_of_month = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
    # 5月
    print(day_of_month[month - 1])

 

# 练习2:
#     在控制台中录入日期(年月),计算这是这一年的第几天
#     例如:3月5日
#          1月天数+ 2月天数 + 5

# 方法1:
day_of_month = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
month = int(input("请输入月份:"))
day = int(input("请输入日:"))
# 累加前几个月的天数
total_day = 0
# 思路1:
# month = 3
# # 0  1
# for item in range(2):
for i in range(month - 1):
    total_day += day_of_month[i]
# 与思路1对比:
# day_of_month[1]
# month = 5
# # 0 1 2 3
# for item in range(4):

# 累加当月天数
total_day += day
print("是这年的第%d天....." % total_day)

# 方法2:使用切片的方法去实现
day_of_month = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
month = int(input("请输入月份:"))
day = int(input("请输入日:"))
total_day = 0
total_day = sum(day_of_month[:month - 1])
total_day += day
print("是这年的第%d天....." % total_day)

 

标签:tuple,python,31,30,元组,print,month,day
From: https://www.cnblogs.com/Remick/p/17086161.html

相关文章

  • 单一资产VaR风险--基于python
    数据源:使用 AKShare包。它是一个免费、开源的Python财经数据接口包。网址https://www.akshare.xyz/index.html一、获取数据:计算日收益率:importpandasaspdimport......
  • 单一资产VBA风险--基于python处理
    数据来源:AKShare包;介绍:https://www.akshare.xyz/index.html;它是一个免费、开源的Python财经数据接口包。一、计算日收益率;importpandasaspdimportnumpyasnpim......
  • 多资产VBA风险--基于python处理
    一、数据准备,先在excel表格上计算每日的波动率;excel数据为:  二、数据导入:importpandasaspdimportnumpyasnpimportakshareasakimportscipy.statsass......
  • Linux下Python2.x升级Python3.7
    一、查看当前Python版本[root@localhost~]#python-VPython2.7.5二、下载新的python包并安装进入Python官网(https://www.python.org),选择需要的版本。此处我选择Py......
  • Python用PrettyTable输出漂亮的表格
     https://linuxops.org/blog/python/prettytable.html https://github.com/jazzband/prettytable PrettyTable      PrettyTableletsyouprinttablesi......
  • 基于pythondetcp多个客户端连接服务器
    壹:TCP是面向运输层的协议。使用TCP协议之前,必须先建立TCP连接,在传输完成后,必须释放已经建立的TCP连接。每条TCP连接只能有两个端,每一条TCP连接只能是点对点的。TCP提供可......
  • Python代码打包成可执行文件的常用方法!
    大家都知道,平时我们写的Python程序,其运行主要依赖于Python环境。当我们想要提供给别人使用或者更换电脑时,就需要重新安装Python环境,十分麻烦,因此我们想要将它传给任何人......
  • python pandas.DataFrame 编写的Excel分割工具
    importpandasaspdfrompandasimportDataFrameasDFimportosclassSplitExcel(object): """ 分割任意的Excel文件,根据指定的列对文件进行分割,并存储到指......
  • python读文件
    第一种方法#encoding=utf-8file=open("./man_data.txt","r")try:printfile.read()finally:file.close()第二种方法#encoding=utf-8try:withopen('./man_dat......
  • python数据持久存储:pickle模块的使用
    python的pickle模块实现了基本的数据序列和反序列化。通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储;通过pickle模块的反序列化操作,我们......