Tuples in Python

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 15:- Tuples in Python

In Python, a tuple is a data structure similar to a list, but with one crucial difference: tuples are immutable. Once a tuple is created, you cannot modify its elements or size. Tuples are defined by enclosing a comma-separated sequence of elements within parentheses ( ).

Here's a brief introduction to tuples in Python:

  1. Creating a Tuple: You can create a tuple by defining it with parentheses and including the elements separated by commas.

Example:

pythonCopy code

# Tuple of integers numbers = (1, 2, 3, 4, 5) # Tuple of strings fruits = ("apple", "banana", "orange") # Mixed data types mixed_tuple = (10, "hello", 3.14, True)

  1. Accessing Elements: Similar to lists, you can access individual elements in a tuple using their index. The index of the first element is 0, the second element is 1, and so on. Negative indices can also be used to access elements from the end of the tuple.

Example:

pythonCopy code

numbers = (1, 2, 3, 4, 5) print(numbers[0])     # Output: 1 print(numbers[2])     # Output: 3 print(numbers[-1])    # Output: 5 (last element)

  1. Tuple Packing and Unpacking: You can create a tuple by simply separating values with commas, even without using parentheses. This is known as tuple packing. Additionally, you can unpack a tuple by assigning its elements to multiple variables.

Example:

pythonCopy code

# Tuple packing a = 10 b = "hello" c = 3.14 my_tuple = a, b, c print(my_tuple)       # Output: (10, "hello", 3.14) # Tuple unpacking x, y, z = my_tuple print(x)              # Output: 10 print(y)              # Output: "hello" print(z)              # Output: 3.14

  1. Immutable Nature: Unlike lists, tuples are immutable. Once a tuple is created, you cannot change its elements or size.

Example:

pythonCopy code

numbers = (1, 2, 3, 4, 5) numbers[0] = 10       # This will raise an error since tuples are immutable.

  1. Uses of Tuples: Tuples are often used to represent collections of related data, where the individual elements have different meanings and cannot be changed after creation. They are commonly used in scenarios where data integrity and immutability are required, such as dictionary keys, function arguments, and returning multiple values from functions.

In summary, tuples are similar to lists but with the key difference that they are immutable. They are used when you need to create a collection of items that should not be modified after creation.

2. Variables Data Types

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?