{"id":3311,"date":"2022-11-21T19:58:47","date_gmt":"2022-11-21T19:58:47","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3311"},"modified":"2022-11-21T19:58:49","modified_gmt":"2022-11-21T19:58:49","slug":"creating-tuple-in-python","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/creating-tuple-in-python\/","title":{"rendered":"Creating Tuple in Python"},"content":{"rendered":"<p style=\"text-align: center;\">Creating Tuple in Python<\/p>\n<p>As mentioned before, the tuple is used to store a collection of data. The thing is that the tuples are immutable, which simply means that we cannot alter, insert or remove elements in the tuple. So now, as our very first step towards understanding the tuples, we are going to learn how to create tuples. Basically, we are going to have a look at two different ways to create tuples, using the parentheses (actually the parentheses are optional, but we need to separate the elements with commas), and the elements separated by commas, or we can also use the tuple constructor to create a tuple.<\/p>\n<h2 style=\"text-align: center;\">Creating Tuple in Python<\/h2>\n<p>Let\u2019s have a look at both ways. Here is a simple program, which demonstrates to us how to create a tuple with the help of parentheses. We would also use the type function, to check the type of object that we would create.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3312 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/CREATING-A-TUPLE-IN-PYTHON.jpg\" alt=\"\" width=\"800\" height=\"127\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/CREATING-A-TUPLE-IN-PYTHON.jpg 800w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/CREATING-A-TUPLE-IN-PYTHON-300x48.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/CREATING-A-TUPLE-IN-PYTHON-768x122.jpg 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>As you can see, we have created a tuple, with some elements into it(you can have it empty as well). After that, we are also checking the type of the tuple. You can try executing the above program and find that the object that we have created is a tuple.<\/p>\n<p>The tuple can have data of different types as well. Let\u2019s have a look at another program, which demonstrates us the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3313 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/CREATING-A-TUPLE-IN-PYTHON-1.jpg\" alt=\"\" width=\"790\" height=\"119\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/CREATING-A-TUPLE-IN-PYTHON-1.jpg 790w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/CREATING-A-TUPLE-IN-PYTHON-1-300x45.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/CREATING-A-TUPLE-IN-PYTHON-1-768x116.jpg 768w\" sizes=\"auto, (max-width: 790px) 100vw, 790px\" \/><\/p>\n<p>As you can see, we have a tuple, with different types of data, and after that, we have checked the type of the object. You can try executing the program and observe the output. You can play with the values as well. We can make it more complicated, by having a tuple, or another list inside a tuple, but for now, it can be considered as a big achievement for us, that we have created a tuple.<\/p>\n<p>We can also create a tuple, using the tuple constructor. Let\u2019s have a look at how we can do that. Have a look at the below program, which demonstrates the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3314 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/CREATING-A-TUPLE-IN-PYTHON-2.jpg\" alt=\"\" width=\"824\" height=\"101\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/CREATING-A-TUPLE-IN-PYTHON-2.jpg 824w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/CREATING-A-TUPLE-IN-PYTHON-2-300x37.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/CREATING-A-TUPLE-IN-PYTHON-2-768x94.jpg 768w\" sizes=\"auto, (max-width: 824px) 100vw, 824px\" \/><\/p>\n<p>As you can see, we have used the tuple constructor to create a tuple. Here, no argument is passed to the constructor, so an empty tuple is created. If we pass some iterable to the constructor, then the tuple is created from the iterable&#8217;s items.<\/p>\n<p>So, these are some of the ways, using which, we can create a tuple. Often, there can be situations, when we need to make use of tuples in our programs. So, we would make use of tuples, as and when required in our python programs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating Tuple in Python As mentioned before, the tuple is used to store a collection of data. The thing is that the tuples are immutable, which simply means that we cannot alter, insert or remove elements in the tuple. So now, as our very first step towards understanding the tuples, we are going to learn [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3311","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3311"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3311\/revisions"}],"predecessor-version":[{"id":4088,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3311\/revisions\/4088"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}