Home » Python

Creating tuple without using parenthesis in Python

In this tutorial, we are going to learn how to create tuple without using parenthesis in Python programming language?
Submitted by IncludeHelp, on November 14, 2019

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

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.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.