- Create a Tuple
- Access items in a Tuple
- Get the count of items in an Tuple
- Check if an item exists in a Tuple
- Loop through items in a Tuple
- Add items to a Tuple
Features of a Tuple
A tuple is a collection of objects. It contains the following features:
- It is ordered
- It is NOT changeable
- It allows duplicate members
- Smaller and faster than a list
1) Create a Tuple
#Create a tuple. Use parenthesis
workdays = ("Monday","Tuesday","Wednesday","Thursday","Friday")
print(workdays)

2) Access items in a Tuple
Items in a tuple are accessed by index. You can also use slice notation.
#Get items in a Tuple
workdays = ("Monday","Tuesday","Wednesday","Thursday","Friday")
#Get the first item
print(workdays[0])
#Get the last item
print(workdays[-1])
#Get the items starting at position 1 and ending before position 3
print(workdays[1:3])
#Get the first 3 items
print(workdays[:3])
#Get all the items starting at position 2
print(workdays[2:])

3) Get the count of items in an Tuple
To get the count of items in a Tuple use the len() method
#Get number of items in a tuple
workdays = ("Monday","Tuesday","Wednesday","Thursday","Friday")
print(len(workdays))

4) Check if an item exists in a Tuple
To check if an item exists in a Tuple use the in keyword
workdays = ("Monday","Tuesday","Wednesday","Thursday","Friday")
"Monday" in workdays

5) Loop through items in a Tuple
Use a for loop to loop though items in a Tuple.
workdays = ("Monday","Tuesday","Wednesday","Thursday","Friday")
for x in workdays:
print(x)

6) Add items to a Tuple
Tuples are not changeable. To add an item to a Tuple you must create a new Tuple.
#To add items to a tuple you must create a new Tuple
workdays = ("Monday","Tuesday","Wednesday","Thursday","Friday")
weekend = ("Saturday","Sunday")
daysofweek = workdays + weekend
print(daysofweek)

- Python Lesson 1: Hello World in Python
- Python Lesson 2: Reserved Keywords
- Python Lesson 3: Creating Variables
- Python Lesson 4: Performing math calculations in Python using operators
- Python Lesson 5: Working with Strings
- Python Lesson 6: Useful String Methods
- Python Lesson 7: Getting User Keyboard input
- Python Lesson 8: Importing Modules into your Python code
- Python Lesson 9: Decision making in Python
- Python Lesson 10: Creating loops in Python
- Python Lesson 11: Lists in Python
- Python Lesson 12: Tuples in Python
- Python Lesson 13: Dictionaries in Python
- Python Lesson 14: Sets in Python
- Python Lesson 15: Dealing with dates and time
- Python Lesson 16: Creating and using functions
- Python Lesson 17: Creating and using classes in Python
- Python Lesson 18: List Comprehensions
- Python Lesson 19: Lambda Functions
- Python Lesson 20: Error Handling