Skip to content

Instantly share code, notes, and snippets.

[2, 4, 6, 7, 10, 12, 13, 15, 17]
[2, 4, 6, 8, 10, 12, 13, 15, 17]
[2, 4, 6, 8, 10, 12, 14, 16, 18]
['Code Refer', 4, 6, 8, 10, 12, 14, 16, 18]
Traceback (most recent call last):
File "Listedit.py", line 21, in <module>
list_1[11] = 20
IndexError: list assignment index out of range
list_1 = [2, 4, 6, 7, 10, 12, 13, 15, 17]
print(list_1)
# Returns new value for 4th element
list_1[3] = 8
print(list_1)
# Returns new value for 7th, 8th & 9th element
['d', 'e', 'r']
['e', 'f', 'e', 'r']
['c', 'o', 'd']
list_1 = ['c', 'o', 'd', 'e', 'r', 'e', 'f', 'e', 'r']
# Return only 3rd,4th,5th items of list.
print(list_1[2:5])
# Return values from 6th item to last item of list
print(list_1[5:])
# Return values from first item of list to 4th element
print(list_1[:3])
e
r
Traceback (most recent call last):
File "NegativeIndex.py", line 9, in <module>
print(list_1[-10])
IndexError: list index out of range
list_1 = ['c', 'o', 'd', 'e', 'r', 'e', 'f', 'e', 'r']
# Print using Negative Indexing
print(list_1[-2])
print(list_1[-5])
# provided out of range index
print(list_1[-10])
d
c
r
Traceback (most recent call last):
File "Accesselements.py", line 11, in <module>
print(list_1[2.0])
TypeError: list indices must be integers or slices, not float
list_1 = ['c', 'o', 'd', 'e', 'r', 'e', 'f', 'e', 'r']
# Index value as integer
print(list_1[2])
print(list_1[0])
print(list_1[4])
# Index value as float
# List of integers
list_1 = [2, 3, 7, 9, 5]
# List of strings
list_2 = ["List", "Tuple", "Python"]
# List of different data types
list_3 = ["Coderefer", 23, 9.5]
NumberFormat us=NumberFormat.getCurrencyInstance().format(payment);