The tuple is a data type in Python. It is an immutable, ordered collection of items. Once it is created, it can not be modified or changed. Tuples do not stick to a single data type, which means tuples can contain items of different data types.
Lists and tuples are comparable in that they both contain an ordered collection of elements. Nevertheless, tuples are immutable. Let’s see how data are stored in tuples:
tup1 = ("letsupdateskills", "Python", 'a', 'b')
tup2 = (1, 2, 3, 4, 5)
tup3 = ("a", "b", 1, 2)
Tuples are useful when you wish to store a group of values, such as coordinates or dates, that shouldn't change while the application is running.
The following are the characteristics of tuples:
Items are entered into parenthesis() and separated with commas to construct tuples.
# An empty tuple
empty_tuple = ()
# A tuple with multiple data types
your_tuple = (1, "Hello", 3.14)
# A tuple with one element (note the trailing comma)
single_element_tuple = (5,)
To access an element of a tuple, you use square brackets with an index. You can also access a range of elements (a slice) by separating the starting index and ending index with a colon (:).
# creattion of a tuple
my_tuple = (1, 2, 3, "hii", "hello", 'a')
# Accessing element from a tuple
print(my_tuple[1]) # output: 2
# accessing elements in a range
print(my_tuple[0:3]) # output (1, 2, 3)
You can't update a tuple because it is immutable. However, you can merge two tuples, or we can create a new tuple by taking some portions of the existing tuple. This can be seen in the following demonstrations.
tup1 = (1, 2, 2.5);
tup2 = ('a', 'b', 'c', 'abc');
# So let's create a new tuple as follows
tup3 = tup1 + tup2;
print (tup3); # Output: (1, 2, 2.5, 'a', 'b', 'c', 'abc')
Deleting individual elements of tuples is not possible because tuples are immutable. However, you can delete the entire tuple itself using the del statement. This can be seen in the following demonstrations.
tup = ('hello', 'world', "letsupdateskill");
print (tup); # output: ('hello', 'world', 'letsupdateskill')
del tup;
print ("After deleting tup : ");
print (tup); # output: name 'tup' is not defined
Following are the operations on a tuple:
To get the length of the tuple use the len function. Here, is an example:
my_tuple = (1, 2, 3)
length = len(my_tuple)
print(length) # output: 3
To concatenate one tuple with another use the + operator. Here, is an example:
my_tuple1 = (1, 2, 3)
my_tuple2 = ("Hello", "World")
print(my_tuple1+my_tuple2) # output: (1, 2, 3, 'Hello', 'World')
In Python, you can repeat the element of the tuple using the * operator. Here, is an example:
my_tuple = (1, 2, 3)
# repetition of elements
print(my_tuple * 2) # output: (1, 2, 3, 1, 2, 3)
In Python, you can check membership in a tuple using the in operator. Here is an example:
num_tuple = (10, 20, 30, 40, 50)
print(20 in num_tuple) # Output: True
print(60 in num_tuple) # Output: False
The tuple is a data type in Python. It is an immutable, ordered collection of items. Once it is created, it can not be modified or changed. Tuples do not stick to a single data type, which means tuples can contain items of different data types.
Lists and tuples are comparable in that they both contain an ordered collection of elements. Nevertheless, tuples are immutable. Let’s see how data are stored in tuples:
pythontup1 = ("letsupdateskills", "Python", 'a', 'b') tup2 = (1, 2, 3, 4, 5) tup3 = ("a", "b", 1, 2)
Tuples are useful when you wish to store a group of values, such as coordinates or dates, that shouldn't change while the application is running.
The following are the characteristics of tuples:
Items are entered into parenthesis() and separated with commas to construct tuples.
python# An empty tuple empty_tuple = () # A tuple with multiple data types your_tuple = (1, "Hello", 3.14) # A tuple with one element (note the trailing comma) single_element_tuple = (5,)
To access an element of a tuple, you use square brackets with an index. You can also access a range of elements (a slice) by separating the starting index and ending index with a colon (:).
python# creattion of a tuple my_tuple = (1, 2, 3, "hii", "hello", 'a') # Accessing element from a tuple print(my_tuple[1]) # output: 2 # accessing elements in a range print(my_tuple[0:3]) # output (1, 2, 3)
You can't update a tuple because it is immutable. However, you can merge two tuples, or we can create a new tuple by taking some portions of the existing tuple. This can be seen in the following demonstrations.
pythontup1 = (1, 2, 2.5); tup2 = ('a', 'b', 'c', 'abc'); # So let's create a new tuple as follows tup3 = tup1 + tup2; print (tup3); # Output: (1, 2, 2.5, 'a', 'b', 'c', 'abc')
Deleting individual elements of tuples is not possible because tuples are immutable. However, you can delete the entire tuple itself using the del statement. This can be seen in the following demonstrations.
pythontup = ('hello', 'world', "letsupdateskill"); print (tup); # output: ('hello', 'world', 'letsupdateskill') del tup; print ("After deleting tup : "); print (tup); # output: name 'tup' is not defined
Following are the operations on a tuple:
To get the length of the tuple use the len function. Here, is an example:
pythonmy_tuple = (1, 2, 3) length = len(my_tuple) print(length) # output: 3
To concatenate one tuple with another use the + operator. Here, is an example:
pythonmy_tuple1 = (1, 2, 3) my_tuple2 = ("Hello", "World") print(my_tuple1+my_tuple2) # output: (1, 2, 3, 'Hello', 'World')
In Python, you can repeat the element of the tuple using the * operator. Here, is an example:
pythonmy_tuple = (1, 2, 3) # repetition of elements print(my_tuple * 2) # output: (1, 2, 3, 1, 2, 3)
In Python, you can check membership in a tuple using the in operator. Here is an example:
pythonnum_tuple = (10, 20, 30, 40, 50) print(20 in num_tuple) # Output: True print(60 in num_tuple) # Output: False
Python is commonly used for developing websites and software, task automation, data analysis, and data visualisation. Since it's relatively easy to learn, Python has been adopted by many non-programmers, such as accountants and scientists, for a variety of everyday tasks, like organising finances.
Learning Curve: Python is generally considered easier to learn for beginners due to its simplicity, while Java is more complex but provides a deeper understanding of how programming works.
The point is that Java is more complicated to learn than Python. It doesn't matter the order. You will have to do some things in Java that you don't in Python. The general programming skills you learn from using either language will transfer to another.
Read on for tips on how to maximize your learning. In general, it takes around two to six months to learn the fundamentals of Python. But you can learn enough to write your first short program in a matter of minutes. Developing mastery of Python's vast array of libraries can take months or years.
6 Top Tips for Learning Python
The following is a step-by-step guide for beginners interested in learning Python using Windows.
Best YouTube Channels to Learn Python
Write your first Python programStart by writing a simple Python program, such as a classic "Hello, World!" script. This process will help you understand the syntax and structure of Python code.
The average salary for Python Developer is ₹5,55,000 per year in the India. The average additional cash compensation for a Python Developer is within a range from ₹3,000 - ₹1,20,000.
Copyrights © 2024 letsupdateskills All rights reserved