Tuple In Programming Python

In Programming Python tuple learn
Tuple in the Python programming is a form of data structure that serves to store a set of data. Tuples are immutable , meaning that the contents of the value of the tuple can not be changed and deleted. But we can fill with a variety of values and objects.
In this material, the following things will be studied:
  • How to Make a Python tuple In Program
  • How to Access the value tuples
  • Slicing Value Tuple
  • Taking the long way tuple
  • Nested Tuple
  • Unpacking Sequence

How to Make a Python tuple In Program

Tuple is usually made by parentheses like the following example:

t = (1234, 4321, ‘Hello’)

in fact it could be without brackets are like this:

t = 1234, 4321, ‘World!’

Both are valid for use.

Creating Empty tuples and Singleton

If we want to create an empty tuple or no content of value, we can write the following example:

# Example of Making a Tuple Blank
empty = ()

Then to make a tuple with only one (singleton), then we should be adding a comma in end code.
Example code:

# Example Make TupleSingleton
one = ( ‘contents’)
oneValue = “contents of the course”,

Why should it be added in a coma?
Because if you do not add a comma, will be treated as a string.

one = ( ‘content’) # <- This string
oneValue = “contents of the course” # <- This is also a string

How to Access the value tuples

Just like material lists, tuples also has an index to Access the item or value in it. The tuples index sequence and the same list that is always starting from zero 0.
Example code:
# make a tuple
name = ('Hello World', 'FajarYusuf.Com', 'linux')
# access the tuple value
print (name [1])
So the result:
In Programming Python tuple learn
If we try to change the value, then the error will occur.
as the following example:

# make a tuple
name = ('Hello World', 'FajarYusuf.Com', 'linux')
# access the tuple value
name [1] = 'Coding'
print (name [1])

Tuple In Programming Python

Error because tuples are immutable or unchangeable value.

Slicing Value Tuple

Just like the list, tuples we can also do slicing .
Example code :
# Our prefix has this tuple
web = (123, 'FajarYusuf', 'https://www.FajarYusuf.com')
# then we want to cut it to be displayed
# from index numbers 1 to 2 only
print (web [1:2])
Obtained results:
Tuple In Programming Python
Slice same logic as in the list.

How to Take a Long-tuple

To take the length or number of items in tuples, how can be done by using the function len ().
Example code:
# make Tuple Day
days=('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',' Saturday ',' Sunday ')
# Take the tuple length of day
print ("Number of days:% d"% len (days))
Obtained results:
Tuple In Programming Python

Nested Tuple

Tuple can also be nested, meaning that tuples can be filled with tuples (tuple Nested).
Example code:

tuple1 = “me”, “like”, “you”
tuple2 = “for”, 5, “years”
tuple3 = (tuple1, tuple2) # <- nested tuple
print tuple3

tuple3 will contain the value of tuple1 and tuple2.
Tuple In Programming Python
Tuple can also be filled with any object such as a list, dictionary, object, and others.
Example :

t = ([1,2,3], {‘name’: ‘FajarYusuf’, ‘rank’, 123}, True)

Tuple t containing lists, dictionaries, and boolean values.

Unpacking Sequence

Tuple making process might be called packing, while to retrieve (extract) the entire contents is called unpacking.

# Prefix create such a tuple
web = 123,”FajarYusuf.Com“,”https://www.FajarYusuf.Com ”
# and in-unpacking
id_web, name, url = web
# then now three of these variables will be worth the
# appropriate in the tuple
# let us print
print (id_web)
print (name)
print (url)

The result:
In Programming Python tuple learn
By doing upacking, tuple contents will be copied to the variable. Then the variables we can do anything, such as changing the contents. Because the variables are mutable.

Leave a Reply

Your email address will not be published. Required fields are marked *