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数组使用中括号