向元组添加变量

编程入门 行业动态 更新时间:2024-10-11 19:21:17
本文介绍了向元组添加变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在学习 Python 并创建数据库连接.在尝试添加到数据库时,我正在考虑根据信息创建元组,然后将它们添加到数据库中.

I am learning Python and creating a database connection. While trying to add to the DB, I am thinking of creating tuples out of information and then add them to the DB.

我在做什么:我从用户那里获取信息并将其存储在变量中.我可以将这些变量添加到元组中吗?你能帮我看看语法吗?

What I am Doing: I am taking information from the user and store it in variables. Can I add these variables into a tuple? Can you please help me with the syntax?

另外,如果有一种有效的方法可以做到这一点,请分享...

Also if there is an efficient way of doing this, please share...

编辑让我稍微编辑一下这个问题......我只需要元组将信息输入到数据库中.将信息添加到数据库后,我应该删除元组吗?我的意思是我不再需要元组了.

EDIT Let me edit this question a bit...I only need the tuple to enter info into the DB. Once the information is added to the DB, should I delete the tuple? I mean I don't need the tuple anymore.

推荐答案

元组是不可变的;您不能在构造后更改它们包含的变量.但是,您可以将它们连接或切片以形成新的元组:

Tuples are immutable; you can't change which variables they contain after construction. However, you can concatenate or slice them to form new tuples:

a = (1, 2, 3) b = a + (4, 5, 6) # (1, 2, 3, 4, 5, 6) c = b[1:] # (2, 3, 4, 5, 6)

当然,从现有值构建它们:

And, of course, build them from existing values:

name = "Joe" age = 40 location = "New York" joe = (name, age, location)

更多推荐

向元组添加变量

本文发布于:2023-11-12 11:21:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581356.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!