#4 - Python Tuples

#4 - Python Tuples

By Ifeanyi Omeata


Topics:


1. Tuples


1. Tuples


>>Return to Menu
Tuple is one of Python's four built-in data types for storing collections of data; the other three are List, Set, and Dictionary, all of which have different properties and applications.
A tuple is a collection of items that is both ordered and immutable (unchangeable), and allow duplicate values.
Tuples are simply immutable Lists.
Round brackets are used to write tuples.

- Ordered:
Items have a defined order, and that order will not change. If you add new items to a list, the new items will be placed at the end of the list.

- Unchangeable:
We cannot change, add, and remove items in a list after it has been created or formed.

- Allow Duplicates:
Tuples are indexed, hence they can have items with the same value.**

#empty Tuple
empty_tuple = ()
empty_tuple = tuple()

people = ('Ben', 'Samuel', 'Mary', 'Bethany', 'Cross')

print(people)
#The person at index=2
print(people[2])
#The last person in the tuple
print(people[-1])
#The people from index=2 to index=4(excluded)
print(people[2:4])
#Everyone from index=2
print(people[2:])
#Everyone from the first person to index=4(excluded)
print(people[:4])
#Everyone from the first person with step=2
print(people[::2])
#The tuple in reverse
print(people[::-1])
#The number of people in the tuple
print(len(people))
#The Index location of person 'Bethany'
print(people.index('Bethany'))
#Number of times 'Mary' is found in the tuple
print(people.count('Mary'))

image.png

#End


Hope you enjoyed this! :) Follow me for more contents...


Get in Touch:
ifeanyiomeata.com

Youtube: youtube.com/c/IfeanyiOmeata
Linkedin: linkedin.com/in/omeatai
Twitter: twitter.com/iomeata
Github: github.com/omeatai
Stackoverflow: stackoverflow.com/users/2689166/omeatai
Hashnode: hashnode.com/@omeatai
Medium: medium.com/@omeatai
© 2022