Categories
5G Network
Agile
Amazon EC2
Android
Angular
Ansible
Arduino
Artificial Intelligence
Augmented Reality
AWS
Azure
Big Data
Blockchain
BootStrap
Cache Teachniques
Cassandra
Commercial Insurance
C#
C++
Cloud
CD
CI
Cyber Security
Data Handling
Data using R
Data Science
DBMS
Design-Pattern
DevOps
ECMAScript
Fortify
Ethical Hacking
Framework
GIT
GIT Slack
Gradle
Hadoop
HBase
HDFS
Hibernate
Hive
HTML
Image Processing
IOT
JavaScript
Java
Jenkins
Jira
JUnit
Kibana
Linux
Machine Learning
MangoDB
MVC
NGINX
Onsen UI
Oracle
PHP
Python
QTP
R Language
Regression Analysis
React JS
Robotic
Salesforce
SAP
Selenium
Service Discovery
Service Now
SOAP UI
Spark SQL
Testing
TOGAF
Research Method
Virtual Reality
Vue.js
Home
Recent Q&A
Feedback
Ask a Question
What is Python tuple?
Home
>
Python
>
What is Python tuple?
Dec 12, 2019
in
Python
Q: Python Tuple
#python-tuple
1
Answer
0
votes
Dec 12, 2019
Python Tuple is used to store the sequence of immutable python objects. Tuple is similar to lists since the value of the items stored in the list can be changed whereas the tuple is immutable and the value of the items stored in the tuple can not be changed.
A tuple can be written as the collection of comma-separated values enclosed with the small brackets. A tuple can be defined as follows.
T1 = (101, "Ayush", 22)
T2 = ("Apple", "Banana", "Orange")
Example
tuple1 = (10, 20, 30, 40, 50, 60)
print(tuple1)
count = 0
for i in tuple1:
print("tuple1[%d] = %d"%(count, i));
Output:
(10, 20, 30, 40, 50, 60)
tuple1[0] = 10
tuple1[0] = 20
tuple1[0] = 30
tuple1[0] = 40
tuple1[0] = 50
tuple1[0] = 60
Example 2
tuple1 = tuple(input("Enter the tuple elements ..."))
print(tuple1)
count = 0
for i in tuple1:
print("tuple1[%d] = %s"%(count, i));
Output:
Enter the tuple elements ...12345
('1', '2', '3', '4', '5')
tuple1[0] = 1
tuple1[0] = 2
tuple1[0] = 3
tuple1[0] = 4
tuple1[0] = 5
However, if we try to reassign the items of a tuple, we would get an error as the tuple object doesn't support the item assignment.
An empty tuple can be written as follows.
T3 = ()
The tuple having a single value must include a comma as given below.
T4 = (90,)
A tuple is indexed in the same way as the lists. The items in the tuple can be accessed by using their specific index value.
We will see all these aspects of tuple in this section of the tutorial.
Tuple indexing and splitting
The indexing and slicing in tuple are similar to lists. The indexing in the tuple starts from 0 and goes to length(tuple) - 1.
The items in the tuple can be accessed by using the slice operator. Python also allows us to use the colon operator to access multiple items in the tuple.
Click here to read more about Python
Click here to read more about Insurance
Facebook
Twitter
LinkedIn
Related questions
0
votes
Q: Can you please explain what is a tuple in Python Language?
Aug 30, 2020
in
Python
#python-tuple
#tuple-python-language
0
votes
Q: What is the difference between list and tuple in Python?
May 16, 2020
in
Python
#python-tuple
#python-list
#list-python
#tuple-python
0
votes
Q: Can you please clarify what is the key distinction between a rundown and the tuple?
Aug 29, 2020
in
Python
#python-tuple
0
votes
Q: What Is The Principal Difference Between A List And The Tuple?
Dec 14, 2019
in
Python
#python-tuple
0
votes
Q: What Is A Tuple In Python?
Dec 19, 2019
in
Python
#tuple-python
0
votes
Q: python-tuple
Dec 14, 2019
in
Python
#python-tuples
...