Tuple in Python explained
What is a tuple give an example?
A tuple is a collection that is ordered and unchangeable. In Python tuples, You can access tuple items by referring to the index number, inside square
Tuple Example:
# Tuple data structure
# tuple can store any data type
# most important tuples are immutable,
# one tuple is created you can’t update
# data inside a tuple
example = (‘one’,‘two’,‘three’,‘four’)
# no append,no insert,no pop, no remove
days = (‘monday’,‘tuesday’)
# tuple is faster than lists
Method
count, index len function slicing example[0]=1 # tuple do no assine number = (1,2,3,4,5,6,7.0)
What is nested list with examples?
In Python, the list which is inside the list is known as a nested list. Then how can we excess the nested list? Nested lists are obtained by using nested indexing.
Example to Excess Nested List:
my_list = [‘p’, ‘r’, ‘o’, ‘b’, ‘e’]
# Output: P
print (my_list [0]) # Output: o
print (my_list [2]) # Output: E
Print (my_list [4]) # Error! Only integers can be used for indexing
# my_list [4.0]
# Nested list
n_list = [“Happy”, [2,0,1,5]]
# Nested indexing
# Output: A
Print (n_list [0] [1])
# Output: 5
Print (n_list [1] [3])
How do you extract an element from a tuple? using for loop
number = (1,2,3,4,5,6,7.0)
for i in number:
print(i)
To learn More about tuple click Here
Recommended Post:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find a common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number