python数组使用中括号_在Python中不使用括号创建元组

python数组使用中括号

It is possible to create a tuple without using parenthesis in Python, this concept is known as "tuple packing".

可以在不使用括号的情况下在Python中创建元组 ,此概念称为“元组包装”

Here is an example of tuple packing and unpacking by creating a tuple without using parenthesis.

这是通过不使用括号创建元组来进行元组打包和解包示例

# declaring tuple containing student's infomation
# without using parenthesis
student = "prem", 21, 98.56, "New Delhi"

# printing its type
print('Type of student:', type(student))

# printing the tuple
print("student: ", student)

# unpacking tuple and printing each values
name, age, perc, city = student
print('Name:', name)
print('Age:', age)
print('Percentage:', perc)
print('City: ', city)

Output

输出量

Type of student: <class 'tuple'>
student:  ('prem', 21, 98.56, 'New Delhi')
Name: prem
Age: 21
Percentage: 98.56
City:  New Delhi

Here, we are declaring a tuple named student that contains name, age, percentage and city. Then, we are printing its type using type() method and then unpacking the tuple and assigning the student's information in separate variables name, age, perc and city. Finally, we are printing the student's information.

在这里,我们声明一个名为Student的元组,其中包含姓名,年龄,百分比和城市。 然后,我们使用type()方法打印其类型,然后解压缩元组,并在单独的变量name , age , perc和city中分配学生的信息。 最后,我们正在打印学生的信息。

翻译自: https://www.includehelp.com/python/creating-tuple-without-using-parenthesis.aspx

python数组使用中括号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值