Wednesday, 1 March 2023

Tuples in Python Programming

 In Python, a tuple is an immutable sequence of values, similar to a list. However, tuples cannot be modified once they are created. Tuples are defined using parentheses () and elements are separated by commas.

Here's an example of creating a tuple:

makefile
my_tuple = (1, 2, 3, "hello")

You can access elements in a tuple using indexing, just like you would with a list:

lua
print(my_tuple[0]) # output: 1 print(my_tuple[3]) # output: "hello"

Tuples can also be unpacked into separate variables:

css
a, b, c, d = my_tuple print(a, b, c, d) # output: 1 2 3 "hello"

Tuples can be useful in situations where you need to pass around a collection of values that should not be modified, such as returning multiple values from a function or defining keys in a dictionary.

No comments:

Post a Comment

Program For String

 # This program demonstrates various string operations # Define a string variable my_string = "Hello, World!" # Print the string p...