向元组添加变量 [英] Add Variables to Tuple

查看:66
本文介绍了向元组添加变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 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)

这篇关于向元组添加变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆