#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 16 16:10:02 2019

@author: chakra
"""

#A tuple is a collection which is ordered and unchangeable.
#In Python tuples are written with round brackets.

#in python tuples are written inside () braces

mytuple=("apple", "banana", "cherry")
print(mytuple)

print(mytuple[1]) #returns value at 2 thats index 1

#negative indexing
print(mytuple[-1])

#range of index
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5])

#negative indexing
print(thistuple[-4:-1]) #-1 last skipped

#Change Tuple Values
#Once a tuple is created, you cannot change its values. Tuples are unchangeable, 
#or immutable as it also is called. But there is a workaround. You can convert 
#the tuple into a list, change the list, and convert the list back into a tuple.

#Example
#Convert the tuple into a list to be able to change it:

x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
#banana will be changed to kiwi
print(x)

#Loop Through a Tuple
#You can loop through the tuple items by using a for loop.
#Example
#Iterate through the items and print the values:
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
  print(x)
  
#To determine if a specified item is present in a tuple use the in keyword:
#Example
#Check if "apple" is present in the tuple:
thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple:
  print("Yes, 'apple' is in the fruits tuple")
  
#Tuple Length
#To determine how many items a tuple has, use the len() method:
#Example
#Print the number of items in the tuple:
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))

#Remove Items
#Note: You cannot remove items in a tuple.

#Tuples are unchangeable, so you cannot remove items from it, but you can delete the tuple completely:
#Example
#The del keyword can delete the tuple completely:

thistuple = ("apple", "banana", "cherry")
del thistuple
#print(thistuple) #this will raise an error because the tuple no longer exists


#Join Two Tuples
#To join two or more tuples you can use the + operator:
#Example
#Join two tuples:
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2
print(tuple3)
#The tuple() Constructor
#It is also possible to use the tuple() constructor to make a tuple.
#Using the tuple() method to make a tuple:
thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
print(thistuple)
#-----------------------------------------------------------------------------------------------------------

'''Tuple Methods
Python has two built-in methods that you can use on tuples.

Method	Description
count()	Returns the number of times a specified value occurs in a tuple
index()	Searches the tuple for a specified value and returns the position of where it was found'''
#---------------------------------------------------------------------------------------------------------