“Python Negative Indexierung” Code-Antworten

Negativer Index in der Pythonliste

# Negative indexing in lists
my_list = ['p','r','o','b','e']

# last item
print(my_list[-1])
#e
# fifth last item
print(my_list[-5])
#p
David Cao

Python Negative Indexierung

myList = [1, 2, 3, 4, 5]

# NORMAL INDEXING
print(myList[0]) # 1
print(myList[1]) # 2
print(myList[2]) # 3

# NEGATIVE INDEXING (STARTS FROM LAST ELEMENT IN LIST)
print(myList[-1]) # 5
print(myList[-2]) # 4
print(myList[-3]) # 3

print(myList[-3:]) # Last 3 elements
Ninja Penguin

Python Negative Indexierung

# Negative indexing for accessing tuple elements
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')

# Output: 't'
print(my_tuple[-1])

# Output: 'p'
print(my_tuple[-6])
SAMER SAEID

Python Negative Indexierung

# Negative indexing in lists
my_list = ['p','r','o','b','e']

# last item
print(my_list[-1])

# fifth last item
print(my_list[-5])
SAMER SAEID

Ähnliche Antworten wie “Python Negative Indexierung”

Fragen ähnlich wie “Python Negative Indexierung”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen