# Slice3py usrbinenv python slice3py n 1 2 3 4 5 6 7 8

• 31

This preview shows page 17 - 22 out of 31 pages.

slice3.py#!/usr/bin/env python# slice3.pyn = [1, 2, 3, 4, 5, 6, 7, 8]print(n[-4:-1])print(n[-1:-4])print(n[-5:])print(n[-6:-2:2])print(n[::-1])In this script, we form five lists. We also use negative indexnumbers.
print(n[-4:-1])print(n[-1:-4])The first line returns [5, 6, 7], the second line returns an emptylist. Lower indexes must come before higher indexes.print(n[::-1])This creates a reversed list.\$ ./slice3.py[5, 6, 7][][4, 5, 6, 7, 8][3, 5][8, 7, 6, 5, 4, 3, 2, 1]Output of the example.The above mentioned syntax can be used in assignments. Theremust be an iterable on the right side of the assignment.slice4.py#!/usr/bin/env python# slice4.pyn = [1, 2, 3, 4, 5, 6, 7, 8]n[0] = 10n[1:3] = 20, 30n[3::1] = 40, 50, 60, 70, 80print(n)We have a list of eight integers. We use the slice syntax to replacethe elements with new values.Traversing Python listsThis section will point out three basic ways to traverse a list inPython.traverse.py#!/usr/bin/env python# traverse.pyn = [1, 2, 3, 4, 5]
for e in n:print(e, end=" ")print()The first one is the most straightforward way to traverse a list.n = [1, 2, 3, 4, 5]We have a numerical list. There are five integers in the list.for e in n:print(e, end=" ")Using theforloop, we go through the list one by one and print thecurrent element to the console.\$ ./traverse.py1 2 3 4 5This is the output of the script. The integers are printed to theterminal.The second example is a bit more verbose.traverse2.py#!/usr/bin/env python# traverse2.pyn = [1, 2, 3, 4, 5]i = 0s = len(n)while i < s:print(n[i], end=" ")i = i + 1print()We are traversing the list using thewhileloop.i = 0l = len(n)First, we need to define a counter and find out the size of the list.
while i < s:print(n[i], end=" ")i = i + 1With the help of these two numbers, we go through the list andprint each element to the terminal.Theenumerate()built-in function gives us both the index and thevalue of a list in a loop.traverse3.py#!/usr/bin/env python# traverse3.pyn = [1, 2, 3, 4, 5]print(list(enumerate(n)))for e, i in enumerate(n):print("n[{0}] = {1}".format(e, i))In the example, we print the values and the indexes of the values.\$ ./traverse3.py[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]n[0] = 1n[1] = 2n[2] = 3n[3] = 4n[4] = 5Running the script.Python counting list elementsSometimes it is important to count list elements. For this, Pythonhas thecount()method.counting.py#!/usr/bin/env python# counting.pyn = [1, 1, 2, 3, 4, 4, 4, 5]print(n.count(4))print(n.count(1))print(n.count(2))
print(n.count(6))In this example, we count the number of occurrences of a fewnumbers in thenlist.n = [1, 1, 2, 3, 4, 4, 4, 5]We have a list of integer numbers. Integers 1 and 4 are presentmultiple times.print(n.count(4))print(n.count(1))print(n.count(2))print(n.count(6))Using thecount()method, we find out the occurrence of 4, 1, 2,and 6 numbers.\$ ./counting.py3210Number 4 is present 3 times, 1 twice, 2 once, and 6 is not presentin the list.Python nested listsIt is possible to nest lists into another lists. With a nested list anew dimension is created. To access nested lists one needsadditional square brackets[].

Course Hero member to access this document

Course Hero member to access this document

End of preview. Want to read all 31 pages?