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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.