Chapter 7 - Lists in Python



AD

7.1.0 Lists in Python

A list contains a list of values assigned to a variable. A list is essentially a single dimension array. Arrays proper are imported in a separate module. We will do that later. We create a list as follows:

mylist = [3,6,9,32]
print(mylist)
print(mylist[0])
print(mylist[1])
print(mylist[2])
print(mylist[3])

#Output
# [3, 6, 9, 32]
# 3
# 6
# 9
# 32

In the second half of the code I have printed individual items of the array. Notice the list starts with index 0.

7.2.0 Two Dimensional Lists

Two dimensional lists are also useful. They are similar in appearance to 2D arrays. We will cover arrays in a future chapter.

x = [[44,8,99], [6,3,87]]
print(x)

x[0][0] = 7

print(x[0][0])
print(x)

# Output
# [[44, 8, 99], [6, 3, 87]]    # Beginning 2D list
# 7    # Replacement value for Row 0, Column 0
# [[7, 8, 99], [6, 3, 87]]   # Final 2D list

Run the code and you will see the output as listed in the lines beginning with a # mark.





Engineering-Python

SALARSEN.COM
Table of Contents
Ch1-Install Python
Ch2-Install PyCharm
Ch3-Save Work
Ch4-Add Project
Ch5-Variables
Ch6-Print&Input
Ch7-Lists
Ch8-Loops
Ch9-If&Logical
Ch10-Functions
Ch11-Bubble Sort
Ch12-Plotting
Ch13-Files
Ch14-Print Format
Ch15-Dict&Comp&Zip
Ch16-Arrays
Ch17-Electrical
Ch18-Regression
Ch19-Differential
Ch20-Secant